Java Thread Programming 1.6 - Thread Prioriti…

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

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

System Thread Priorities—JDK 1.2
Priority
Thread Name
5
main
8
Finalizer
10
Reference Handler
5
Signal dispatcher
5
AWT-Windows
6
AWT-EventQueue-0
5
SunToolkit.PostEventQueue-0
4
Screen Updater
A notable change in JDK 1.2 is that the priority of the Finalizer thread was increased from 1 to 8 to help ensure that the garbage collector gets more of a chance to run and free up memory.
Jdk1.2中Finalizer线程的优先级从1升高到8,确保垃圾收集器能及时回收资源释放内存
When assigning priorities to your threads, you should consider the relative priorities of the system threads to be sure your threads don’t overwhelm their operations. By default, when a new Thread is constructed, it runs at the same priority as the thread that constructed it. Most new threads are constructed directly or indirectly by the main thread and will therefore run at a priority of 5. This works well under many scenarios, but there are times when you will want to raise or lower a thread’s priority.
子线程的优先级和父线程的优先级相同,大部分线程从main线程创建,所以优先级为5
There are three public static final int member variables of Thread that indicate the range of priority values that can be passed to setPriority().
Thread.MAX_PRIORITY
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
Thread类中有三个静态优先级变量
Thread.MAX_PRIORITY,最高优先级,通常为10
Thread.MIN_PRIORITY,最低优先级,通常为1
Thread.NORM_PRIORITY,一般优先级,通常为5
getPriority()
The getPriority() method of Thread returns an int representing the current priority of the thread. Generally, a thread’s priority does not change over its lifetime, but it can.
getPriority()返回线程的当前优先级,通常线程的优先级在其生命周期中不会变,但可以变
setPriority()
The setPriority() method of Thread takes an int as a parameter indicating the new priority for the thread. The setPriority() method can be invoked either before the thread is started or once it is running. Some VMs might allow you to change the priority of the system threads, but this is not recommended—even if it is permitted.
setPriority()设置线程新的优先级,可以在线程开始前调用,也可以在线程运行中调用,有些虚拟机允许改变系统线程的优先级,但我们不推荐这么做
Calls to setPriority can throw a SecurityException if the calling thread is not permitted to make the priority change. This will happen only if there is a SecurityManager installed and it rejects the change request. By default, applications do not have a SecurityManager.
如果不允许线程优先级改变,调用setPriority()方法时会抛出SecurityException异常,当安装了SecurityManager时,可能会出现这种情况
An IllegalArgumentException will be thrown if the specified priority is greater than Thread.MAX_PRIORITY or less than Thread.MIN_PRIORITY.allowed for the thread group. For example, in this code fragment
Thread t = //...
ThreadGroup tg = t.getThreadGroup();
int groupMax = tg.getMaxPriority();
the maximum value for the thread priority of thread t is groupMax. Usually, this is the same as Thread.MAX_PRIORITY. When groupMax is less than Thread.MAX_PRIORITY, calls to setPriority() still work, but will silently reduce the value passed to groupMax. If groupMax is 6 and setPriority(9) is called, the code will run as if setPriority(6) was called instead. See Chapter 10, “Thread Groups,” for more on ThreadGroup.
如果将线程优先级设置在线程组的Thread.MAX_PRIORITY,和Thread.MIN_PRIORITY之外,会抛出IllegalArgumentException异常。线程组允许的最大优先级和Thread.MAXPRIORITY可能会不一致,如果线程组最大优先级为6,而将线程优先级设置为9,那么代码执行的结果和setPriority(6)一样。
Thread States
State
Blocked
Interrupted
Discription
Running
Current running on the processor
Ready to run
Waiting for the processor
Sleeping
Will move to “ready to run” state
after a certain amount of time has Eclipsed or after being interrupted
Waiting

标签:

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

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

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