1 | sclass ConcurrentEvaluator {
|
2 | new L functions; |
3 | L results = synchroList(); |
4 | ExecutorService executorService; |
5 | O onNewResult; // Runnable or voidfunc(int) |
6 | volatile bool cancelled; |
7 | L<Thread> threads = synchroList(); |
8 | int coresToUse; // if 0, use coresToUse() |
9 | |
10 | void add(O f) { functions.add(f); }
|
11 | void addAll(Collection l) { functions.addAll(l); }
|
12 | |
13 | void start() {
|
14 | executorService = Executors.newFixedThreadPool(coresToUse != 0 ? coresToUse : coresToUse()); |
15 | while (l(results) < l(functions)) results.add(null); |
16 | for _i over functions: {
|
17 | final int i = _i; |
18 | executorService.execute(r {
|
19 | threads.add(currentThread()); |
20 | try {
|
21 | results.set(i, pcallF(functions.get(i))); |
22 | ping(); |
23 | onNewResult(i); |
24 | } finally {
|
25 | threads.remove(currentThread()); |
26 | } |
27 | }); |
28 | } |
29 | } |
30 | |
31 | void onNewResult(int i) { if (!cancelled) pcallF(onNewResult, i); }
|
32 | |
33 | L results() { ret cloneList(results); }
|
34 | |
35 | void cancel {
|
36 | pcall {
|
37 | cancelled = true; |
38 | executorService.shutdownNow(); |
39 | cancelThreads(cloneList(threads)); |
40 | } |
41 | } |
42 | |
43 | void cancelAndWait {
|
44 | long time = sysNow(); |
45 | cancel(); |
46 | while (nempty(threads)) sleep(1); |
47 | sysDone("Cancelling evaluators", time, 10);
|
48 | } |
49 | |
50 | bool done() { ret empty(threads); }
|
51 | |
52 | void startWaitCleanUp {
|
53 | try {
|
54 | start(); |
55 | while (!done()) sleepSeconds(1); |
56 | } finally {
|
57 | cancel(); |
58 | } |
59 | } |
60 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1008561 |
| Snippet name: | ConcurrentEvaluator |
| Eternal ID of this version: | #1008561/11 |
| Text MD5: | 2925adf721cf2ae3d5352937822b00f7 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2018-05-01 19:48:38 |
| Source code size: | 1515 bytes / 60 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 759 / 1387 |
| Version history: | 10 change(s) |
| Referenced in: | [show references] |