getPriotity(),获取当前线程优先级
setPriotity(),设置当前线程优先级(优先级必须启动前设置)
如不设置优先级,则默认优先级为5,优先级别从低到高为1到10,包含1和10,优先级相同时顺序执行
package Day8;
public class Priority {
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(10);
t3.start();
t4.setPriority(8);
t4.start();
t5.setPriority(5);
t5.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority());
}
}
运行结果
main 5
Thread-0 5
Thread-2 10
Thread-3 8
Thread-4 5
Thread-1 1