急,急,帮忙看下这题,到底哪种正确??
[code]public class Flags2 {
private boolean isReady = false;
public synchronized void produce() {
isReady = true;
notifyAll();
}
public synchronized void consume() {
while(!isReady) {
try {
wait();
}catch(Exception ex) {}
}
isReay = true;
}
}[/code]
[code]public class Flags2 {
private boolean isReady = false;
public synchronized void produce() {
isReady = true;
notifyAll();
}
public synchronized void consume() {
while(!isReady) {
try {
wait();
}catch(Exception ex) {}
}
isReay = false;
}
}[/code]
两段代码,最后一句 isReady = true; 或者 isReady = false;都可以编译,那么到底应该是isReady = ture还是isReady = false呢??为什么?
------解决方案--------------------看你想干啥啊
------解决方案--------------------我感觉是ture,多线程方面的问题吧.
while(!isReady)就是要!isReady 为真,则isReady 为假,才进入循环,isReay = true;退出循环
------解决方案-------------------- false
producer and consumer
[code]public class Flags2 {
private boolean isReady = false;
public synchronized void produce() {
isReady = true; //生产者产生食物
notifyAll(); //通知消费者消耗食物
}
public synchronized void consume() {
while(!isReady) { //食物不存在
try {
wait(); //消费者等待食物产生
}catch(Exception ex) {}
}
isReay = false; //消费者消耗掉食物
}
}[/code