線程優先級
阿新 • • 發佈:2017-06-05
jid read main rgs prior art mis nts 重寫
package youxianji.xianchen; import java.util.MissingFormatArgumentException; /* * 設置線程的優先級 * setPriority public final void setPriority(int newPriority) 參數: newPriority - 要為線程設定的優先級 Java中 三種 優先級 static int MAX_PRIORITY 線程可以具有的最高優先級。 static int MIN_PRIORITY 線程可以具有的最低優先級。 static int NORM_PRIORITY 分配給線程的默認優先級。*/ //寫一個類去實現runnable class YouXianJiDemo implements Runnable{ //重寫 run 方法 public void run(){ //循環 5次 for (int i = 0; i <5; i++) { try {Thread.sleep(500);} catch(Exception e){ System.out.println(e); } //獲取當前線程 System.out.println(Thread.currentThread().getName()+"運行"+i); } } }public class YouXianJi { public static void main(String[] args) { //YouXianJiDemo yx =new YouXianJiDemo(); //Thread t1 =new Thread(yx,"線程a"); Thread t1 = new Thread(new YouXianJiDemo(),"線程a"); Thread t2 = new Thread(new YouXianJiDemo(),"線程b"); Thread t3 = new Thread(newYouXianJiDemo(),"線程c"); t1.setPriority(Thread.NORM_PRIORITY); t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.MIN_PRIORITY); //啟動線程 t1.start(); t2.start(); t3.start(); } }
線程優先級