class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (!available) {
try {
wait();
} catch (InterruptedException e) { }
} // while
available = false;
System.out.println("Consumer got: " + contents);
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available) {
try {
wait();
} catch (InterruptedException e) { }
} // while
contents = value;
available = true;
System.out.println("Producer put: " + value);
notifyAll();
}
} // end CubbyHole |