RecursiveTask is an abstract class encapsulates a task that returns a result. It is a subclass of ForkJoinTask. The RecursiveTask class is extended to create a task that has a particular return type. The code that represents the computational portion of the task is kept within the compute() method of RecursiveTask.
RecursiveTask class is mostly used in the context of parallel programming. Tasks that can be divided into independent subtasks and the final outcome of the task can be obtained from the outcomes of the subtask, can be implemented more efficiently using RecursiveTask. For example, searching for an element in a large array.
Class Hierarchy
java.lang.Object ↳ java.util.concurrent.ForkJoinTask ↳ java.util.concurrent.RecursiveTask<V>
Constructor
-
RecursiveTask()– Creates an object of RecursiveTask with default settings.
public RecursiveTask()
Methods
- compute()– The method that defines the task.
protected abstract void compute()
-
exec()– This method implements the basic rules necessary for the execution of a task.
protected final boolean exec()
-
getRawResult()– The function returns the value obtained after the completion of the task, even if the task is completed abnormally. It returns null, if the task is not yet completed.
public final Void getRawResult()
-
setRawResult()– The function sets the return value of the task to the value passed in the argument.
protected final void setRawResult(Void mustBeNull)
Example to demonstrate RecursiveTask
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; public class RecursiveTaskDemo { public static void main(String[] args) { ForkJoinPool fjp = new ForkJoinPool(); double[] nums = new double[5000]; for (int i = 0; i < nums.length; i++) { nums[i] = (double)(((i % 2) == 0) ? i : -1); } Sum task = new Sum(nums, 0, nums.length); double summation = fjp.invoke(task); System.out.println("Summation " + summation); } } class Sum extends RecursiveTask<Double> { final int seqThreshold = 500; double[] data; int start, end; Sum(double[] data, int start, int end) { this.data = data; this.start = start; this.end = end; } @Override protected Double compute() { double sum = 0; if ((end - start) < seqThreshold) { for (int i = start; i < end; i++) sum += data[i]; } else { int middle = (start + end) / 2; Sum subtaskA = new Sum(data, start, middle); Sum subtaskB = new Sum(data, middle, end); subtaskA.fork(); subtaskB.fork(); sum += subtaskA.join() + subtaskB.join(); } return sum; } } |
Summation 6245000.0
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java class dependency analyzer in Java 8 with Examples
- Java.awt.image.RescaleOp Class in Java with Examples
- Java.util.BitSet class in Java with Examples | Set 1
- URL class in Java with Examples
- Date class in Java (With Examples)
- Calendar Class in Java with examples
- Java AWT | Cursor class with examples
- Throwable Class in Java with Examples
- StringBuilder Class in Java with Examples
- AbstractSet Class in Java with Examples
- Class getFields() method in Java with Examples
- Class getTypeName() method in Java with Examples
- Class getDeclaredAnnotationsByType() method in Java with Examples
- Class getAnnotation() method in Java with Examples
- Class getName() method in Java with Examples
- Class cast() method in Java with Examples
- Class toGenericString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

