Junior — Middle
Does each Parallel Stream use a separate thread pool or a common one for all?
sobes.tech AI
Answer from AI
All parallel streams in Java by default use a common thread pool — ForkJoinPool.commonPool(). This means that multiple parallel streams will share the same thread pool.
If you need to use a separate pool for a specific parallel stream, you can create your own ForkJoinPool and execute the stream within its context:
ForkJoinPool customThreadPool = new ForkJoinPool(4);
customThreadPool.submit(() -> {
list.parallelStream()
.forEach(...);
}).get();
Thus, the default pool is shared, but if necessary, you can specify a separate one.