/** 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(); } }
Began life as a copy of #1000915
download show line numbers debug dex old transpilations
Travelled to 1 computer(s): mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1035584 |
| Snippet name: | Flag (old version without double-checked locking) |
| Eternal ID of this version: | #1035584/1 |
| Text MD5: | 2127596af4e0e11772430fe8ab57468b |
| Author: | stefan |
| Category: | |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-06-20 21:36:19 |
| Source code size: | 1355 bytes / 61 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 362 / 372 |
| Referenced in: | [show references] |