/** this class is fully thread-safe */ static class Flag implements Runnable { private boolean up; /** returns true if flag was down before (i.e. flag was actually raised right now) */ public synchronized boolean raise() { if (!up) { up = true; notifyAll(); return true; } else return false; } public synchronized void waitUntilUp() ctex { while (!up) { //try { wait(); /*} catch (InterruptedException e) { e.printStackTrace(); }*/ } } public bool waitUntilUp(double timeout) { if (timeout == infinity()) { waitUntilUp(); ret isUp(); } else ret waitUntilUp(toMS(timeout)); } public synchronized bool waitUntilUp(long timeout) ctex { if (!up) { //try { wait(timeout); /*} catch (InterruptedException e) { e.printStackTrace(); }*/ } ret isUp(); } public synchronized boolean isUp() { return up; } bool get() { ret isUp(); } public String toString() { return isUp() ? "up" : "down"; } // currently does a semi-active wait with latency = 50 ms public void waitForThisOr(Flag otherFlag) ctex { while (!isUp() && !otherFlag.isUp()) Thread.sleep(50); } public void run() { raise(); } }