package ThreadDemo;
// 线程优先级 1--10 // 默认优先级5
// 优先级高不代表一定先执行,概率变高而已
public class Test16_Priority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); // 主线程的优先级
// 子线程
Thread t1 = new Thread(new PriorityDemo(), "t1");
Thread t2 = new Thread(new PriorityDemo(), "t2");
Thread t3 = new Thread(new PriorityDemo(), "t3");
Thread t4 = new Thread(new PriorityDemo(), "t4");
// 先设置优先级,再启动线程
t1.setPriority(1);
t1.start();
t2.setPriority(5);
t2.start();
t3.setPriority(Thread.MAX_PRIORITY); // 10
t3.start();
t4.setPriority(11); // 出错
t4.start();
}
}
class PriorityDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
} // 打印优先级
}
16 线程优先级
发布时间 2023-09-07 16:14:36作者: 被占用的小海海