doprotected.java
来自「java手机程序开发随书光盘源代码」· Java 代码 · 共 75 行
JAVA
75 行
import javax.microedition.midlet.*;
public class DoProtected extends MIDlet{
int data = 0;
SetThread setThread;
GetThread getThread;
Monitor monitor;
boolean settable = false;
public DoProtected(){
setThread = new SetThread();
getThread = new GetThread();
monitor = new Monitor();
}
public void startApp(){
getThread.start();
setThread.start();
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
class SetThread extends Thread{
public void run(){
while(data != 10){
monitor.set();
}
System.out.println("SetThread Exit!");
}
}
class GetThread extends Thread{
public void run(){
while(data != 10){
for(int i=0;i<1000;i++){}
monitor.get();
}
System.out.println("GetThread Exit!");
}
}
class Monitor{
public synchronized void set(){
while(!settable){
try{
wait();
}
catch(Exception ex){}
}
data++;
System.out.println("Set data = "+data);
settable = false;
notify();
}
public synchronized void get(){
while(settable){
try{
wait();
}
catch(Exception ex){}
}
System.out.println("Get data = "+data);
settable = true;
notify();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?