I was studying the oracle docs about the Fork/Join framework when i came across this constructor of ForkJoinPool
: ForkJoinPool(int parallelism)
. The docs said that this was the level of parallelism, which is by default equal to the number of processors available. Can anyone tell me how I can use it to increase the speed and efficiency of my program?
ForkJoinPool acts recursively, unlike Executor threads, which splits the task and submits smaller chunks to worker Threads. ForkJoinPool takes a big task, splits it into smaller tasks, and those smaller tasks split themselves again into subtasks until each subtask is atomic or not divisible. So it works recursively.
Its implementation restricts the maximum number of running threads to 32767 and attempting to create pools with greater than this size will result to IllegalArgumentException .
The getParallelism() method of ForkJoinPool class is used to get the parallelism level of the common Pool. It returns an integer value representing the parallelism level.
ForkJoinPool#commonPool() is a static thread-pool, which is lazily initialized when is actually needed. Two major concepts use the commonPool inside JDK: CompletableFuture and Parallel Streams .
Essentially, the parallelism setting tells the ForkJoinPool
how many worker threads to use.
The default setting is typically optimal, however let's say you have a worker thread separate from the ForkJoinPool
, then you might find setting the number of worker threads to number of processors - 1 is better than using all of the processors. In general, the only way to increase the speed and efficiency in a specific program is to profile with different settings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With