Customizing Thread Pools for Java parallelStream to Improve Performance and Isolation
This article explains how Java's parallelStream uses the shared ForkJoinPool, why developers may need to customize the thread pool for better performance and isolation, and provides two approaches—setting the system property and creating a dedicated ForkJoinPool—along with complete code examples and execution results.
In Java, parallelStream (introduced in Java 8) processes collection elements concurrently using the default ForkJoinPool.commonPool() , which may affect performance and lacks isolation, so a custom thread pool is often required.
1. Set system property : Adjust java.util.concurrent.ForkJoinPool.common.parallelism to change the parallelism of the shared pool.
public static void main(String[] args) throws InterruptedException {
List
list = IntStream.range(1, 50).boxed().collect(Collectors.toList());
list.parallelStream().forEach(t -> {
System.out.println(t + ":" + Thread.currentThread().getName());
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
});
TimeUnit.HOURS.sleep(1);
}The default parallelism equals the number of CPU cores (e.g., 11 on the author's machine). You can query it with:
System.out.println("ForkJoinPool parallelism " + ForkJoinPool.getCommonPoolParallelism());To increase it, set the property:
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20");
System.out.println("ForkJoinPool parallelism: " + ForkJoinPool.getCommonPoolParallelism());Note: Changing the property only adjusts the shared pool's parallelism; it still does not provide isolation.
2. Run parallel operations in a custom ForkJoinPool : Create a dedicated pool and submit the parallel stream to it.
ForkJoinPool forkJoinPool = new ForkJoinPool(20);
forkJoinPool.submit(() -> list.parallelStream().forEach(t -> {
System.out.println(t + ":" + Thread.currentThread().getName());
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
}));
TimeUnit.HOURS.sleep(1);Full example:
package com.renzhikeji.demo;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.IntStream;
public class JdkDemo {
public static void main(String[] args) throws InterruptedException {
List
list = IntStream.range(1, 50).boxed().toList();
ForkJoinPool forkJoinPool = new ForkJoinPool(20);
forkJoinPool.submit(() -> list.parallelStream().forEach(t -> {
System.out.println(t + ":" + Thread.currentThread().getName());
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
}));
TimeUnit.HOURS.sleep(1);
}
}The execution results show each element processed by a thread from the custom pool, confirming that the custom pool provides both higher parallelism and isolation.
Conclusion : Java's parallelStream may require a custom thread pool to improve performance and isolate failures; developers can either adjust the common pool's parallelism via a system property or create a dedicated ForkJoinPool and submit tasks to it.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.