VOOZH about

URL: https://www.geeksforgeeks.org/java/java-util-concurrent-recursivetask-class-in-java-with-examples/

⇱ Java.util.concurrent.RecursiveTask class in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.util.concurrent.RecursiveTask class in Java with Examples

Last Updated : 28 Jun, 2019
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
  1. RecursiveTask()- Creates an object of RecursiveTask with default settings.
    public RecursiveTask()
    
Methods
  1. compute()- The method that defines the task.
    protected abstract void compute()
    
  2. exec()- This method implements the basic rules necessary for the execution of a task.
    protected final boolean exec()
    
  3. 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()
    
  4. 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
Output:
Summation 6245000.0
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html
Comment