public class Monitor { private boolean locked; public Monitor() { this.locked = true; } public boolean isLocked() { return this.locked; } public void unlockAndNotify() { this.locked = false; this.notify(); } public void timedWait(int timeout) throws InterruptedException { long abortTime = System.currentTimeMillis() + timeout; while (isLocked() && System.currentTimeMillis() < abortTime) wait(abortTime - System.currentTimeMillis()); } public void untimedWait() throws InterruptedException { while (isLocked()) wait(); } }