Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

57
LINES

< > BotCompany Repo | #1000915 - Simple static "Flag" class (thread-safe flagging, LIVE)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3851L/22K).

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() {
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  
    ret waitUntilUp(toMS(timeout));
27  
  }
28  
  
29  
  public synchronized bool waitUntilUp(long timeout) {
30  
    if (!up) {
31  
      try {
32  
        wait(timeout);
33  
      } catch (InterruptedException e) {
34  
        e.printStackTrace();
35  
      }
36  
    }
37  
    ret isUp();
38  
  }
39  
40  
  public synchronized boolean isUp() {
41  
    return up;
42  
  }
43  
  
44  
  bool get() { ret isUp(); }
45  
46  
  public String toString() {
47  
    return isUp() ? "up" : "down";
48  
  }
49  
50  
  // currently does a semi-active wait with latency = 50 ms
51  
  public void waitForThisOr(Flag otherFlag) ctex {
52  
    while (!isUp() && !otherFlag.isUp())
53  
      Thread.sleep(50);
54  
  }
55  
  
56  
  public void run() { raise(); }
57  
}

Author comment

Began life as a copy of #1000782

download  show line numbers  debug dex  old transpilations   

Travelled to 24 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, fehiwqupcyrn, gwrvuhgaqvyk, imzmzdywqqli, ishqpsrjomds, jtubtzbbkimh, lhdilzshxjzv, lpdgvwnxivlt, lulzaavyztxj, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000915
Snippet name: Simple static "Flag" class (thread-safe flagging, LIVE)
Eternal ID of this version: #1000915/9
Text MD5: 71137175e33f19fca0e3ec29482b6082
Transpilation MD5: 16d13b0a95a38fc61e3b36195007f8ca
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-24 21:57:51
Source code size: 1244 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 610 / 8783
Version history: 8 change(s)
Referenced in: [show references]