Libraryless. Click here for Pure Java version (9908L/54K).
1 | sclass RestartableCountdown implements AutoCloseable { |
2 | java.util.Timer timer; |
3 | long targetTime; // in sys time |
4 | |
5 | ifndef RestartableCountdown_noStats |
6 | long /*firings,*/ totalSleepTime; // stats |
7 | endifndef |
8 | |
9 | synchronized void setTargetTime(long targetTime, Runnable action) { |
10 | if (targetTime <= 0) |
11 | stop(); |
12 | else if (targetTime != this.targetTime) { |
13 | start(targetTime-sysNow(), action); |
14 | this.targetTime = targetTime; |
15 | } |
16 | } |
17 | |
18 | // stops the countdown and restarts it |
19 | synchronized void start(long delayMS, O action) { |
20 | stop(); |
21 | if (delayMS <= 0) |
22 | thread { callF(action); } |
23 | else { |
24 | ifndef RestartableCountdown_noStats |
25 | totalSleepTime += delayMS; |
26 | endifndef |
27 | timer = doLater_daemon(delayMS, action); |
28 | targetTime = sysNow()+delayMS; |
29 | } |
30 | } |
31 | |
32 | void start(double delaySeconds, O action) { |
33 | start(toMS(delaySeconds), action); |
34 | } |
35 | |
36 | synchronized void stop() { |
37 | cancelTimer(timer); |
38 | timer = null; |
39 | targetTime = 0; |
40 | } |
41 | |
42 | public void close() { stop(); } |
43 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1015557 |
Snippet name: | RestartableCountdown |
Eternal ID of this version: | #1015557/11 |
Text MD5: | 33512b8ef0eb920e6611dbb568e6c0fb |
Transpilation MD5: | 83a633bb1316969f434c63bdbcd1d445 |
Author: | stefan |
Category: | javax / timers |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-20 02:07:19 |
Source code size: | 1086 bytes / 43 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 621 / 1336 |
Version history: | 10 change(s) |
Referenced in: | [show references] |