1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.IntStream;
public class Main {
public static void main(String args[]) throws Exception {
final ExecutorService threadPool = Executors.newCachedThreadPool();
final List<Future<Integer>> futures = new ArrayList<>();
final int[] data = IntStream.range(0, 10000).toArray();
int sum = 0;
for (int i = 0, step = 1000; i < data.length; i += step) {
final IntStream chunk = Arrays.stream(data, i, i + step);
futures.add(threadPool.submit(chunk::sum));
}
for (final Future<Integer> f : futures) {
sum += f.get();
}
System.out.println("Parallel Sum: " + sum);
threadPool.shutdown();
}
}
Enter to Rename, Shift+Enter to Preview