Java如何設置線程的優先級?
如何設置一個線程的優先級?
解決方法
下麵的例子如何設置一個線程的優先級。
public class Main { public static void main(String[] args) throws Exception { Thread thread1 = new Thread(new TestThread(1)); Thread thread2 = new Thread(new TestThread(2)); thread1.setPriority(Thread.MAX_PRIORITY); thread2.setPriority(Thread.MIN_PRIORITY); thread1.start(); thread2.start(); thread1.join(); thread2.join(); System.out.println("The priority has been set."); } }
結果
上麵的代碼示例將產生以下結果。
The priority has been set.