public class Pot { private int size; private int content; public Pot(int size) { this.size = size; this.content = 0; } private synchronized void beginCook() throws InterruptedException { while (this.content > 0) { this.wait(); } } private synchronized void endCook() { this.content = this.size; this.notifyAll(); } public void cook() throws InterruptedException { this.beginCook(); // cook this.endCook(); } public synchronized void take() { while (this.content == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.content--; this.notifyAll(); } }