About Question enthuware.ocpjp.v7.2.1470 :
Posted: Thu Aug 04, 2016 1:43 am
Hi!
I think there are two correct answers: one described in explanation and another with opposite values of the flag in methods produce() and consume(). Here is the latter:
I think there are two correct answers: one described in explanation and another with opposite values of the flag in methods produce() and consume(). Here is the latter:
Code: Select all
public class ProdCon {
private boolean flag = false;
public synchronized void produce() {
flag = false;
notifyAll();
}
public synchronized void consume() {
while (flag) {
try {
wait();
} catch(Exception e) { e.printStackTrace(); }
}
flag = true;
}
}