| 45 | } |
| 46 | |
| 47 | public void execute(Consumer<T> consumer, Callback<Double> progress, int progressInterval) { |
| 48 | ChronoLatch cl = new ChronoLatch(progressInterval); |
| 49 | Contained<Integer> consumed = new Contained<Integer>(0); |
| 50 | executor = Executors.newFixedThreadPool(threads); |
| 51 | int length = q.size(); |
| 52 | int remaining = length; |
| 53 | |
| 54 | while (remaining > 0) { |
| 55 | int at = remaining; |
| 56 | remaining -= (remaining > workload ? workload : remaining); |
| 57 | int to = remaining; |
| 58 | |
| 59 | executor.submit(() -> |
| 60 | { |
| 61 | J.dofor(at, (i) -> i >= to, -1, (i) -> J.attempt(() -> consumer.accept(q.get(i)))); |
| 62 | consumed.mod((c) -> c += workload); |
| 63 | J.doif(() -> progress != null && cl.flip(), () -> progress.run((double) consumed.get() / (double) length)); |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | executor.shutdown(); |
| 68 | J.attempt(() -> executor.awaitTermination(100, TimeUnit.HOURS)); |
| 69 | } |
| 70 | } |