| 16 | } |
| 17 | } |
| 18 | public static void main(String[] args) |
| 19 | throws InterruptedException { |
| 20 | ExecutorService exec = |
| 21 | Executors.newCachedThreadPool(); |
| 22 | List<CountingTask> tasks = |
| 23 | IntStream.range(0, 10) |
| 24 | .mapToObj(CountingTask::new) |
| 25 | .collect(Collectors.toList()); |
| 26 | List<Future<Integer>> futures = |
| 27 | exec.invokeAll(tasks); |
| 28 | Integer sum = futures.stream() |
| 29 | .map(CachedThreadPool3::extractResult) |
| 30 | .reduce(0, Integer::sum); |
| 31 | System.out.println("sum = " + sum); |
| 32 | exec.shutdown(); |
| 33 | } |
| 34 | } |
| 35 | /* Output: |
| 36 | 0 pool-1-thread-1 100 |