手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

Java Thread Programming 1.6 - Thread Prioritization

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
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

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!