There actually is a trick how to execute a parallel operation in a specific fork-join pool. If you execute it as a task in a fork-join pool, it stays there and does not use the common one.

final int parallelism = 4;
ForkJoinPool forkJoinPool = null;
try {
    forkJoinPool = new ForkJoinPool(parallelism);
    final List<Integer> primes = forkJoinPool.submit(() ->
        // Parallel task here, for example
        IntStream.range(1, 1_000_000).parallel()
                .filter(PrimesPrint::isPrime)
                .boxed().collect(Collectors.toList())
    ).get();
    System.out.println(primes);
} catch (InterruptedException | ExecutionException e) {
    throw new RuntimeException(e);
} finally {
    if (forkJoinPool != null) {
        forkJoinPool.shutdown();
    }
}

The trick is based on ForkJoinTask.fork which specifies: "Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the ForkJoinPool.commonPool() if not inForkJoinPool()"

Answer from Lukáš Křečan on Stack Overflow
Top answer
1 of 16
520

There actually is a trick how to execute a parallel operation in a specific fork-join pool. If you execute it as a task in a fork-join pool, it stays there and does not use the common one.

final int parallelism = 4;
ForkJoinPool forkJoinPool = null;
try {
    forkJoinPool = new ForkJoinPool(parallelism);
    final List<Integer> primes = forkJoinPool.submit(() ->
        // Parallel task here, for example
        IntStream.range(1, 1_000_000).parallel()
                .filter(PrimesPrint::isPrime)
                .boxed().collect(Collectors.toList())
    ).get();
    System.out.println(primes);
} catch (InterruptedException | ExecutionException e) {
    throw new RuntimeException(e);
} finally {
    if (forkJoinPool != null) {
        forkJoinPool.shutdown();
    }
}

The trick is based on ForkJoinTask.fork which specifies: "Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the ForkJoinPool.commonPool() if not inForkJoinPool()"

2 of 16
251

The parallel streams use the default ForkJoinPool.commonPool which by default has one less threads as you have processors, as returned by Runtime.getRuntime().availableProcessors() (This means that parallel streams leave one processor for the calling thread).

For applications that require separate or custom pools, a ForkJoinPool may be constructed with a given target parallelism level; by default, equal to the number of available processors.

This also means if you have nested parallel streams or multiple parallel streams started concurrently, they will all share the same pool. Advantage: you will never use more than the default (number of available processors). Disadvantage: you may not get "all the processors" assigned to each parallel stream you initiate (if you happen to have more than one). (Apparently you can use a ManagedBlocker to circumvent that.)

To change the way parallel streams are executed, you can either

  • submit the parallel stream execution to your own ForkJoinPool: yourFJP.submit(() -> stream.parallel().forEach(soSomething)).get(); or
  • you can change the size of the common pool using system properties: System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20") for a target parallelism of 20 threads.

Example of the latter on my machine which has 8 processors. If I run the following program:

long start = System.currentTimeMillis();
IntStream s = IntStream.range(0, 20);
//System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20");
s.parallel().forEach(i -> {
    try { Thread.sleep(100); } catch (Exception ignore) {}
    System.out.print((System.currentTimeMillis() - start) + " ");
});

The output is:

215 216 216 216 216 216 216 216 315 316 316 316 316 316 316 316 415 416 416 416

So you can see that the parallel stream processes 8 items at a time, i.e. it uses 8 threads. However, if I uncomment the commented line, the output is:

215 215 215 215 215 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216

This time, the parallel stream has used 20 threads and all 20 elements in the stream have been processed concurrently.

🌐
Baeldung
baeldung.com › home › java › java concurrency › custom thread pools in java parallel streams
Custom Thread Pools in Java Parallel Streams | Baeldung
December 28, 2023 - In this quick tutorial, we’ll look at one of the biggest limitations of Stream API and see how to make a parallel stream work with a custom ThreadPool instance, alternatively – there’s a library that handles this. Let’s start with a simple example – calling the parallelStream method on any of the Collection types – which will return a possibly parallel Stream:
Discussions

