rendered paste bodyimport java.util.Date;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 { int secondsWaited = 0; Date before, after; while (isLocked()) { before = new Date(); this.wait(Math.max(0, timeout - secondsWaited)); after = new Date(); secondsWaited += 1000*secondsDifference(before, after); if (secondsWaited >= timeout) { this.unlock(); break; } } } private int secondsDifference(Date before, Date after) { return (int) (after.getTime() / 1000 - before.getTime() / 1000); }}