Javatpoint Logo
Javatpoint Logo

java.util.concurrent.RecursiveAction class in Java With Examples

The RecursiveAction class is used just for jobs that don't return any results and is an abstract subclass of the java.util.concurrent.ForkJoinTask. In order to signal that the job does not provide a result, it extends the java.lang.Void class.

Tasks that can be separated and can have concurrent execution are begin handled by the RecursiveAction. RecursiveAction, for instance, makes it simple to sort large arrays by splitting them up into smaller, and more manageable chunks and sorting each one of them on a different core.

Key Features and Methods

Fork(), join(), and call() are just a few of the helpful methods that RecursiveAction has inherited as it is a subclass of ForkJoinTask. These techniques make it possible for the work to be carried out in a Fork/Join pool, allowing for effective parallel execution.

Recursive Execution: The compute() method in RecursiveAction is the main method that needs to be implemented. It defines the computation that needs to be performed for a particular task. This method can divide the task into smaller subtasks, invoke them using fork(), and then combine the results using join().

Recursive Termination: To prevent an infinite recursive loop, the compute() method should include a termination condition. This condition checks whether the task has become small enough to be solved sequentially. If the termination condition is met, the task can directly perform its computation without further recursion.

Example

Fork(), join(), and call() are just a few of the helpful methods that RecursiveAction has inherited as it is a subclass of ForkJoinTask. These techniques make it possible for the work to be carried out in a Fork/Join pool, allowing for effective parallel execution.

Recursive Execution: The compute() method in RecursiveAction is the main method that needs to be implemented. It defines the computation that needs to be performed for a particular task. This method can divide the task into smaller subtasks, invoke them using fork(), and then combine the results using join().

Recursive Termination: To prevent an infinite recursive loop, the compute() method should include a termination condition. The condition checks whether the task has become small enough to be solved sequentially. If the termination condition is met, the task can directly perform its computation without further recursion.

To illustrate the usage of RecursiveAction, let's consider an example of calculating the sum of an array of integers. We can divide the array into smaller subarrays, calculate the sum of each subarray concurrently, and then combine the results to obtain the final sum.

Filename: RecursiveActionClass.java

Output:

Sum: 55

In this illustration, the compute() method is overridden by the SumTask class, which inherits RecursiveAction. The job uses the computeSequentially() function to sequentially calculate the sum if the array's size is less than or equal to the threshold value. If not, it splits the task into two smaller ones and recursively calculates the sum of each one. In order to arrive at the final amount, it finally integrates the outcomes of both subtasks.

To use the SumTask class, we can create an instance of it and invoke the invoke() method on it as follows:

Filename: RecursiveTaskClass.java

Output:

Sum: 55

In this example, the SumTask class now extends RecursiveTask<Integer> instead of RecursiveAction. The compute() method returns an Integer value instead of void, and the computation logic is adjusted accordingly. The result of forkJoinPool.invoke(sumTask) is now assigned to the sum variable directly.

In Java, the RecursiveAction class is part of the java.util.concurrent package and is used for creating recursive tasks that do not return a result. It is typically used in conjunction with the Fork/Join framework for parallel computing.

Methods

Here are some methods commonly used with the RecursiveAction class:

protected abstract void compute(): This method must be implemented by subclasses of RecursiveAction. It represents the computation that needs to be performed. It can be recursive in nature, where the task is divided into smaller subtasks.

protected void invokeAll(RecursiveAction... actions): This method can be used to invoke multiple recursive actions concurrently. It initiates the execution of the specified actions and waits for their completion.

protected abstract void setRawResult(Void value): This method is used to set the raw result of the computation. Since RecursiveAction does not return a result, the setRawResult() method is typically not used.

protected abstract Void getRawResult(): This method retrieves the raw result of the computation. As RecursiveAction does not return a result, the getRawResult() method is typically not used.

protected boolean computeInParallel(): This method is a hook that allows subclasses to control whether the computation should be divided into smaller subtasks and executed in parallel or not. By default, it returns true, indicating that the computation should be executed in parallel.

protected void setDone(): This method marks the task as completed. It is usually called when the computation has finished.

These are some of the common methods used with the RecursiveAction class. By extending this class and implementing the compute() method, you can create recursive tasks that can be executed in parallel using the Fork/Join framework.

Here's a complete example that includes the implementation of the methods mentioned earlier in Java:

File Name: MyRecursiveAction.java

Output:

Processing: 1
Processing: 2
Processing: 3
Processing: 4
Processing: 5
Processing: 6
Processing: 7
Processing: 8
Processing: 9
Processing: 10

Conclusion

A useful method for implementing parallel algorithms that adhere to the "divide-and-conquer" principle is provided by the RecursiveAction class in the java.util.concurrent package. The process of breaking a problem down into smaller subproblems and merging their results is made easier by it. Developers can take use of parallel processing capabilities and enhance the performance of their applications by utilising the RecursiveAction class and the Fork/Join framework.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA