sclass MultiSleeper extends RestartableCountdown is ISleeper { MultiSetMap entries = treeMultiSetMap(); void check { var time = nextWakeUpTime(); var action = firstValue(entries); setTargetTime(time == null ? 0 : time.sysTime(), r { Set 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); } }; } }