Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 760
membersound Avatar asked Sep 20 '25 10:09

membersound


2 Answers

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

like image 60
szeak Avatar answered Sep 22 '25 00:09

szeak


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.

like image 36
Nikolas Charalambidis Avatar answered Sep 22 '25 01:09

Nikolas Charalambidis