Transpiled version (7288L) is out of date.
1 | static class Q implements AutoCloseable { |
2 | S name = "Unnamed Queue"; |
3 | L<Runnable> q = synchroLinkedList(); |
4 | ReliableSingleThread rst = new(r { _run() }); |
5 | gettable volatile bool retired; |
6 | gettable volatile Runnable currentJob; |
7 | new AtomicLong jobsDone; |
8 | |
9 | *() {} |
10 | *(S *name) {} |
11 | |
12 | void add(Runnable r) { |
13 | assertNotRetired(); |
14 | q.add(r); |
15 | _trigger(); |
16 | } |
17 | |
18 | void addInFront(Runnable r) { |
19 | assertNotRetired(); |
20 | q.add(0, r); |
21 | _trigger(); |
22 | } |
23 | |
24 | void _trigger() { |
25 | rst.name = name; |
26 | rst.go(); |
27 | } |
28 | |
29 | void add(O r) { |
30 | add(toRunnable(r)); |
31 | } |
32 | |
33 | void _run() { |
34 | Runnable r; |
35 | while (licensed() && !retired && (r = syncPopFirst(q)) != null) { |
36 | currentJob = r; |
37 | inc(jobsDone); |
38 | pcall { r.run(); } |
39 | currentJob = null; |
40 | } |
41 | onIdle(); |
42 | } |
43 | |
44 | public void close() { retired = true; } // TODO: interrupt thread |
45 | void done() {} // legacy function |
46 | |
47 | bool isEmpty() { ret q.isEmpty(); } |
48 | int size() { ret q.size(); } |
49 | |
50 | O mutex() { ret q; } // clients can synchronize on this |
51 | L<Runnable> snapshot() { ret cloneList(q); } |
52 | |
53 | // override this |
54 | void onIdle {} |
55 | |
56 | bool busy() { ret currentJob != null; } |
57 | |
58 | void assertNotRetired() { |
59 | assertFalse("Queue is retired", retired()); |
60 | } |
61 | |
62 | bool hasThread() { ret rst.hasThread(); } |
63 | |
64 | long nJobsDone() { ret jobsDone!; } |
65 | |
66 | toString { |
67 | ret (retired ? "Retired " : "") + "Q " + systemHashCodeHex(this) |
68 | + " (" + (isEmpty() ? "empty" : nEntries(size()) + ", current job: " + currentJob) + ")"; |
69 | } |
70 | } |
download show line numbers debug dex old transpilations
Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, omdjrrnzbjjv, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1000934 |
Snippet name: | class Q (LIVE, runnable queue, new version without idle thread) |
Eternal ID of this version: | #1000934/35 |
Text MD5: | 537eb6662c1d02cccbd0ca86c61d6862 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-03 16:32:33 |
Source code size: | 1599 bytes / 70 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 934 / 2866 |
Version history: | 34 change(s) |
Referenced in: | [show references] |