public class Buffer {
int value;
boolean flag = false;
public synchronized int getValue() {
if(!flag)
{
try{
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(" get_value "+value);
flag = false;
notify();
return value;
}
public synchronized void setValue(int value) {
if(flag)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("set_value "+value);
this.value = value;
flag = true;
notify();
}
}
Thread control
最新推荐文章于 2024-03-26 17:05:00 发布