Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is level of parallelism in the Java ForkJoinPool?

Tags:

java

fork-join

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?

like image 340
JavaNewbie_M107 Avatar asked Jan 13 '13 08:01

JavaNewbie_M107


People also ask

How does ForkJoinPool work in Java?

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.

How many threads does ForkJoinPool use?

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 .

How can we get the target parallelism level in Java?

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.

What is ForkJoinPool commonPool ()?

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 .


1 Answers

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.

like image 52
Alex DiCarlo Avatar answered Oct 06 '22 01:10

Alex DiCarlo