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

43
LINES

< > BotCompany Repo | #1015557 // RestartableCountdown

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

Libraryless. Click here for Pure Java version (9908L/54K).

sclass RestartableCountdown implements AutoCloseable {
  java.util.Timer timer;
  long targetTime; // in sys time

  ifndef RestartableCountdown_noStats
  long /*firings,*/ totalSleepTime; // stats
  endifndef
  
  synchronized void setTargetTime(long targetTime, Runnable action) {
    if (targetTime <= 0)
      stop();
    else if (targetTime != this.targetTime) {
      start(targetTime-sysNow(), action);
      this.targetTime = targetTime;
    }
  }
  
  // stops the countdown and restarts it
  synchronized void start(long delayMS, O action) {
    stop();
    if (delayMS <= 0)
      thread { callF(action); }
    else {
      ifndef RestartableCountdown_noStats
        totalSleepTime += delayMS;
      endifndef
      timer = doLater_daemon(delayMS, action);
      targetTime = sysNow()+delayMS;
    }
  }
  
  void start(double delaySeconds, O action) {
    start(toMS(delaySeconds), action);
  }
  
  synchronized void stop() {
    cancelTimer(timer);
    timer = null;
    targetTime = 0;
  }
  
  public void close() { stop(); }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015557
Snippet name: RestartableCountdown
Eternal ID of this version: #1015557/11
Text MD5: 33512b8ef0eb920e6611dbb568e6c0fb
Transpilation MD5: 83a633bb1316969f434c63bdbcd1d445
Author: stefan
Category: javax / timers
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-20 02:07:19
Source code size: 1086 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 595 / 1301
Version history: 10 change(s)
Referenced in: [show references]