/** this class is fully thread-safe */ class Flag { private boolean up; private Trigger listeners = new Trigger(); /** returns true if flag was down before */ public synchronized boolean raise() { boolean result = doRaise(); if (result) { listeners.trigger(); listeners = null; } return result; } 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 void onRaise(Runnable listener) { if (!addListener(listener)) listener.run(); } private synchronized boolean addListener(Runnable listener) { if (!up) { listeners.addListener(listener); return true; } else return false; } 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) { while (!isUp() && !otherFlag.isUp()) MultiCoreUtil.sleep(50); } }
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1000781 |
| Snippet name: | Flag class with listeners (incomplete) |
| Eternal ID of this version: | #1000781/1 |
| Text MD5: | 83018195c822c1c7aec9305d168dc9b2 |
| Author: | stefan |
| Category: | |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-08-27 01:57:30 |
| Source code size: | 1307 bytes / 61 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 838 / 776 |
| Referenced in: | [show references] |