Java.util.concurrent.RecursiveTask class in Java with Examples Last Updated : 28 Jun, 2019 Comments Improve Suggest changes Like Article Like Report 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 Java 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; } } Output: Summation 6245000.0 Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html Comment More infoAdvertise with us Next Article Java.util.concurrent.RecursiveTask class in Java with Examples C CharchitKapoor Follow Improve Article Tags : Java Java - util package Java-concurrent-package Practice Tags : Java Similar Reads Java.util.concurrent.RecursiveAction class in Java with Examples 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 vo 3 min read Java.util.concurrent.Phaser class in Java with Examples Phaser's primary purpose is to enable synchronization of threads that represent one or more phases of activity. It lets us define a synchronization object that waits until a specific phase has been completed. It then advances to the next phase until that phase concludes. It can also be used to synch 7 min read Java.util.concurrent.Exchanger class with Examples Exchanger is the most interesting synchronization class of Java. It facilitates the exchange of elements between a pair of threads by creating a synchronization point. It simplifies the exchange of data between two threads. Its operation is simple: it simply waits until two separate threads call its 3 min read Java.util.concurrent.Executor interface with Examples The concurrent API in Java provides a feature known as an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads using the thread class. At the core of an executor is the Executor interface. It refers to the objects that execute 1 min read ConcurrentSkipListMap put() method in Java with Examples The put() method of java.util.concurrent.ConcurrentSkipListMap is an in-built function in Java which associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced. Syntax: public V put(K key, V value) Parameter: Th 2 min read ConcurrentSkipListMap clear() method in Java with Examples The clear() method of java.util.concurrent.ConcurrentSkipListMap is an in-built function in Java which removes all of the mappings from this map. This means that all the elements from the map are removed and an empty map is returned. Syntax: public void clear() Parameter: The function does not accep 2 min read Java.util.concurrent.ExecutorService Interface with Examples The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads that determine the shutdown status. The ExecutorSer 3 min read ConcurrentHashMap compute() method in Java with Examples The compute(Key, BiFunction) method of ConcurrentHashMap class is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). This method is used to atomically update a value for given key in ConcurrentHashMap.If the remapping funct 3 min read ConcurrentLinkedDeque remove() method in Java with Examples The java.util.concurrent.ConcurrentLinkedDeque.remove() is an in-built function in Java which is used to remove an element from this deque.Syntax: public E remove() or public boolean remove(Object o) Parameters: The first overload of this method does not accepts any parameter. However the second ove 2 min read Java.util.TimerTask class in Java TimerTask is an abstract class defined in java.util package. TimerTask class defines a task that can be scheduled to run for just once or for repeated number of time. In order to define a TimerTask object, this class needs to be implemented and the run method need to be overridden. The run method is 3 min read Like