代码演示
start()调度之前先设置优先级
// 测试线程优先级
public class TestPriority {
public static void main(String[] args) {
// 主线程有默认的优先级
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority()); //main主线程
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority,"1");
Thread t2 = new Thread(myPriority,"2");
Thread t3 = new Thread(myPriority,"3");
Thread t4 = new Thread(myPriority,"4");
Thread t5 = new Thread(myPriority,"5");
Thread t6 = new Thread(myPriority,"6");
// 不设置优先级
t1.start(); // 默认优先级---5
t2.setPriority(2);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(6);
t4.start();
t5.setPriority(Thread.MAX_PRIORITY);
t5.start();
t6.setPriority(Thread.MIN_PRIORITY);
t6.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println("Thread"+Thread.currentThread().getName() + "--->" + Thread.currentThread().getPriority());
}
}
结论:默认优先级一直优先,执行的顺序不一定按照优先级来运行。