Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Fork/Join Framework in Java | RecursiveTask, RecursiveAction

  • Newton's laws of Motion, State and Explained, Formula
    Newton’s laws of Motion, State and Explained, Formula Science
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans of freedom fighters in Hindi | स्वतंत्रता सेनानियों के प्रसिद्ध नारे हिंदी में General Knowledge
  • Why is Makar Sankranti celebrated
    Why is Makar Sankranti celebrated? Festival
  • Human rights day
    Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे General Knowledge
  • What is Adjective in Hindi (विशेषण क्या है?)
    What is Adjective in Hindi (विशेषण क्या है?) Grammar
  • Ramayana Uttar Kand Luv Kush| रामायण उत्तर कांड इन हिंदी
    Ramayana Uttar Kand Luv Kush | रामायण उत्तर कांड इन हिंदी Spiritual
  • What is Pronoun?
    What is Pronoun with example? Pronoun definition and examples Grammar
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge

Fork/Join Framework in Java | RecursiveTask, RecursiveAction

Posted on May 11, 2022May 21, 2022 By GeekCer Education No Comments on Fork/Join Framework in Java | RecursiveTask, RecursiveAction
Fork/Join Framework in Java | RecursiveTask, RecursiveAction

Fork/Join allows us to divide the task into smaller pieces and combine the results. It uses Work-stealing algorithm. If more than one thread is working at the same time, and one thread completes its job while the other thread remains busy, the completed thread steals the tasks from the busy thread and begins working on them.

The basic purpose of using Fork/Join is to improve application performance by taking use of multiple processors. You’ve probably heard of the “divide and conquer” strategy. The Fork/Join system operates similarly.

Let’s understand the basics about Fork and Join. What is Fork and What is Join with some small example. With examples, we will learn how to implement RecursiveTask and RecursiveAction in Java in this article.

Table of Contents

  • What is Fork in Java?
  • What is Join in Java?
  • Pseudocode of Fork/Join in Java
  • ForkJoinPool
  • Implementation of Fork/Join by extending RecursiveTask in Java
    • Syntax of RecursiveTask in Fork/Join
    • Example of RecursiveTask in Fork/Join
  • Implementation of Fork/Join by extending RecursiveAction in Java
    • Syntax of RecursiveAction in Fork/Join
    • Example of RecursiveAction in Fork/Join
  • Difference between RecursiveAction and RecursiveTask in java

What is Fork in Java?

In Java, Fork is the process of dividing a work into smaller tasks that are also independent of one another. These tasks can also be performed in parallel.

What is Join in Java?

The Join idea is used when a work is separated into smaller pieces and executed in parallel. Once all subtasks have completed their execution, the join process joins all the results; otherwise, the join will continue to wait.

Pseudocode of Fork/Join in Java

There are a few things to think about. If your work is really small, you should not split it. It should be carried out immediately. If you work is larger, you should break it down into smaller chunks.

if (smaller work)
  do not split
else
  split work into pieces
  wait for the results

ForkJoinPool

Fork/Join is a framework that implements the ExecutorService interface. ForkJoinPool is the core of the this framework. ForkJoinPool implements the core work-stealing mechanism.

In case of ForkJoinPool, there is no separate thread for each sub-task. The task is stored in its own deque for ForkJoinPool.

Implementation of Fork/Join by extending RecursiveTask in Java

A task can return a value if you use a RecursiveTask in java.

Syntax of RecursiveTask in Fork/Join


pubilc class RecursiveTaskDemo extends RecursiveTask {

   @Override
   protected Long compute() { 
		return null; 
   }
}

Example of RecursiveTask in Fork/Join


import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class RecursiveTaskExample extends RecursiveTask {

	private long[] array;
	private int startIndex;
	private int length;

	public RecursiveTaskExample(int startIndex, int length, long[] array) {
		this.startIndex = startIndex;
		this.length = length;
		this.array = array;
	}

	@Override
	protected Long compute() {
		int processingSizeMin = 100;
		long total = 0;
		if (length - startIndex < processingSizeMin) {
			for (int i = startIndex; i < length; i++) {
				total += array[i];
			}
		} else {
			int mid = (startIndex + length) / 2;
			RecursiveTaskExample recursiveTask1 = new RecursiveTaskExample(startIndex, mid, array);
			RecursiveTaskExample recursiveTask2 = new RecursiveTaskExample(mid, length, array);
			recursiveTask1.fork();
			recursiveTask2.fork();

			total = (long)recursiveTask1.join() + (long)recursiveTask2.join();
		}

		return total;
	}

	public static void main(String[] args) {

		final int maxSize = 10000;
		ForkJoinPool forkJoinPool = new ForkJoinPool();
		long[] array = new long[maxSize];
		for (int i = 0; i < maxSize; i++) {
			array[i] = i;
		}
		RecursiveTaskExample recursiveTask = new RecursiveTaskExample(0, array.length, array);
		long total = (long)forkJoinPool.invoke(recursiveTask);
		System.out.println("Sum of elements = " + total);
	}
}

Output:
Sum of elements = 49995000

Implementation of Fork/Join by extending RecursiveAction in Java

A task doesn’t return a value if you use a RecursiveAction in java.

Syntax of RecursiveAction in Fork/Join


public class RecursiveActionDemo extends RecursiveAction {
   
   @Override
   protected void compute() {
	// Your code here
   }
}

Example of RecursiveAction in Fork/Join


import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class RecursiveActionExample extends RecursiveAction {

	long[] array;
	int startIndex;
	int length;

	public RecursiveActionExample(int startIndex, int length, long[] array) {
		this.startIndex = startIndex;
		this.length = length;
		this.array = array;
	}

	@Override
	protected void compute() {

		int processingSizeMin = 100;
		if (length - startIndex < processingSizeMin) {
			for (int i = startIndex; i < length; i++) {
				array[i] = array[i] * array[i];
			}
		} else {
			int middleValue = (startIndex + length) / 2;
			invokeAll(new RecursiveActionExample(startIndex, middleValue, array),
					new RecursiveActionExample(middleValue, length, array));
		}
	}

	public static void main(String[] args) {

		final int maxLength = 100000;
		ForkJoinPool joinPool = new ForkJoinPool();
		long[] inputArray = new long[maxLength];
		for (int i = 0; i < maxLength; i++) {
			inputArray[i] = i;
		}
		RecursiveActionExample recursiveActionExample = new RecursiveActionExample(0, inputArray.length, inputArray);
		joinPool.invoke(recursiveActionExample);
		System.out.println("Parallelism Level > " + joinPool.getParallelism());

	}
}

Output:
Parallelism Level > 4

Difference between RecursiveAction and RecursiveTask in java

#RecursiveTask RecursiveAction
1The method of this class returns a value if you submit the task using RecursiveTask.But the method of this class doesn’t return any value in case of RecursiveAction.
2protected abstract V compute();
You can change the return type as per your requirement.
protected abstract void compute()
The return type is void.

Also Read:
Multithreading in Java
Difference between Callable and Runnable

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: Java 11 new Features (With example Programs)
Next Post: Java 8 Stream map example | Java 8 map(function) parameter

More Related Articles

Top Java Programs for Coder Top Java Programs for Coder Java
Difference between GenericServlet and HttpServlet Difference between GenericServlet and HttpServlet Differences
Java 8 interview questions and answers, Java Stream, Optional Java 8 interview questions and answers, Java Stream, Optional Important
Java Switch Case Java Switch Case : Switch fall-through, default, break, examples Java
Collections in Java Collections in Java Java
Java Operators Java Operators Java

Leave a Reply Cancel reply

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

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved