1 | /** this class is fully thread-safe */ |
2 | static class Flag implements Runnable {
|
3 | private boolean up; |
4 | |
5 | /** returns true if flag was down before (i.e. flag was actually raised right now) */ |
6 | public synchronized boolean raise() {
|
7 | if (!up) {
|
8 | up = true; |
9 | notifyAll(); |
10 | return true; |
11 | } else |
12 | return false; |
13 | } |
14 | |
15 | public synchronized void waitUntilUp() ctex {
|
16 | while (!up) {
|
17 | //try {
|
18 | wait(); |
19 | /*} catch (InterruptedException e) {
|
20 | e.printStackTrace(); |
21 | }*/ |
22 | } |
23 | } |
24 | |
25 | public bool waitUntilUp(double timeout) {
|
26 | if (timeout == infinity()) {
|
27 | waitUntilUp(); |
28 | ret isUp(); |
29 | } else |
30 | ret waitUntilUp(toMS(timeout)); |
31 | } |
32 | |
33 | public synchronized bool waitUntilUp(long timeout) ctex {
|
34 | if (!up) {
|
35 | //try {
|
36 | wait(timeout); |
37 | /*} catch (InterruptedException e) {
|
38 | e.printStackTrace(); |
39 | }*/ |
40 | } |
41 | ret isUp(); |
42 | } |
43 | |
44 | public synchronized boolean isUp() {
|
45 | return up; |
46 | } |
47 | |
48 | bool get() { ret isUp(); }
|
49 | |
50 | public String toString() {
|
51 | return isUp() ? "up" : "down"; |
52 | } |
53 | |
54 | // currently does a semi-active wait with latency = 50 ms |
55 | public void waitForThisOr(Flag otherFlag) ctex {
|
56 | while (!isUp() && !otherFlag.isUp()) |
57 | Thread.sleep(50); |
58 | } |
59 | |
60 | public void run() { raise(); }
|
61 | } |
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: | 363 / 373 |
| Referenced in: | [show references] |