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

39
LINES

< > BotCompany Repo | #1036583 // MultiSleeper backup

JavaX fragment (include)

1  
sclass MultiSleeper extends RestartableCountdown is ISleeper {
2  
  MultiSetMap<Timestamp, Runnable> entries = treeMultiSetMap();
3  
  
4  
  void check {
5  
    var time = nextWakeUpTime();
6  
    var action = firstValue(entries);
7  
    setTargetTime(time == null ? 0 : time.sysTime(), r {
8  
      Set<Runnable> toCall;
9  
      synchronized(MultiSleeper.this) {
10  
        toCall = entries.get(time);
11  
        entries.remove(time);
12  
        check();
13  
      }
14  
      pcallFAll(toCall);
15  
    });
16  
  }
17  
  
18  
  synchronized void removeEntry(Timestamp targetTime, Runnable action) {
19  
    entries.remove(targetTime, action);
20  
  }
21  
  
22  
  // API
23  
  
24  
  synchronized Timestamp nextWakeUpTime() {
25  
    ret firstKey(entries);
26  
  }
27  
  
28  
  public synchronized Sleeping doLater(Timestamp targetTime, Runnable r) {
29  
    if (r == null || targetTime == null) null;
30  
    targetTime = max(targetTime, tsNow());
31  
    entries.put(targetTime, r);
32  
    check();
33  
    ret new Sleeping(targetTime, r) {
34  
      close {
35  
        removeEntry(targetTime, r);
36  
      }
37  
    };
38  
  }
39  
}

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: 128 / 142
Referenced in: [show references]