Java Thread Programming 1.6 - Thread Prioriti…

2008-02-23 09:36:20来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Thread:PY0/Priority:3, count=0 ,0%
Thread:PY1/Priority:6, count=235049 ,51%
Thread:PY2/Priority:6, count=219616 ,48%
run with yield
totalcount=401611 counter/ms=40
Thread:PY0/Priority:3, count=0 ,0%
Thread:PY1/Priority:6, count=200805 ,49%
Thread:PY2/Priority:6, count=200806 ,50%
通过调用yield的对比实验很明显看出
1、 优先级高的获得更多的处理器周期
2、 同一优先级的线程又相同的运行机会
3、 调用yield不会增加性能,而是降低了cpu总周期
Also note that without yielding, the count/ms was about 76 (line 3), and when yielding was done, it was cut down to about 25 (line 10). This shows that the yielding caused a lot of context switches that brought with them some significant overhead. In this example, the excessive yielding wasted processor resources.
频繁的yield浪费处理器资源
When assigning priorities to the threads in your application, use the higher priorities only for threads that block frequently (sleeping, waiting, I/O). CPU-intensive calculations should be done with a medium- to low-priority thread to ensure that the processor is not hogged. Avoid setting a priority to Thread.MAX_PRIORITY unless the thread spends nearly all of its time blocked or is very short-lived.
给经常阻塞的线程高的优先级,给密集运算的线程中低优先级保证处理器不会被独占,避免设置优先级为Thread.MAX_PRIORITY,除非线程的生命周期特别短或者整个生命周期中基本都处于阻塞状态
Thread scheduling varies from VM to VM and from operating system to operating system. Because of these differences, prioritization should only be used to try to improve responsiveness and efficiency, and should not be relied upon for program correctness.
不同的虚拟机不同的操作系统有不同的线程调用机制,由于这些不同,线程优先级只是用来提高响应和效率的一种手段,不能作为程序正确性的依赖。

上一篇: 软件开发工作的几点感想(原创)
下一篇: 【原创】单元测试策略(Junit版)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【原创】单元测试策略(Junit版)

下一篇:使用Maven构造Turbine项目