Introduction
By default, ForkJoinPool creates a thread for each processor, and parallelStream will use this shared thread pool to submit tasks if not specified.
So how do we handle a specific situation where we want to use a custom ForkJoinPool?
Common operations
If we want to do an addition from 1 to 1000, we can use parallel stream like this.
Output result.
|
|
Using a custom ForkJoinPool
The above example uses a shared thread pool. Let’s look at how to use a custom thread pool to commit a parallel stream:
In the above example, we defined a ForkJoinPool with 4 threads and used it to submit this parallelStream.
Output result.
|
|
Summary
If you don’t want to use a public thread pool, you can use a custom ForkJoinPool to submit it.