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.

sclass Flag is Runnable {
  private volatile boolean up;

  /** returns true if flag was down before (i.e. flag was actually raised right now) */
  public bool raise() {
    if (up) false;
    synchronized {
      if (up) false;
      up = true;
      notifyAll();
      true;
    }
  }

  run { raise(); }
  
  public void waitUntilUp() ctex {
    if (up) ret;
    synchronized {
      while (!up) wait();
    }
  }

  public bool waitUntilUp(double timeout) {
    if (timeout == infinity()) {
      waitUntilUp();
      ret isUp();
    } else
      ret waitUntilUp(toMS(timeout));
  }
  
  public bool waitUntilUp(long timeout) ctex {
    if (up) ret true;
    synchronized {
      if (!up)
        wait(timeout);
    }
    ret isUp();
  }

  public bool isUp aka get() { ret up; }

  toString { ret isUp() ? "up" : "down"; }
}

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: 889 / 9339
Version history: 15 change(s)
Referenced in: [show references]