All pastes #732138 Raw Edit

Mine

public java v1 · immutable
·published 2007-10-10 18:05 UTC
rendered paste body
import 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();		}	}	private int secondsDifference(Date before, Date after) {		return (int) (after.getTime() / 1000 - before.getTime() / 1000);	}}