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

39
LINES

< > BotCompany Repo | #1036583 // MultiSleeper backup

JavaX fragment (include)

sclass MultiSleeper extends RestartableCountdown is ISleeper {
  MultiSetMap<Timestamp, Runnable> entries = treeMultiSetMap();
  
  void check {
    var time = nextWakeUpTime();
    var action = firstValue(entries);
    setTargetTime(time == null ? 0 : time.sysTime(), r {
      Set<Runnable> toCall;
      synchronized(MultiSleeper.this) {
        toCall = entries.get(time);
        entries.remove(time);
        check();
      }
      pcallFAll(toCall);
    });
  }
  
  synchronized void removeEntry(Timestamp targetTime, Runnable action) {
    entries.remove(targetTime, action);
  }
  
  // API
  
  synchronized Timestamp nextWakeUpTime() {
    ret firstKey(entries);
  }
  
  public synchronized Sleeping doLater(Timestamp targetTime, Runnable r) {
    if (r == null || targetTime == null) null;
    targetTime = max(targetTime, tsNow());
    entries.put(targetTime, r);
    check();
    ret new Sleeping(targetTime, r) {
      close {
        removeEntry(targetTime, r);
      }
    };
  }
}

Author comment

Began life as a copy of #1033563

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036583
Snippet name: MultiSleeper backup
Eternal ID of this version: #1036583/1
Text MD5: abf7ec4f3dc76e8bb2791154c2f482be
Author: stefan
Category: javax / timers
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-22 14:54:09
Source code size: 1038 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 126 / 140
Referenced in: [show references]