TechAlpine – The Technology world

What is Fork/Join framework in java 7.0 (Parallel programming)?

Fork/Join framework has been introduced in java 7 to solve multi-threading/parallel programming issues.

It is very difficult to deal with concurrent (parallel) programming by using traditional way. Because you have to deal with thread synchronization, notification, join etc and also the pitfalls of shared resources. language-level support for concurrent programming on the Java platform is very strong as proven by the efforts in different communities. These communities all tried to provide comprehensive programming models and efficient implementations that abstracts the pain points associated with multi-threaded and distributed applications.Java Platform, Standard Edition (Java SE) 5 and then Java SE 6 introduced a set of packages (Executor service)providing powerful concurrency building blocks. Java SE 7 further enhanced the framework adding more supports for parallelism.

Fork/Join is a new concept in java 7.0 version. The basic purpose of this framework is to facilitate the multithreading environment in java applications. The finer then the executor service framework is fork/join framework. It takes the advantages of all processing power and hence the application gets more resource and processing power. It is an implementation of the ExecutorService interface that helps you take advantage of multiple processors. It is designed for works which can be broken into smaller pieces recursively and hence make the processing faster.

Similar to ExecutorService, the fork/join framework also distributes tasks to worker threads in a thread pool. But the main difference is that the fork/join framework uses a work-stealing algorithm. Here the worker threads that run out of things to do can steal tasks from other threads that are still busy. The ForkJoinPoolclass is an extension of AbstractExecutorService and the heart of the framework. ForkJoinPool implements the core work-stealing algorithm and executes ForkJoinTasks.
Now let me take an example to understand the concept in details.

The basic steps are
· Split the task
· Assign it to the available threads
· After completion join the chunks and make it complete

Let us take a list which will contain large number of integers generated at random.

This class is showing the traditional way to do the task.

import java.util.Random;
// This class defins a list which will contain large number of integers.
public class LargeInteger {
private final int[] list = new int[2000000];
public largeInteger() {
Random generator = new Random(19580427);
for (int i = 0; i < list.length; i++) {
list[i] = generator.nextInt(500000);
public int[] getList() {
return list;

Now the following SplitTask class is performing the split and join task.

import java.util.Arrays;
import jsr166y.forkjoin.RecursiveAction;
public class SplitTask extends RecursiveAction {
private int[] list;
public long result;
public SplitTask(int[] array) {
this.list = array;

protected void compute() {
if (list.length == 1) {
result = list[0];
} else {
int midpoint = list.length / 2;
int[] l1 = Arrays.copyOfRange(list, 0, midpoint);
int[] l2 = Arrays.copyOfRange(list, midpoint, list.length);
SplitTask s1 = new SplitTask(l1);
SplitTask s2 = new SplitTask(l2);
forkJoin(s1, s2);
result = s1.result + s2.result;

The last class is testing the process. It is invoking the ForkJoinExecutor pool to perform the task.

import jsr166y.forkjoin.ForkJoinExecutor;
import jsr166y.forkjoin.ForkJoinPool;
public class TestForkJoin {
public static void main(String[] args) {
LargeInteger test = new LargeInteger();
// Check the number of available processors
int nThreads = Runtime.getRuntime().availableProcessors();
SplitTask mfj = new SplitTask(test.getList());
ForkJoinExecutor pool = new ForkJoinPool(nThreads);
long result = mfj.getResult();
System.out.println("Done. Result: " + result);


Leave a Reply

Your email address will not be published. Required fields are marked *

8 × = 8

TechAlpine Books