Fork Join Java Multithreading

Let’s study very interesting topic Fork Join Java Multithreading to increases the performance in java programs.

Introduction

Objective: Fork and join in java Multithreading mainly designed to simplify parallel programming for Java programmers.

Firstly will understand the meaning of Fork and join in java Multithreading.

Fork

Fork is a process in which larger task (Task 1) split into sub-task like task 1.1 and task 1.2, independent sub-tasks can be executed concurrently (As shown in the below diagram).

Example

Fork

Syntax
Sum left = new Sum(array, low, mid);

left.fork();

In the above syntax sum is a sub-class of Recursive Task and left.fork() split the task into sub-tasks.

Join

Join is a process where, task join all the results of sub-tasks like Task 1.1 take results from Task 1.1.1 and Task 1.1.2 once the sub-tasks finished their execution (As shown in the below diagram), otherwise it keeps waiting.

join

Syntax
left.join();

In the above syntax left is an object of Sum class.

Note: Fork-Join Pool is a special thread pool designed to work with fork and join task splitting.

Example

The below “Testdemo” program explains usage of Fork and join in java Multithreading.

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ForkJoinPool;

import java.util.concurrent.RecursiveTask;

 

public class Testdemo {

public static void main(final String[] arg) throws InterruptedException,

ExecutionException {

 

int nThreads = Runtime.getRuntime().availableProcessors();

System.out.println(nThreads);

 

int[] numbers = new int[2000];

 

for(int i = 0; i < numbers.length; i++) {

numbers[i] = i;

}

 

ForkJoinPool forkJoinPool = new ForkJoinPool(nThreads);

Long result = forkJoinPool.invoke(new Sum(numbers,0,numbers.length));

System.out.println(result);

}

 

static class Sum extends RecursiveTask<Long> {

int low;

int high;

int[] array;

 

Sum(int[] array, int low, int high) {

this.array = array;

this.low   = low;

this.high  = high;

}

 

protected Long compute() {

 

if(high – low <= 10) {

long sum = 0;

 

for(int i = low; i < high; ++i)

sum += array[i];

return sum;

} else {

int mid = low + (high – low) / 2;

Sum left  = new Sum(array, low, mid);

Sum right = new Sum(array, mid, high);

left.fork();

long rightResult = right.compute();

long leftResult  = left.join();

return leftResult + rightResult;

}

}

}

}

Screenshot

Fork and join in java Multithreading

Benefits of Fork and join in java Multithreading

  • It is good for recursive problems, where a task involves running sub-tasks and then processing their results i.e divide and conquer job.
  • It takes the advantage of the multiple processors or multiple cores on a machine and easily solve the parallelism problems.
  • Reduce computation time and increase performance in solving big problems like image processing, video processing, and  big data processing, etc.

“That’s all about the Fork and join in java Multithreading”

 

Leave a Reply

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