問題:?已經設置了 線程的優先級, ?但輸出結果為什么是這樣, ?是因為 電腦 是 雙核的嗎?class?SETPriority?implements?Runnable{
public?void?run(){
for(int?count?=?0;?count?<?10;?count++)
System.out.println(?Thread.currentThread().getName()?+?count);
}
}
public?class?SetPriorityDemo{
public?static?void?main(String[]?args){
Thread?maxPro?=?new?Thread(new?SETPriority(),?"MAX...");
Thread?minPro?=?new?Thread(new?SETPriority(),?"min...");
maxPro.setPriority(10); //設置優先級?為?10
minPro.setPriority(1);??//設置優先級?為??1
maxPro.start();
minPro.start();
System.out.println("........END");?//main?線程的優先級?為?普通,?相當于?5
}
}下面列舉 幾種 輸出結果?----------------------------分割線----------------------------------------------------------------分割線------------------------------------
3 回答

慕標2337738
TA貢獻23條經驗 獲得超13個贊
package?practice; class?SETPriority?implements?Runnable{ ????public?void?run(){ synchronized?("")?{ ? ????????for(int?count?=?0;?count?<?10;?count++) ????????????System.out.println(?Thread.currentThread().getName()?+?count); ????} } } ? public?class?SetPriorityDemo{ ????public?static?void?main(String[]?args){ ????????Thread?maxPro?=?new?Thread(new?SETPriority(),?"MAX..."); ????????Thread?minPro?=?new?Thread(new?SETPriority(),?"min..."); ? ????????maxPro.setPriority(10);?//設置優先級?為?10 ????????minPro.setPriority(1);??//設置優先級?為??1 ? ????????maxPro.start(); ????????minPro.start(); ? ????????System.out.println("........END");?//main?線程的優先級?為?普通,?相當于?5 ????} }
加鎖,線程不安全。

甫艾蒽廷
TA貢獻38條經驗 獲得超21個贊
每個類都有自己的優先級,一般property用1-10的整數表示,默認優先級是5,優先級最高是10;優先級高的線程并不一定比優先級低的線程執行的機會高,只是執行的機率高;默認一個線程的優先級和創建他的線程優先級相同;
添加回答
舉報
0/150
提交
取消