/** this class is fully thread-safe */ class Flag { private boolean up; /** returns true if flag was down before */ public synchronized boolean raise() { return doRaise(); } private synchronized boolean doRaise() { if (!up) { up = true; notifyAll(); return true; } else return false; } public synchronized void waitUntilUp() { if (!up) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized boolean isUp() { return up; } 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); } }
Began life as a copy of #1000781
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): cfunsshuasjs, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1000782 |
| Snippet name: | Simple "Flag" class (thread-safe flagging!) |
| Eternal ID of this version: | #1000782/1 |
| Text MD5: | d4ed5418a5fde2cc3629ea0cddca98f7 |
| Author: | stefan |
| Category: | |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | Yes |
| Created/modified: | 2015-08-27 02:00:08 |
| Source code size: | 862 bytes / 42 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 763 / 953 |
| Referenced in: | #1000776 - Kill Switch! #1000915 - Flag - boolean variable that stays true after being set true (thread-safe) #3000382 - Answer for ferdie (>> t = 1, f = 0) |