Skip to content
Related Articles

Related Articles

Java.util.concurrent.RecursiveAction class in Java with Examples
  • Difficulty Level : Basic
  • Last Updated : 17 Jun, 2019

RecursiveAction is an abstract class encapsulates a task that does not return a result. It is a subclass of ForkJoinTask, which is an abstract class representing a task that can be executed on a separate core in a multicore system. The RecursiveAction class is extended to create a task that has a void return type. The code that represents the computational portion of the task is kept within the compute() method of RecursiveAction.

RecursiveAction is used for tasks that can be divided and executed parallelly. These tasks should not return any value. For example, sorting a large array can be easily implemented with RecursiveAction, with the array being divided into small manageable chunks and each part being sorted on a separate core.

Class Hierarchy

↳ java.util.concurrent.ForkJoinTask<Void>
  ↳ java.util.concurrent.RecursiveAction

Constructor of RecursiveAction:

  1. RecursiveAction: Creates an object of RecursiveAction with default settings.


    public RecursiveAction()


  1. compute()– It is the method that does the computation performed by the task.


    protected abstract void compute()
  2. exec()– This method implements the basic rules necessary for the execution of a task for RecursiveAction.


    protected final boolean exec()
  3. getRawResult()– The function returns the task completion status. It always returns null.


    public final Void getRawResult()
  4. setRawResult()– The function sets the task completion status, to the value passed in the argument.


    protected final void setRawResult(Void mustBeNull)

Example: To demonstrate RecursiveAction class

// Java program to demonstrate RecursiveAction Class
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
public class ForkJoinDemo {
    public static void main(String[] args)
        // Create a pool of threads.
        ForkJoinPool fjp = new ForkJoinPool();
        double[] nums = new double[100000];
        // Give nums some values
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (double)i;
        System.out.println("A portion of the original sequence");
        for (int i = 0; i < 9; i++) {
            System.out.print(nums[i] + " ");
        SqrtTransform task
            = new SqrtTransform(nums, 0, nums.length);
        // Start the task
        System.out.println("A portion of the transformed sequence"
                           + " (to four decimal places): ");
        for (int i = 0; i < 9; i++) {
            System.out.printf("%.4f ", nums[i]);
// A task that transforms the elements into their square roots
class SqrtTransform extends RecursiveAction {
    final int seqThreshold = 1000;
    double[] data;
    // Determines what part of data to process
    int start, end;
    SqrtTransform(double[] data, int start, int end)
    { = data;
        this.start = start;
        this.end = end;
    // The method where parallel computation will occur
    protected void compute()
        // If the number of elements are less
        // than the sequential threshold
        if ((end - start) < seqThreshold) {
            for (int i = start; i < end; i++) {
                data[i] = Math.sqrt(data[i]);
        else {
            // Otherwise, continue to break the data into smaller pieces
            // Find the midpoint
            int middle = (start + end) / 2;
            // Invoke new tasks, using the subdivided tasks.
            invokeAll(new SqrtTransform(data, start, middle),
                      new SqrtTransform(data, middle, end));

A portion of the original sequence
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 
A portion of the transformed sequence (to four decimal places): 
0.0000 1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 2.6458 2.8284 


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :