线程的优先级

 1 package 多线程练习;
 2 
 3 public class Thread优先级 {
 4     public static void main(String[] args) {
 5         Runnable runnable = () -> {
 6             System.out.println("线程名:" + Thread.currentThread().getName() + " <-> 优先级:" + Thread.currentThread().getPriority());
 7         };
 8 
 9         // 装载线程
10         Thread t1 = new Thread(runnable, "t1");
11         Thread t2 = new Thread(runnable, "t2");
12         Thread t3 = new Thread(runnable, "t3");
13         Thread t4 = new Thread(runnable, "t4");
14         Thread t5 = new Thread(runnable, "t5");
15 
16         // 设置优先级的大小  具体实际优先级看CPU的调度 设置还是能代表一定的优先权重
17         t1.setPriority(Thread.MIN_PRIORITY);
18         t1.start();
19 
20         t2.setPriority(2);
21         t2.start();
22 
23         t3.setPriority(3);
24         t3.start();
25 
26 
27         t4.start();
28 
29         t5.setPriority(Thread.MAX_PRIORITY);
30         t5.start();
31 
32 
33     }
34 }

 

posted @ 2022-01-15 15:58  Chris丶Woo  阅读(19)  评论(0编辑  收藏  举报