Home  >  Java 7

Example of ForkJoinPool in Java

By Arvind Rai, December 24, 2013
java.util.concurrent.ForkJoinPool has been introduced in JDK 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();
 
By default ForkJoinPool is instantiated with available number of processors i.e Runtime.getRuntime().availableProcessors() or pass the number of parallelism as
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";
	}
}
 

ForkJoinPool.getStealCount()

Suppose there are two threads which has queue of task to perform. It is possible that one thread will steal the task from the queue of another thread. It provides the estimated steal count.

ForkJoinPool.getQueuedTaskCount()

It returns the estimated number of task which is in queue held by worker thread. This value will not be exact value, only approximate value.

ForkJoinPool.getParallelism()

It returns the number of targeted parallelism which has been provided while instantiating ForkJoinPool . If we have not provided any number the it will return the default number that is Runtime.getRuntime().availableProcessors().

ForkJoinPool.isQuiescent()

It provides a Boolean value and checks if all threads are idle. If all threads are idle, it will return true.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us