Is it possible to use virtual threads with parallel streams?
Parallel streams don't have to use the global fork join pool. If you use them in the context of your own fork join pool, that's the one they will use More on reddit.com
🌐 r/java
20
37
March 22, 2023
Alternating between Java streams and parallel streams at runtime - Software Engineering Stack Exchange
I don't understand why this approach for the parallel stream should run in parallel inside the pool instead of inside the global pool? Thorbjørn Ravn Andersen – Thorbjørn Ravn Andersen · 2021-02-12 18:58:00 +00:00 Commented Feb 12, 2021 at 18:58 · @ThorbjørnRavnAndersen Can you clarify what you mean by 'global thread pool'? This doesn't seem to be a standard term with regard to Java... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
multithreading - How many threads are spawned in parallelStream in Java 8? - Stack Overflow
The parallel.streams API uses the CountedCompleter Class which has some interesting problems. Since the F/J framework does not use a separate object to hold results, long chains may result in an OOME. Also those long chains can sometimes cause a Stack Overflow. The answer to those problems is the use of the Paraquential technique as I pointed out in this article. The other problem is excessive thread ... More on stackoverflow.com
🌐 stackoverflow.com
java - Why does the parallel stream not use all the threads of the ForkJoinPool? - Stack Overflow
So I know that if you use the parallelStream without a custom ForkJoinPool it will use the default ForkJoinPool which by default has one less threads as you have processors. So, as stated here (an... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DEV Community
dev.to › igalhaddad › java-8-parallel-stream-with-threadpool-32kd
Java 8 Parallel Stream with ThreadPool - DEV Community
February 10, 2020 - When executing a parallel stream, it runs in the Common Fork Join Pool (ForkJoinPool.commonPool()), s... Tagged with java, tutorial, multithreading, threadpool.
🌐
Geeky Hacker
geekyhacker.com › home › java › control threads number in java parallel stream
Control threads number in Java parallel stream - Geeky Hacker
March 30, 2024 - There are two ways to control threads number in Java parallel stream. One using configuration and another by using a custom fork-join pool.
🌐
Glassthought
glassthought.com › notes › lc2wo9z3twpqypqncncbgzd
Parallel Streams with Custom Thread Pool - GlassThought.com
December 21, 2023 - import java.util.List; import java.util.concurrent.ForkJoinPool; import java.util.stream.Collectors; public class ParallelStreamExample { public static void main(String[] args) { List<Integer> data = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); int numberOfThreads = 4; // Specify the number of threads ForkJoinPool customThreadPool = new ForkJoinPool(numberOfThreads); try { // Submit the parallel stream execution to the custom thread pool List<Integer> result = customThreadPool.submit( () -> data.parallelStream() .map(ParallelStreamExample::process) .collect(Collectors.toList()) ).join(); // This will wait for all tasks to complete System.out.println("Processed data: " + result); } finally { customThreadPool.shutdown(); // Always shutdown the thread pool } } private static int process(int input) { // Example processing (this could be any operation) return input * input; } }
🌐
GitHub
github.com › ferstl › parallel-stream-support
GitHub - ferstl/parallel-stream-support: Parallel streams in Java with a custom ForkJoinPool · GitHub
Q: Does this library also support sequential streams? A: Yes. Just call sequential() on the stream and it will be processed within the calling thread. When created, all streams of this library are configured to be parallel.
Starred by 31 users
Forked by 3 users
Languages   Java
🌐
Medium
medium.com › @michaelbespalov › parallel-stream-pitfalls-and-how-to-avoid-them-91f11808a16c
Parallel stream pitfalls and how to avoid them | by Michael Bespalov | Medium
December 20, 2017 - If we use another parallel stream ... provide a custom thread pool to the parallel stream, but there is a way by wrapping it in custom ForkJoinPool....
Find elsewhere
🌐
Javaspecialists
javaspecialists.eu › archive › Issue311-Virtual-Threads-and-Parallel-Streams.html
[JavaSpecialists 311] - Virtual Threads and Parallel Streams
August 31, 2023 - If I run it with -XX:ActiveProcessorCount=1 or -XX:ActiveProcessorCount=2, or with the common pool parallelism set to 0, then we will see output such as: VirtualThread[#14]/runnable@ForkJoinPool-1-worker-1 VirtualThread[#14]/runnable@ForkJoinPool-1-worker-1 maybe not unmounted time = 14223ms · I hope this helps a bit in case you need to work with large data sets using parallel streams and you want to be sure that your code will also work if being called from a virtual thread. ... P.S. Our Mastering Virtual Threads in Java Course is now available as an in-house course for 10 or more programmers, presented either virtually or in-person.
Top answer
1 of 3
5

You can define a custom thread pool by implementing the (Executor) interface that increases or decreases the number of threads in the pool as needed. You can submit your parallelStream chain to it as shown here using a ForkJoinPool:

I've created a working example which prints the threads that are doing the work:

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class TestParallel
{
  public static void main(String... args) throws InterruptedException, ExecutionException
  {
    testParallel();
  }
  
  
  static Long sum(long a, long b)
  {
    System.out.println(Thread.currentThread() + " - sum: " + a + " " + b);
    return a + b;
  }
  
  public static void testParallel() 
      throws InterruptedException, ExecutionException {
        
        long firstNum = 1;
        long lastNum = 10;

        List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed()
          .collect(Collectors.toList());

        System.out.println("custom: ");
        System.out.println();
        
        ForkJoinPool customThreadPool = new ForkJoinPool(4);
        long totalCustom = customThreadPool.submit(
          () -> aList.parallelStream().reduce(0L, TestParallel::sum)).get();
        
        System.out.println();
        System.out.println("standard: ");
        System.out.println();
        
        long totalStandard = aList.parallelStream().reduce(0L, TestParallel::sum);
        
        System.out.println();
        System.out.println(totalCustom + " " + totalStandard);
    }
}

Personally, if you want to get to that level of control, I'm not sure the streaming API is worth bothering with. It's not doing anything you can't do with Executors and concurrent libs. It's just a simplified facade to those features with limited capabilities.

Streams are kind of nice when you need to lay out a simple multi-step process in a little bit of code. But if all you are doing is using them to manage parallelism of tasks, the Executors and ExecutorService are more straightforward IMO. One thing I would avoid is pushing the number of threads above your machine's native thread count unless you have IO-bound processing. And if that's the case NIO is the more efficient solution.

What I'm not sure about is what the logic is that decides when to use multiple threads and when to use one. You'd have to better explain what factors come into play.

2 of 3
2

I don't know if this is useful but there is a design pattern called Bridge that decouples the abstraction from its implementation so you can, at runtime change between implementations.

A simple example would be a stack. For stacks where the total amount of data stored at one time is relatively small, it is more efficient to use an array. When the amount of data hits a certain point, it becomes better to use a linked-list. The stack implementation determines when it switches from one to the other.

For your case, it sounds like the processing would be behind some interface and based on the volume (do you know it before you start the processing?) your Processor class could use streams or parallel streams as appropriate.

🌐
Medium
medium.com › @daniel.las › performance-of-parallel-java-streams-68988191d9f8
Performance of parallel Java Streams | by Daniel Las | Medium
April 21, 2024 - Benchmark results are presented as charts showing number of operations per second of sequential and parallel streams running in 2, 4, 6, 8, 10,12, 14 and 16 threads. The number of operations is the sum of operations executed in every running thread within one second. If you wonder if this setup reflects the reality of web applications … this is how modern Non-Blocking I/O based web frameworks work: there is some thread responsible for I/O and passing tasks to multiple worker threads waiting in the pool.
Top answer
1 of 2
144

The Oracle's implementation[1] of parallel stream uses the current thread and in addition to that, if needed, also the threads that compose the default fork join pool ForkJoinPool.commonPool(), which has a default size equal to one less than the number of cores of your CPU.

That default size of the common pool can be changed with this property:

-Djava.util.concurrent.ForkJoinPool.common.parallelism=8

Alternatively, you can use your own pool:

ForkJoinPool myPool = new ForkJoinPool(8);
myPool.submit(() ->
    list.parallelStream().forEach(/* Do Something */);
).get();

Regarding the order, jobs will be executed as soon as a thread is available, in no specific order.

As correctly pointed out by @Holger this is an implementation specific detail (with just one vague reference at the bottom of a document), both approaches will work on Oracle's JVM but are definitely not guaranteed to work on JVMs from other vendors, the property could not exist in a non-Oracle implementation and Streams could not even use a ForkJoinPool internally rendering the alternative based on the behavior of ForkJoinTask.fork completely useless (see here for details on this).

2 of 2
5

While @uraimo is correct, the answer depends on exactly what "Do Something" does. The parallel.streams API uses the CountedCompleter Class which has some interesting problems. Since the F/J framework does not use a separate object to hold results, long chains may result in an OOME. Also those long chains can sometimes cause a Stack Overflow. The answer to those problems is the use of the Paraquential technique as I pointed out in this article.

The other problem is excessive thread creation when using nested parallel forEach.

Top answer
1 of 2
11

Update

Originally this answer was an elaborate explanation claiming that the ForkJoinPool applies back-pressure and doesn't even reach the prescribed parallelism level, because there are always idle workers available to process the stream.

That's incorrect.

The actual answer is provided in the original question to which this was marked as duplicate - using a custom ForkJoinPool for stream processing is not officially supported, and when using forEach, the default pool parallelism is used to determine the stream spliterator behavior.

Here's an example how when tasks are manually submitted to a custom ForkJoinPool, the pool's active thread count easily reaches its parallelism level:

for (int i = 0; i < 1_000_000; ++i) {
   forkJoinPool.submit(() -> {
      try {
         Thread.sleep(1);
         thNames.add(Thread.currentThread().getName());
         System.out.println("Size: " + thNames.size() + " activeCount: " + forkJoinPool.getActiveThreadCount() + " parallelism: " + forkJoinPool.getParallelism());
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   });
}

Thanks to Stuart Marks for pointing this out and to Sotirios Delimanolis for arguing that my original answer is wrong :)

2 of 2
-5

It seems to me that when you submit a lambda to the FJP that lambda will use the common pool and not the FJP. Sotirios Delimanolis proved this with his comment, above. What you are submitting is a Task that runs in your FJP.

Try profiling this code to see what threads are actually being used.

You cannot name the threads within the FJP.

🌐
Codementor
codementor.io › community › controlling parallelism of java 8 collection streams
Controlling Parallelism of Java 8 Collection Streams | Codementor
January 23, 2017 - When we're using collection streams in parallel of Java, there doesn't seem to be any parameter that takes our own thread pool. Some may wonder how many threads certain operation would be given while others may actually believe that we can leave JVM to it because it would know what to do.
🌐
Medium
medium.com › geekculture › pitfalls-of-java-parallel-streams-731fe0c1eb5f
Internals Of Java Parallel Streams | by Thameena S | Geek Culture | Medium
July 1, 2021 - Fork step splits the task into smaller subtasks and these tasks are executed concurrently, by different threads. After all the subtasks are executed, the Join step will combine all the results into one result. These steps adds a lot more overhead to parallel execution compared to sequential. The fork/join framework uses a pool of threads managed by the ForkJoinPool.
🌐
Java2Blog
java2blog.com › home › core java › java 8 › java parallel stream
Java Parallel Stream - Java2Blog
January 26, 2021 - In case, you are using multiple parallel streams, then they will share same ForkJoinPool.commonPool .This means you may not be able to use all the processors assigned to each parallel stream. To solve this issue, you can create own thread pool while processing the stream.
🌐
Javaprogramto
javaprogramto.com › 2020 › 05 › java-8-parallel-streams-custom-threadpool.html
Java 8 Parallel Streams - Custom Thread Pools Examples JavaProgramTo.com
May 2, 2021 - If you have only one parallel stream then you can use it with a limited pool count. But, Wait for a java update that parallel stream can take ForkJoinPool as input to limit the number of parallel processes. In this article, You've seen how to create parallel streams in java stream api and parallel stream api uses a common share thread pool from ForkJoinPool.
🌐
TheServerSide
theserverside.com › tip › How-to-use-parallel-streams-in-Java-with-virtual-threads
How to use parallel streams in Java with virtual threads | TheServerSide
July 3, 2024 - Parallel streams utilize the fork/join pool which defaults to the number of available cores. This can help improve performance, but it's limited by the pool size and the fact that this is I/O-bound work.
🌐
DZone
dzone.com › coding › java › think twice before using java 8 parallel streams
Think Twice Before Using Java 8 Parallel Streams
August 13, 2019 - Imagine a servlet environment, ... though each of them requires different resources. What's worse, you can not specify thread pool for parallel streams; the whole class loader has to use the same one....
🌐
Xperti
xperti.io › home › when to use the parallel stream in java
When To Use The Parallel Stream In Java
February 4, 2026 - A Java Parallel stream uses the fork-join framework and its common pool of worker threads to perform parallel executions. This framework was introduced in java.util.concurrent in Java 7 for task management between multiple threads.