Java.util.concurrent.RecursiveAction class in Java with Examples Last Updated : 17 Jun, 2019 Comments Improve Suggest changes Like Article Like Report 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.lang.Object ↳ java.util.concurrent.ForkJoinTask<Void> ↳ java.util.concurrent.RecursiveAction Constructor of RecursiveAction: RecursiveAction: Creates an object of RecursiveAction with default settings. Syntax: public RecursiveAction() Methods compute()- It is the method that does the computation performed by the task. Syntax: protected abstract void compute() exec()- This method implements the basic rules necessary for the execution of a task for RecursiveAction. Syntax: protected final boolean exec() getRawResult()- The function returns the task completion status. It always returns null. Syntax: public final Void getRawResult() setRawResult()- The function sets the task completion status, to the value passed in the argument. Syntax: protected final void setRawResult(Void mustBeNull) Example: To demonstrate RecursiveAction class Java // 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] + " "); } System.out.println(); SqrtTransform task = new SqrtTransform(nums, 0, nums.length); // Start the task fjp.invoke(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]); } System.out.println(); } } // 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) { this.data = data; this.start = start; this.end = end; } // The method where parallel computation will occur @Override 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)); } } } Output: 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 Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveAction.html Comment More infoAdvertise with us Next Article Java.util.concurrent.RecursiveAction 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.RecursiveTask class in Java with Examples 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 o 2 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 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 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 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 ConcurrentLinkedDeque push() method in Java with Examples The push() method of ConcurrentLinkedDeque class is an in-built function in Java which pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success an 2 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 pop() method in Java with Examples The Java.util.ConcurrentLinkedDeque.pop() method in Java is used to pop an element from the ConcurrentLinkedDeque. The element is popped from the top of the ConcurrentLinkedDeque and is removed from the same.Syntax: ConcurrentLinkedDeque.pop() Parameters: The method does not take any parameters.Retu 2 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 Like