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

44
LINES

< > BotCompany Repo | #1033563 // MultiSleeper

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

Libraryless. Click here for Pure Java version (10643L/59K).

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 #1015557

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033563
Snippet name: MultiSleeper
Eternal ID of this version: #1033563/13
Text MD5: d66bad59222765209dc6d307d14b8006
Transpilation MD5: f530b41a8251265bf1ed041b3131b5b0
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:55:41
Source code size: 1133 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 213 / 390
Version history: 12 change(s)
Referenced in: [show references]