优先级的范围是1-10,使用getPriority()获取,setPriority()改变
main方法的默认优先级是5
优先级的高低值意味着被CPU调度的可能性高低,能否被调度还是CPU,所以不一定优先级高就一定先被调用,只能增加概率
package com.thread.state;
?
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"->"+Thread.currentThread().getPriority());
?
MyPriority myPriority = new MyPriority();
?
Thread t1=new Thread(myPriority);
Thread t2=new Thread(myPriority);
Thread t3=new Thread(myPriority);
Thread t4=new Thread(myPriority);
Thread t5=new Thread(myPriority);
?
//先设置优先级再启动
t1.start();
?
t2.setPriority(1);
t2.start();
?
t3.setPriority(8);
t3.start();
?
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();
?
t5.setPriority(9);
t5.start();
}
?
}
class MyPriority implements Runnable{
?
原文:https://www.cnblogs.com/Share-my-life/p/14759207.html