sclass MultiSleeper is ISleeper, AutoCloseable { MultiSetMap entries = treeMultiSetMap(); new RestartableCountdown countdown; private void check { var time = nextWakeUpTime(); var action = firstValue(entries); countdown.setTargetTime(time == null ? 0 : time.sysTime(), r { Set toCall; synchronized(MultiSleeper.this) { toCall = entries.get(time); entries.remove(time); check(); } pcallFAll(toCall); }); } private 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); } }; } close { dispose countdown; } }