1 package com.thread.state; 2 3 //测试线程优先级 4 public class TestPriority { 5 6 public static void main(String[] args) { 7 8 //主线程默认优先级 9 System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority()); 10 11 MyPriority myPriority = new MyPriority(); 12 13 Thread t1 = new Thread(myPriority); 14 Thread t2 = new Thread(myPriority); 15 Thread t3 = new Thread(myPriority); 16 Thread t4 = new Thread(myPriority); 17 Thread t5 = new Thread(myPriority); 18 Thread t6 = new Thread(myPriority); 19 20 //先设置优先级,再启动 21 t1.start(); 22 23 t2.setPriority(1); 24 t2.start(); 25 26 t3.setPriority(4); 27 t3.start(); 28 29 t4.setPriority(Thread.MAX_PRIORITY);//10 30 t4.start(); 31 32 33 } 34 } 35 36 class MyPriority implements Runnable { 37 38 @Override 39 public void run() { 40 System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority()); 41 } 42 } 43 44 结果: 45 main-->5 46 Thread-0-->5 47 Thread-1-->1 48 Thread-3-->10 49 Thread-2-->4
原文:https://www.cnblogs.com/duanfu/p/12260549.html