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

44
LINES

< > BotCompany Repo | #1036585 // MultiSleeper backup 2

JavaX fragment (include)

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

Author comment

Began life as a copy of #1033563

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036585
Snippet name: MultiSleeper backup 2
Eternal ID of this version: #1036585/1
Text MD5: d66bad59222765209dc6d307d14b8006
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 15:04:21
Source code size: 1133 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 70 / 77
Referenced in: [show references]