'How to limit number of parallel executions in ParallelStream?

list.parallelStream().forEach(element -> ...);

How can I limit the number of parallel threads nowadays?

There was a "hack" in the past to set a System property java.util.concurrent.ForkJoinPool.common.parallelism. But that feels wrong, plus it does not work anymore.

Could you advise how to chunk the list into 4 divisions, and then only run those 4 devisions in parallel?



Solution 1:[1]

I believe you rather need to limit the number of concurrent tasks being executed, therefore I don't find a necessity of using a parallel stream here as long as there is an easy solution located in the Java concurrent package. Use ExecutorService with a fixed thread pool of four instead.

Collection<Callable<Void>> = ...
ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.invokeAll(callables);

If you really wish to use a custom thread pool within the parallel streams, please, refer to this question: Custom thread pool in Java 8 parallel stream.

Solution 2:[2]

Use custom thread pool.

Read more here: https://www.baeldung.com/java-8-parallel-streams-custom-threadpool

and here: https://www.baeldung.com/java-when-to-use-parallel-stream

Solution 3:[3]

You have to use custom ForkJoinPool with the number of needed threads to execute your task in parallel.

ForkJoinPool customThreadPool = new ForkJoinPool(NB_THREADS);
customThreadPool.submit(
() -> list.parallelStream().forEach(element -> ...);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikolas Charalambidis
Solution 2 szeak
Solution 3 Nouredine LAMAMRA