JAVA中一个线程有自己的优先级(priority)
getPriority:返回当前线程的优先级
setPriority:设置当前线程的优先级
既然JAVA支持多线程,那么多个线程同时执行的时候必然涉及到调度的问题。
通过研究官方文档和书籍,我发现:JAVA的调度算法非常简单:
1.选择当前可运行线程中优先级最高的线程运行
2.拥有同样优先级的线程:采用round-robin的方式。
JAVA是preemptive的,但是不同平台的实现并不一定能保证这一点。
下面是一段引用:
For the runtime on a Solaris Operating Environment platform,Java technology does not preempt threads of the same priority.However,the runtime on Microsoft Windows platforms uses time-slicing,so it preempts threads of the same priority and even threads of higher priority.Preemption is not guaranteed;however,most JVM implementations result in behavior that appears to be strictly preemptive.Across JVM implementations,there is no absolute guarantee of preemption or time-slicing.The only guarantees lie in the coder’s use of wait and sleep. The model of a preemptive scheduler is that many threads might be runnable,but only one thread is actually running.This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable.In the latter case,the lower priority thread is preempted by the thread of higher priority,which gets a chance to run instead.
看了这段引用后,发现讨论JAVA线程的调度似乎没有什么意义了,呵呵。
下面我是我的一个运行结果。我是win7,双核处理器。从结果看出来,基本还是满足前面两点的。
package multithread;
public class PrioritedThread extends Thread{
public PrioritedThread(int i){
if(i >0 && i < 11){
this.setPriority(i);
}
}
public void run(){
int i = 0;
while(true){
if(i == 100000000){
System.out.print(this.getPriority() + "\t");
i = 0;
}
i++;
}
}
}
package multithread;
public class TestPriority {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 1; i < 11; i++){
Thread temp = new PrioritedThread(i);
temp.start();
}
}
}
运行结果:
10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 8 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 7 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 4 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 6 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 3 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 9 10 10 2 9 10 10 9 10 10 9 10 10 9 10 10 10 9 10 10 10 9 10 10 9 10 10 8 10 10 8 10 10 8 10 10 8