As a consequence:
- You have no control over the pool properties/size.
- There is no isolation between pools so long running tasks or IO locking calls may cause performance problems.
- https://dzone.com/articles/think-twice-using-java-8
- https://dzone.com/articles/whats-wrong-java-8-part-iii
The solution is executing the stream in a specific ForkJoinPool:
ForkJoinPool forkJoinPool = new ForkJoinPool(2);
forkJoinPool.submit(() ->
IntStream.range(1, 1_000_000)
.parallel()
.filter(PrimesPrint::isPrime)
.collect(toList())
).get();
References:
- https://github.com/vandekeiser/parallel-stream-fork-join-pool
- http://blog.krecan.net/2014/03/18/how-to-specify-thread-pool-for-java-8-parallel-streams/
Other solution using CompletableFuture:
ForkJoinPool forkJoinPool = new ForkJoinPool(2);
CompletableFuture<List<Integer>> primes = CompletableFuture.supplyAsync(() ->
range(1, 1_000_000)
.parallel()
.filter(PrimesPrint::isPrime)
.collect(toList()),
forkJoinPool
);
See next blog entry on CompletableFuture.
No comments:
Post a Comment