Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/java/util/concurrent/RecursiveAction#pubctors) \| [Methods](https://developer.android.com/reference/java/util/concurrent/RecursiveAction#pubmethods) \| [Protected Methods](https://developer.android.com/reference/java/util/concurrent/RecursiveAction#promethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/concurrent/RecursiveAction#inhmethods)

# RecursiveAction

` public abstract class RecursiveAction `   
` ` ` extends https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<https://developer.android.com/reference/java/lang/Void> ` ` `

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.util.concurrent.ForkJoinTask](https://developer.android.com/reference/java/util/concurrent/ForkJoinTask)\<[java.lang.Void](https://developer.android.com/reference/java/lang/Void)\> ||
|   | ↳ | java.util.concurrent.RecursiveAction |

<br />

*** ** * ** ***

A recursive resultless `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask`. This class establishes conventions to parameterize resultless actions as `Void` `ForkJoinTask`s. Because `null` is the only valid value of type `Void`, methods such as `join` always return `null` upon completion.

**Sample Usages.** Here is a simple but complete ForkJoin sort that sorts a given `long[]` array:

    static class SortTask extends RecursiveAction {
      final long[] array; final int lo, hi;
      SortTask(long[] array, int lo, int hi) {
        this.array = array; this.lo = lo; this.hi = hi;
      }
      SortTask(long[] array) { this(array, 0, array.length); }
      protected void compute() {
        if (hi - lo < THRESHOLD)
          sortSequentially(lo, hi);
        else {
          int mid = (lo + hi) >>> 1;
          invokeAll(new SortTask(array, lo, mid),
                    new SortTask(array, mid, hi));
          merge(lo, mid, hi);
        }
      }
      // implementation details follow:
      static final int THRESHOLD = 1000;
      void sortSequentially(int lo, int hi) {
        Arrays.sort(array, lo, hi);
      }
      void merge(int lo, int mid, int hi) {
        long[] buf = Arrays.copyOfRange(array, lo, mid);
        for (int i = 0, j = lo, k = mid; i < buf.length; j++)
          array[j] = (k == hi || buf[i] < array[k]) ?
            buf[i++] : array[k++];
      }
    }

You could then sort `anArray` by creating `new SortTask(anArray)` and invoking it in a ForkJoinPool. As a more concrete simple example, the following task increments each element of an array:

    class IncrementTask extends RecursiveAction {
      final long[] array; final int lo, hi;
      IncrementTask(long[] array, int lo, int hi) {
        this.array = array; this.lo = lo; this.hi = hi;
      }
      protected void compute() {
        if (hi - lo < THRESHOLD) {
          for (int i = lo; i < hi; ++i)
            array[i]++;
        }
        else {
          int mid = (lo + hi) >>> 1;
          invokeAll(new IncrementTask(array, lo, mid),
                    new IncrementTask(array, mid, hi));
        }
      }
    }

The following example illustrates some refinements and idioms that may lead to better performance: RecursiveActions need not be fully recursive, so long as they maintain the basic divide-and-conquer approach. Here is a class that sums the squares of each element of a double array, by subdividing out only the right-hand-sides of repeated divisions by two, and keeping track of them with a chain of `next` references. It uses a dynamic threshold based on method `getSurplusQueuedTaskCount`, but counterbalances potential excess partitioning by directly performing leaf actions on unstolen tasks rather than further subdividing.

    double sumOfSquares(ForkJoinPool pool, double[] array) {
      int n = array.length;
      Applyer a = new Applyer(array, 0, n, null);
      pool.invoke(a);
      return a.result;
    }

    class Applyer extends RecursiveAction {
      final double[] array;
      final int lo, hi;
      double result;
      Applyer next; // keeps track of right-hand-side tasks
      Applyer(double[] array, int lo, int hi, Applyer next) {
        this.array = array; this.lo = lo; this.hi = hi;
        this.next = next;
      }

      double atLeaf(int l, int h) {
        double sum = 0;
        for (int i = l; i < h; ++i) // perform leftmost base step
          sum += array[i] * array[i];
        return sum;
      }

      protected void compute() {
        int l = lo;
        int h = hi;
        Applyer right = null;
        while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {
          int mid = (l + h) >>> 1;
          right = new Applyer(array, mid, h, right);
          right.fork();
          h = mid;
        }
        double sum = atLeaf(l, h);
        while (right != null) {
          if (right.tryUnfork()) // directly calculate if not stolen
            sum += right.atLeaf(right.lo, right.hi);
          else {
            right.join();
            sum += right.result;
          }
          right = right.next;
        }
        result = sum;
      }
    }

<br />

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/util/concurrent/RecursiveAction#RecursiveAction()() ` Constructor for subclasses to call. |

| ### Public methods ||
|---|---|
| ` final https://developer.android.com/reference/java/lang/Void` | ` https://developer.android.com/reference/java/util/concurrent/RecursiveAction#getRawResult()() ` Always returns `null`. |

| ### Protected methods ||
|---|---|
| ` abstract void` | ` https://developer.android.com/reference/java/util/concurrent/RecursiveAction#compute()() ` The main computation performed by this task. |
| ` final boolean` | ` https://developer.android.com/reference/java/util/concurrent/RecursiveAction#exec()() ` Implements execution conventions for RecursiveActions. |
| ` final void` | ` https://developer.android.com/reference/java/util/concurrent/RecursiveAction#setRawResult(java.lang.Void)(https://developer.android.com/reference/java/lang/Void mustBeNull) ` Requires null completion value. |

### Inherited methods

From class ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask ` ` static <T> https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<T>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#adapt(java.lang.Runnable,%20T)(https://developer.android.com/reference/java/lang/Runnable runnable, T result) `

Returns a new `ForkJoinTask` that performs the `run` method of the given `Runnable` as its action, and returns the given result upon `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#join()`.
` static https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#adapt(java.lang.Runnable)(https://developer.android.com/reference/java/lang/Runnable runnable) `

Returns a new `ForkJoinTask` that performs the `run` method of the given `Runnable` as its action, and returns a null result upon `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#join()`.
` static <T> https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<T>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#adapt(java.util.concurrent.Callable<?%20extends%20T>)(https://developer.android.com/reference/java/util/concurrent/Callable<? extends T> callable) `

Returns a new `ForkJoinTask` that performs the `call` method of the given `Callable` as its action, and returns its result upon `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#join()`, translating any checked exceptions encountered into `RuntimeException`.
` static <T> https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<T>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#adaptInterruptible(java.util.concurrent.Callable<?%20extends%20T>)(https://developer.android.com/reference/java/util/concurrent/Callable<? extends T> callable) `

Returns a new `ForkJoinTask` that performs the `call` method of the given `Callable` as its action, and returns its result upon `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#join()`, translating any checked exceptions encountered into `RuntimeException`.
` boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#cancel(boolean)(boolean mayInterruptIfRunning) `

Attempts to cancel execution of this task.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#compareAndSetForkJoinTaskTag(short,%20short)(short expect, short update) `

Atomically conditionally sets the tag value for this task.
` void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#complete(V)(https://developer.android.com/reference/java/lang/Void value) `

Completes this task, and if not already aborted or cancelled, returning the given value as the result of subsequent invocations of `join` and related operations.
` void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#completeExceptionally(java.lang.Throwable)(https://developer.android.com/reference/java/lang/Throwable ex) `

Completes this task abnormally, and if not already aborted or cancelled, causes it to throw the given exception upon `join` and related operations.
` https://developer.android.com/reference/java/lang/Throwable` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#exceptionNow()() `

Returns the exception thrown by the task, without waiting.
` abstract boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#exec()() `

Immediately performs the base action of this task and returns true if, upon return from this method, this task is guaranteed to have completed.
` final https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<https://developer.android.com/reference/java/lang/Void>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#fork()() `

Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the `https://developer.android.com/reference/java/util/concurrent/ForkJoinPool#commonPool()` if not `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#inForkJoinPool()`.
` final https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#get(long,%20java.util.concurrent.TimeUnit)(long timeout, https://developer.android.com/reference/java/util/concurrent/TimeUnit unit) `

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
` final https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#get()() `

Waits if necessary for the computation to complete, and then retrieves its result.
` final https://developer.android.com/reference/java/lang/Throwable` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#getException()() `

Returns the exception thrown by the base computation, or a `CancellationException` if cancelled, or `null` if none or if the method has not yet completed.
` final short` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#getForkJoinTaskTag()() `

Returns the tag for this task.
` static https://developer.android.com/reference/java/util/concurrent/ForkJoinPool` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#getPool()() `

Returns the pool hosting the current thread, or `null` if the current thread is executing outside of any ForkJoinPool.
` static int` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#getQueuedTaskCount()() `

Returns an estimate of the number of tasks that have been forked by the current worker thread but not yet executed.
` abstract https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#getRawResult()() `

Returns the result that would be returned by `https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#join()`, even if this task completed abnormally, or `null` if this task is not known to have been completed.
` static int` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#getSurplusQueuedTaskCount()() `

Returns an estimate of how many more locally queued tasks are held by the current worker thread than there are other worker threads that might steal them, or zero if this thread is not operating in a ForkJoinPool.
` static void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#helpQuiesce()() `

Possibly executes tasks until the pool hosting the current task [is quiescent](https://developer.android.com/reference/java/util/concurrent/ForkJoinPool#isQuiescent()).
` static boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#inForkJoinPool()() `

Returns `true` if the current thread is a `https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread` executing as a ForkJoinPool computation.
` final https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#invoke()() `

Commences performing this task, awaits its completion if necessary, and returns its result, or throws an (unchecked) `RuntimeException` or `Error` if the underlying computation did so.
` static void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#invokeAll(java.util.concurrent.ForkJoinTask<?>,%20java.util.concurrent.ForkJoinTask<?>)(https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?> t1, https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?> t2) `

Forks the given tasks, returning when `isDone` holds for each task or an (unchecked) exception is encountered, in which case the exception is rethrown.
` static <T extends https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?>> https://developer.android.com/reference/java/util/Collection<T>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#invokeAll(java.util.Collection<T>)(https://developer.android.com/reference/java/util/Collection<T> tasks) `

Forks all tasks in the specified collection, returning when `isDone` holds for each task or an (unchecked) exception is encountered, in which case the exception is rethrown.
` static void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#invokeAll(java.util.concurrent.ForkJoinTask<?>[])(https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?> tasks) `

Forks the given tasks, returning when `isDone` holds for each task or an (unchecked) exception is encountered, in which case the exception is rethrown.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#isCancelled()() `

Returns `true` if this task was cancelled before it completed normally.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#isCompletedAbnormally()() `

Returns `true` if this task threw an exception or was cancelled.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#isCompletedNormally()() `

Returns `true` if this task completed without throwing an exception and was not cancelled.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#isDone()() `

Returns `true` if this task completed.
` final https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#join()() `

Returns the result of the computation when it [is done](https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#isDone()).
` static https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#peekNextLocalTask()() `

Returns, but does not unschedule or execute, a task queued by the current thread but not yet executed, if one is immediately available.
` static https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#pollNextLocalTask()() `

Unschedules and returns, without executing, the next task queued by the current thread but not yet executed, if the current thread is operating in a ForkJoinPool.
` static https://developer.android.com/reference/java/util/concurrent/ForkJoinTask<?>` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#pollTask()() `

If the current thread is operating in a ForkJoinPool, unschedules and returns, without executing, the next task queued by the current thread but not yet executed, if one is available, or if not available, a task that was forked by some other thread, if available.
` final void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#quietlyComplete()() `

Completes this task normally without setting a value.
` final void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#quietlyInvoke()() `

Commences performing this task and awaits its completion if necessary, without returning its result or throwing its exception.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#quietlyJoin(long,%20java.util.concurrent.TimeUnit)(long timeout, https://developer.android.com/reference/java/util/concurrent/TimeUnit unit) `

Tries to join this task, returning true if it completed (possibly exceptionally) before the given timeout and the current thread has not been interrupted.
` final void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#quietlyJoin()() `

Joins this task, without returning its result or throwing its exception.
` final boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#quietlyJoinUninterruptibly(long,%20java.util.concurrent.TimeUnit)(long timeout, https://developer.android.com/reference/java/util/concurrent/TimeUnit unit) `

Tries to join this task, returning true if it completed (possibly exceptionally) before the given timeout.
` void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#reinitialize()() `

Resets the internal bookkeeping state of this task, allowing a subsequent `fork`.
` https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#resultNow()() `

Returns the computed result, without waiting.
` final short` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#setForkJoinTaskTag(short)(short newValue) `

Atomically sets the tag value for this task and returns the old value.
` abstract void` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#setRawResult(V)(https://developer.android.com/reference/java/lang/Void value) `

Forces the given value to be returned as a result.
` https://developer.android.com/reference/java/util/concurrent/Future.State` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#state()() `

<br />

` boolean` ` https://developer.android.com/reference/java/util/concurrent/ForkJoinTask#tryUnfork()() `

Tries to unschedule this task for execution.
From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/util/concurrent/Future ` ` abstract boolean` ` https://developer.android.com/reference/java/util/concurrent/Future#cancel(boolean)(boolean mayInterruptIfRunning) `

Attempts to cancel execution of this task.
` default https://developer.android.com/reference/java/lang/Throwable` ` https://developer.android.com/reference/java/util/concurrent/Future#exceptionNow()() `

Returns the exception thrown by the task, without waiting.
` abstract https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/Future#get(long,%20java.util.concurrent.TimeUnit)(long timeout, https://developer.android.com/reference/java/util/concurrent/TimeUnit unit) `

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
` abstract https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/Future#get()() `

Waits if necessary for the computation to complete, and then retrieves its result.
` abstract boolean` ` https://developer.android.com/reference/java/util/concurrent/Future#isCancelled()() `

Returns `true` if this task was cancelled before it completed normally.
` abstract boolean` ` https://developer.android.com/reference/java/util/concurrent/Future#isDone()() `

Returns `true` if this task completed.
` default https://developer.android.com/reference/java/lang/Void` ` https://developer.android.com/reference/java/util/concurrent/Future#resultNow()() `

Returns the computed result, without waiting.
` default https://developer.android.com/reference/java/util/concurrent/Future.State` ` https://developer.android.com/reference/java/util/concurrent/Future#state()() `

<br />

## Public constructors

### RecursiveAction

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public RecursiveAction ()
```

Constructor for subclasses to call.

<br />

## Public methods

### getRawResult

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final Void getRawResult ()
```

Always returns `null`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Void` | `null` always <br /> |

## Protected methods

### compute

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected abstract void compute ()
```

The main computation performed by this task.

<br />

### exec

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final boolean exec ()
```

Implements execution conventions for RecursiveActions.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if this task is known to have completed normally <br /> |

### setRawResult

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final void setRawResult (Void mustBeNull)
```

Requires null completion value.

<br />

| Parameters ||
|---|---|
| `mustBeNull` | `Void`: the value <br /> |