ForkJoinPool in Java
December 24, 2013
java.util.concurrent.ForkJoinPool has been introduced in Java 7 and is the part of fork join framework. ForkJoinPool is like ExecutorService. ForkJoinPool works on the basis of given target parallelism level. By default ForkJoinPool is instantiated equal to the number of available processors. ForkJoinPool maintains the enough active threads in the pool by creating, suspending or resuming the internal worker thread. ForkJoinPool provides different type of methods to check its health and to instantiate the task. We will discuss some of them. ForkJoinPool can run ForkJoinTask or other plain runnable and callable threads.
How to create Instance of ForkJoinPool in Java
First we will see how to instantiate the ForkJoinPool.ForkJoinPool pool = new ForkJoinPool();
ForkJoinPool pool = new ForkJoinPool(int parallelism);
Using ForkJoinPool.invoke() in Java
ForkJoinPool invokes the ForkJoinTask and its sub class RecursiveAction and returns the task on its completion. Find the example to run RecursiveAction by using invoke() method.package com.concretepage.util.concurrent; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; public class ForkJoinPoolDemo { public static void main(String[] args) { ForkJoinPool pool = new ForkJoinPool(); Task task = new Task(); pool.invoke(task); System.out.println(pool.getActiveThreadCount()); } } class Task extends RecursiveAction { private static final long serialVersionUID = 1L; @Override protected void compute() { System.out.println("Inside Compute method"); } }
Using ForkJoinPool.execute() in Java
execute() method in ForkJoinPool executes the ForkJoinTask or Runnable task. ForkJoinPool may execute it in calling thread or any new thread or thread from the pool. Tasks are executed asynchronously.package com.concretepage.util.concurrent; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; public class ForkJoinPoolDemo { public static void main(String[] args) { ForkJoinPool pool = new ForkJoinPool(); Task task = new Task(); pool.execute(task); } } class Task extends ForkJoinTask<String> { private static final long serialVersionUID = 1L; @Override protected boolean exec() { System.out.println("executing exec method."); return true; } @Override public String getRawResult() { return null; } @Override protected void setRawResult(String value) { } }
Using ForkJoinPool.submit() in Java
submit() method submits the task to run. The task can be ForkJoinTask or Callable.package com.concretepage.util.concurrent; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; public class ForkJoinPoolDemo { public static void main(String[] args) throws InterruptedException, ExecutionException { ForkJoinPool pool = new ForkJoinPool(); Task task = new Task(); ForkJoinTask<String> output = pool.submit(task); System.out.println(output.get()); } } class Task implements Callable<String>{ public String call() { try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(e); } return "Task Completed"; } }