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