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

44
LINES

< > BotCompany Repo | #1000915 // Flag - boolean variable that stays true after being set true (thread-safe)

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

Transpiled version (112L) is out of date.

1  
sclass Flag is Runnable {
2  
  private volatile boolean up;
3  
4  
  /** returns true if flag was down before (i.e. flag was actually raised right now) */
5  
  public bool raise() {
6  
    if (up) false;
7  
    synchronized {
8  
      if (up) false;
9  
      up = true;
10  
      notifyAll();
11  
      true;
12  
    }
13  
  }
14  
15  
  run { raise(); }
16  
  
17  
  public void waitUntilUp() ctex {
18  
    if (up) ret;
19  
    synchronized {
20  
      while (!up) wait();
21  
    }
22  
  }
23  
24  
  public bool waitUntilUp(double timeout) {
25  
    if (timeout == infinity()) {
26  
      waitUntilUp();
27  
      ret isUp();
28  
    } else
29  
      ret waitUntilUp(toMS(timeout));
30  
  }
31  
  
32  
  public bool waitUntilUp(long timeout) ctex {
33  
    if (up) ret true;
34  
    synchronized {
35  
      if (!up)
36  
        wait(timeout);
37  
    }
38  
    ret isUp();
39  
  }
40  
41  
  public bool isUp aka get() { ret up; }
42  
43  
  toString { ret isUp() ? "up" : "down"; }
44  
}

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: Flag - boolean variable that stays true after being set true (thread-safe)
Eternal ID of this version: #1000915/16
Text MD5: 787466a4713d99bbdfad30f05787c445
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-20 21:37:04
Source code size: 872 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 784 / 9211
Version history: 15 change(s)
Referenced in: [show references]