Javatpoint Logo
Javatpoint Logo

Java ForkJoinPool class

The ForkJoinPool was introduced in Java 7. It is similar to the Executor framework but with one difference. 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.

Java ForkJoinPool class declaration

List of ForkJoinPool Methods

NO Method Description
1. public boolean awaitQuiescence(long timeout, TimeUnit unit) Execute pool until the pool is quiescent. Otherwise, assist performing tasks until specified time value and unit elapses or the pool is quiescent.
2. public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException Awaits termination after a shutdown request or timeout specified in the parameter as timeout elapses until program shutdown.
3. public static ForkJoinPool commonPool() It returns an instance of common Pool.
4. public void execute(Runnable task) This executes the given task in future it may execute in the separate thread or a new pool.
5. public int getActiveThreadCount() Estimate the number of active threads.
6. public boolean getAsyncMode() Checks if the pool uses First-in-First-out scheduling for forked tasks that are never joined.
7. public static int getCommonPoolParallelism() Returns the parallelism level of the common Pool.
8. public ForkJoinPool.ForkJoinWorkerThreadFactory getFactory() Checks the factory used for constructing new workers in the pool. Also, returns the factory used.
9. public int getParallelism() Returns the parallelism level of the common Pool.
10. public int getPoolSize() Returns the Pool size. The size of the pool depends on the number of workers that have started but not yet terminated.
11. public int getQueuedSubmissionCount() Returns the number of tasks submitted to pool but not yet begun to execute.
12. public long getQueuedTaskCount() Returns estimation of the number of tasks in the working queue. This does not include tasks that have not begun to execute.
13. public int getRunningThreadCount() Returns the number of thread that is currently running and not waiting to join the queue.
14. public long getStealCount() Returns estimation of the number of tasks stolen from one thread by another after completing execution of all tasks in own queue.
15. public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() Returns the exception handler for internal worker threads that throw the exception which has an unrecoverable error and terminate due to the uncaught exception.
16. public boolean hasQueuedSubmissions() Returns true if there are any tasks submitted to this pool that have not yet begun executing otherwise false.
17. public <T> T invoke(ForkJoinTask<T> task) Performs the task specified in parameter and returns the result upon its completion.
18. public boolean isQuiescent() Returns true if all worker threads are currently idle.
19. public boolean isShutdown() Returns true if the pool calling isShutdown() has been shut down.
20. public boolean isTerminated() Returns true if all the task in the pool has been shut down.
21. public boolean isTerminating() Returns true if the process of termination has started but not yet completed.
22. public void shutdown() Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
23. public List<Runnable> shutdownNow() Attempts to cancel and/or stop all tasks, and reject all subsequently submitted tasks.
24. public ForkJoinTask<?> submit(Runnable task) Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion. And it is overloaded as follows:
25. public String toString() The toString() method returns a string identifying this pool, as well as its state, including indications of run state, parallelism level, and worker and task counts.

Java ForkJoinPool Class Example: Terminating()

Test it Now

Output:

numbers of core available in your processor:4
   Before awaitTermination (is terminating):false
  is awaitQuiescent :true
   After awaitTermination (is terminating):false
Pool.invoke :161

Java ForkJoinPool Class Example: getActiveThreadCount()

Test it Now

Output:

numbers of core available in your processor:4
 Before invoking number of active thread   :0
 After invoking Numbers of active thread   :3
Common Pool Size   :3

Java ForkJoinPool Class Example: invoke()

Test it Now

Output:

Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 0 to 2
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 2 to 5
Thread[ForkJoinPool-3-worker-1,5,main] computing: 0 to 2
Thread[ForkJoinPool-2-worker-1,5,main] computing: 0 to 2
Thread[ForkJoinPool-1-worker-1,5,main] computing: 0 to 2
Thread[ForkJoinPool-1-worker-1,5,main] computing: 2 to 5
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 5 to 7
Thread[ForkJoinPool.commonPool-worker-1,5,main] computing: 7 to 10
87






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA