线程的“优先级”将线程的重要性传递给了调度器。尽管CPU处理现有线程集的顺序是不确定的。但是调度器将倾向于让优先权最高的线程先执行。然而,这并不意味着优先权较低的线程将得不到执行(也就是说,优先权不会导致死锁)。优先级较低的线程,仅仅是执行的频率较低。
在绝大多数的时间里,所有线程都应该以默认的优先级运行。
JDK中有10个优先级,但它与大多数操作系统都不能映射的很好。比如,Windows有7个优先级且不是固定的,所以这种映射关系也是不确定的。Sun的Solareis有2的31次方个优先级。唯一可移植的方法是当调整优先级的时候,只使用以下三种级别:
- Thread.MIN_PRIORITY = 1
- Thread.NORM_PRIORITY = 5
- Thread.MAX_PRIORITY = 10
示例:
public class SimplePriorities implements Runnable {
private int countDown = 5;
private volatile double d;
private int priority;
public SimplePriorities(int priority) {
this.priority = priority;
}
public String toString() {
return Thread.currentThread() + ": " + countDown;
}
public void run() {
Thread.currentThread().setPriority(priority);
while (true) {
for (int i = 0; i < 100000; i++) {
d += (Math.PI + Math.E) / i;
if (i % 1000 == 0)
Thread.yield();
}
System.out.println(this);
if (--countDown == 0)
return;
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new SimplePriorities(Thread.MIN_PRIORITY));
Thread t2 = new Thread(new SimplePriorities(Thread.NORM_PRIORITY));
Thread t3 = new Thread(new SimplePriorities(Thread.MIN_PRIORITY));
Thread t4 = new Thread(new SimplePriorities(Thread.NORM_PRIORITY));
Thread t5 = new Thread(new SimplePriorities(Thread.MIN_PRIORITY));
Thread t = new Thread(new SimplePriorities(Thread.MAX_PRIORITY));
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t.start();
}
}
执行结果 写道
Thread[Thread-5,10,main]: 5
Thread[Thread-3,5,main]: 5
Thread[Thread-5,10,main]: 4
Thread[Thread-3,5,main]: 4
Thread[Thread-5,10,main]: 3
Thread[Thread-3,5,main]: 3
Thread[Thread-5,10,main]: 2
Thread[Thread-3,5,main]: 2
Thread[Thread-5,10,main]: 1
Thread[Thread-3,5,main]: 1
Thread[Thread-1,5,main]: 5
Thread[Thread-1,5,main]: 4
Thread[Thread-1,5,main]: 3
Thread[Thread-4,1,main]: 5
Thread[Thread-2,1,main]: 5
Thread[Thread-0,1,main]: 5
Thread[Thread-1,5,main]: 2
Thread[Thread-1,5,main]: 1
Thread[Thread-4,1,main]: 4
Thread[Thread-2,1,main]: 4
Thread[Thread-0,1,main]: 4
Thread[Thread-4,1,main]: 3
Thread[Thread-2,1,main]: 3
Thread[Thread-0,1,main]: 3
Thread[Thread-4,1,main]: 2
Thread[Thread-2,1,main]: 2
Thread[Thread-0,1,main]: 2
Thread[Thread-4,1,main]: 1
Thread[Thread-2,1,main]: 1
Thread[Thread-0,1,main]: 1
Thread[Thread-3,5,main]: 5
Thread[Thread-5,10,main]: 4
Thread[Thread-3,5,main]: 4
Thread[Thread-5,10,main]: 3
Thread[Thread-3,5,main]: 3
Thread[Thread-5,10,main]: 2
Thread[Thread-3,5,main]: 2
Thread[Thread-5,10,main]: 1
Thread[Thread-3,5,main]: 1
Thread[Thread-1,5,main]: 5
Thread[Thread-1,5,main]: 4
Thread[Thread-1,5,main]: 3
Thread[Thread-4,1,main]: 5
Thread[Thread-2,1,main]: 5
Thread[Thread-0,1,main]: 5
Thread[Thread-1,5,main]: 2
Thread[Thread-1,5,main]: 1
Thread[Thread-4,1,main]: 4
Thread[Thread-2,1,main]: 4
Thread[Thread-0,1,main]: 4
Thread[Thread-4,1,main]: 3
Thread[Thread-2,1,main]: 3
Thread[Thread-0,1,main]: 3
Thread[Thread-4,1,main]: 2
Thread[Thread-2,1,main]: 2
Thread[Thread-0,1,main]: 2
Thread[Thread-4,1,main]: 1
Thread[Thread-2,1,main]: 1
Thread[Thread-0,1,main]: 1
分析:
- 注意,优先级的设置是在run()方法的开头进行的,在构造函数中设置并无什么好处,因为此时线程并未开始执行
- 代码中加入了数学运算,造成较大开销,目的是为了更好的体现优先级设置的效果
- 控制台输出也会有较大的开销,但是不能被中断,而复杂的浮点运算,可以被中断
- 只有数学运算的时间足够长,线程调度机制才来得及介入,交换任务并关注优先级,使得最高优先级线程被最先选择。
本文源自《Thinking in Java》(Fourth Edition)——21.2.6 优先级(其中内容略有调整,主体思想源自该章节)。