Why does the execution time of java.util.Collection#parallelStream not match the expectation?
The result of cost
is about 30s, expect 10s.
private void process(int i) {
try {
Thread.sleep(10000); // 10s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testPartition() {
List<Integer> numbers = IntStream.range(1, 21).boxed().collect(Collectors.toList());
long cost = 0L;
long jobStart = System.currentTimeMillis();
numbers.parallelStream().forEach(x -> {
process(x);
});
cost += (System.currentTimeMillis() - jobStart);
System.out.println("availableProcessors = " + Runtime.getRuntime().availableProcessors());
System.out.println("cost = " + cost);
}
It seems runs process(x);
3 times. why is that?
@daniu is correct.
The ForkJoinPool in Java uses a default thread number that is equal to the number of available processors, as reported by the Runtime.getRuntime().availableProcessors() method. This design is intended to efficiently utilize the available CPU resources for parallel processing tasks.
output:
availableProcessors = 8
cost = 30015
Thanks to @daniu. parallelStream
uses the default ForkJoinPool
. The ForkJoinPool in Java uses a default thread number that is equal to the number of available processors.
availableProcessors = 8
then 20/8=3
, that's why the cost is about 30s rather than 10s.