Home > Enterprise >  How to limit number of parallel executions in ParallelStream?
How to limit number of parallel executions in ParallelStream?

Time:02-04

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?

CodePudding user response:

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.

CodePudding user response:

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

  •  Tags:  
  • Related