📄 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 + -