?? prioritydemo.java
字號:
//優(yōu)先級演示.
class Priority implements Runnable{
int count;
Thread thrd;
static boolean stop = false;
static String currentName;
/*創(chuàng)建一個新的線程. 注意構造方法并沒有真正開始這個線程的運行*/
Priority(String name){
thrd = new Thread(this, name);
count =0;
currentName = name;
}
//Begin execution of new thread.
public void run(){
System.out.println(thrd.getName()+" 開始執(zhí)行.");
do{
count++;
if(currentName.compareTo(thrd.getName())!=0){
currentName = thrd.getName();
System.out.println("在 "+currentName);
}
}while((stop==false)&&(count<100000000));
stop= true;
System.out.println("\n"+thrd.getName()+" 結束.");
}
}
class PriorityDemo {
public static void main(String args[]){
Priority mt1 = new Priority("高優(yōu)先級.");
Priority mt2 = new Priority("低優(yōu)先級.");
//設置優(yōu)先級.
mt1.thrd.setPriority(Thread.NORM_PRIORITY+2);
mt2.thrd.setPriority(Thread.NORM_PRIORITY-2);
//開始線程
mt1.thrd.start();
mt2.thrd.start();
try{
mt1.thrd.join();
mt2.thrd.join();
}catch(InterruptedException e){
System.out.println("主線程被打斷.");
}
System.out.println("\n高優(yōu)先級線程計數(shù)到 "+
mt1.count);
System.out.println("\n低優(yōu)先級線程計數(shù)到 "+
mt2.count);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -