Java · Java8

ForkJoin Demonstration in JAVA

Fork/Join Framework which is introduced in java7 is an important framework to achieve parallel processing in java.This is very useful if you have to execute recursive operations.

There are different kind of tasks which can be executed as part of fork join framework.

  1. RecursiveTask: A task which returns something after the execution
  2. RecursiveAction: A task which will not return anything
  3. CountedCompleter: A task can trigger other tasks on completion of some set of tasks.

RecursiveTask

The following program demonstrates the fork join concept with RecursiveTask. And attached sequence diagram which is best viewed in MS Paint gives the execution mechanism of this framework.

Aim of the program is to find even numbers from a given list.

Execution Environment: JDK8 and Windows 7

A main program to initialize pool and start the task.

/**
 * 
 */
package forkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * @author prabhu kvn
 *
 */
public class EvenNumberFinderMain {

 public static void main(String args[]) {

 // Mock Data: crate an array of 100 numbers
 List a = new ArrayList();
 List evenNumbers = new ArrayList();
 for (int i = 0; i < 100; i++) {
 a.add(i);
 }
 // Initialize the Thread Pool
 ForkJoinPool pool = new ForkJoinPool(12);
 EvenNumberFinderTask task = new EvenNumberFinderTask(a);
 pool.execute(task);

 do {
 System.out.println("****************Pool****************");
 System.out.println("Parallesim:" + pool.getParallelism());
 System.out.println("Pool size:" + pool.getPoolSize());
 System.out.println("Active Thread count:" + pool.getActiveThreadCount());
 System.out.println("Queed Submission count:" + pool.getQueuedSubmissionCount());
 System.out.println("Qued Task count:" + pool.getQueuedTaskCount());
 System.out.println("Running Thread count:" + pool.getRunningThreadCount());
 System.out.println("Seal count:" + pool.getStealCount());

 } while (!task.isDone());
 pool.shutdown();
 // wait for 
 evenNumbers = task.join();
 System.out.println("Result:" + evenNumbers);

 }
}

Actual Task program

/**
*
*/
package forkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;
import java.util.stream.Collectors;

public class EvenNumberFinderTask extends RecursiveTask{

List a=null;

public EvenNumberFinderTask() {
}

public EvenNumberFinderTask(List a) {
this.a=a;
}
@Override
protected List compute() {
// TODO Auto-generated method stub
List taskList = new ArrayList();
List subList=null;
List evenList = new ArrayList();
/*
* See if the list is greater than 10. if so divide the task.
*/
if(a.size()>10){
subList = a.subList(0, 10);
List remaining = a.subList(10, a.size());
EvenNumberFinderTask task = new EvenNumberFinderTask(remaining);
task.fork();
taskList.add(task);
}else{
subList=a;
}

if(subList!=null){
evenList=subList.parallelStream().filter(element -> {return element%2==0;}).collect(Collectors.toList());
}
System.out.println(“Sub Result:”+evenList);

// wait for all the threads to join
for(EvenNumberFinderTask t: taskList){
evenList.addAll(t.join());
}

return evenList;
}
}

And check the out put of the program which is very interesting. And also sequence diagram

forkjoinseg1

Advertisements

One thought on “ForkJoin Demonstration in JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s