?? prioritydemo.java
字號:
/*
* PriorityDemo.java
*
* Created on 2005年4月11日, 下午2:47
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Liu Bin
* @version
*/
public class PriorityDemo extends MIDlet implements CommandListener {
//Display管理
Display display = null;
Form form = new Form("線程優先級演示");
Command cmdExit = new Command("退出", Command.STOP, 1);
Command cmdRun = new Command("同優先級運行", Command.ITEM, 2);
Command cmdRunHNL = new Command("以高中低優先級運行", Command.ITEM, 2);
Command cmdRunHLN = new Command("以高低中優先級運行", Command.ITEM, 2);
Command cmdRunLNH = new Command("以低中高優先級運行", Command.ITEM, 2);
Command cmdRunNHL = new Command("以中高低優先級運行", Command.ITEM, 2);
MyThreaad p1, p2, p3;
int[] priorty = {5, 5, 5};
public PriorityDemo() {
form.addCommand(cmdExit);
form.addCommand(cmdRun);
form.addCommand(cmdRunHNL);
form.addCommand(cmdRunHLN);
form.addCommand(cmdRunLNH);
form.addCommand(cmdRunNHL);
form.setCommandListener(this);
}
public void startApp() {
display = Display.getDisplay(this); //獲得當前MIDlet的Display對象
display.setCurrent(form); //設置form對象為當前顯示對象
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
destroyApp(true);
} else if (cmd == cmdRun) {
System.out.println("\n\n==========同優先級測試==========\n");
this.priorty[0] = Thread.NORM_PRIORITY;
this.priorty[1] = Thread.NORM_PRIORITY;
this.priorty[2] = Thread.NORM_PRIORITY;
runThread();
} else if (cmd == cmdRunHNL) {
System.out.println("\n\n========高中低優先級測試==========\n");
this.priorty[0] = Thread.MAX_PRIORITY;
this.priorty[1] = Thread.NORM_PRIORITY;
this.priorty[2] = Thread.MIN_PRIORITY;
runThread();
} else if (cmd == cmdRunHLN) {
System.out.println("\n\n==========高低中優先級測試==========\n");
this.priorty[0] = Thread.MAX_PRIORITY;
this.priorty[1] = Thread.MIN_PRIORITY;
this.priorty[2] = Thread.NORM_PRIORITY;
runThread();
} else if (cmd == cmdRunLNH) {
System.out.println("\n\n==========低中高優先級測試==========\n");
this.priorty[0] = Thread.MIN_PRIORITY;
this.priorty[1] = Thread.NORM_PRIORITY;
this.priorty[2] = Thread.MAX_PRIORITY;
runThread();
} else if (cmd == cmdRunNHL) {
System.out.println("\n\n==========中高低優先級測試==========\n");
this.priorty[0] = 2;
this.priorty[1] = Thread.MAX_PRIORITY;
this.priorty[2] = Thread.MIN_PRIORITY;
runThread();
}
}
void runThread() {
//創建線程1
p1 = new MyThreaad(1);
//設置最低優先級
p1.setPriority(this.priorty[0]);
//執行線程1
p1.start();
//創建線程2
p2 = new MyThreaad(2);
//設置普通優先級
p2.setPriority(this.priorty[1]);
//執行線程2
p2.start();
//創建線程3
p3 = new MyThreaad(3);
//設置最高優先級
p3.setPriority(this.priorty[2]);
//執行線程3
p3.start();
}
}
/**
* 從Thread繼承創建線程
*/
class MyThreaad extends Thread {
//當前線程的ID標識
long id;
MyThreaad(long id) {
this.id = id;
}
public void run() {
System.out.println("線程"+ id + "開始執行\n");
out(id);
System.out.println("線程"+ id + "執行完畢\n");
}
static void out(long id) {
for (int k = 0; k < 5; k++) {
int j=0;
while(j<100000) {
j++;
}
System.out.println("線程"+ id + ",優先級" +
Thread.currentThread().getPriority() + " 當前K值: "+ k);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -