java 多線程優先級

多線程–優先級

常用方法 描述
setPriority(值) 設置優先級
getPriority(值) 獲取優先級

值:

常量
NORM_PRIORITY 5 ( 默認)
MIN_PRIORITY 1
MAX_PRIORITY 10
public class YouXianJi {
	public static void main(String[] args) {
		YXJ yxj = new YXJ();
		Thread t1 = new Thread(yxj,"A");
		Thread t2 = new Thread(yxj,"B");
		Thread t3 = new Thread(yxj,"C");
		//設置優先級
		t1.setPriority(Thread.NORM_PRIORITY);
		t2.setPriority(Thread.MIN_PRIORITY);
		t3.setPriority(Thread.MAX_PRIORITY);
		//開啓線程
		t1.start();
		t2.start();
		t3.start();
	}
}
class YXJ implements Runnable{
	public void run() {
		System.out.println(Thread.currentThread().getName()+"---執行"+Thread.currentThread().getPriority());
	}
}

注意咯,優先級不是越高執行的概率就越高。(不信?你試試!)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章