Compartilhar via


RecursiveAction Class

Definition

A recursive resultless ForkJoinTask.

[Android.Runtime.Register("java/util/concurrent/RecursiveAction", DoNotGenerateAcw=true)]
public abstract class RecursiveAction : Java.Util.Concurrent.ForkJoinTask
[<Android.Runtime.Register("java/util/concurrent/RecursiveAction", DoNotGenerateAcw=true)>]
type RecursiveAction = class
    inherit ForkJoinTask
Inheritance
RecursiveAction
Attributes

Remarks

A recursive resultless ForkJoinTask. This class establishes conventions to parameterize resultless actions as VoidForkJoinTasks. Because null is the only valid value of type Void, methods such as join always return null upon completion.

<b>Sample Usages.</b> Here is a simple but complete ForkJoin sort that sorts a given long[] array:

{@code
            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:

{@code
            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.

{@code
            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;
              }
            }}

Added in 1.7.

Java documentation for java.util.concurrent.RecursiveAction.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

RecursiveAction()

Constructor for subclasses to call.

RecursiveAction(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Exception

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.

(Inherited from ForkJoinTask)
ForkJoinTaskTag

Returns the tag for this task.

(Inherited from ForkJoinTask)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsCancelled

Returns true if this task was cancelled before it completed normally.

(Inherited from ForkJoinTask)
IsCompletedAbnormally

Returns true if this task threw an exception or was cancelled.

(Inherited from ForkJoinTask)
IsCompletedNormally

Returns true if this task completed without throwing an exception and was not cancelled.

(Inherited from ForkJoinTask)
IsDone

Returns true if this task completed.

(Inherited from ForkJoinTask)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
RawRawResult

Returns the result that would be returned by Join(), even if this task completed abnormally, or null if this task is not known to have been completed.

(Inherited from ForkJoinTask)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Cancel(Boolean)

Attempts to cancel execution of this task.

(Inherited from ForkJoinTask)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CompareAndSetForkJoinTaskTag(Int16, Int16)

Atomically conditionally sets the tag value for this task.

(Inherited from ForkJoinTask)
Complete(Object)

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.

(Inherited from ForkJoinTask)
CompleteExceptionally(Throwable)

Completes this task abnormally, and if not already aborted or cancelled, causes it to throw the given exception upon join and related operations.

(Inherited from ForkJoinTask)
Compute()

The main computation performed by this task.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Exec()

Implements execution conventions for RecursiveActions.

Fork()

Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the ForkJoinPool#commonPool() if not #inForkJoinPool.

(Inherited from ForkJoinTask)
Get()

Waits if necessary for the computation to complete, and then retrieves its result.

(Inherited from ForkJoinTask)
Get(Int64, TimeUnit)

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

(Inherited from ForkJoinTask)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
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.

(Inherited from ForkJoinTask)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Join()

Returns the result of the computation when it #isDone is done.

(Inherited from ForkJoinTask)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
QuietlyComplete()

Completes this task normally without setting a value.

(Inherited from ForkJoinTask)
QuietlyInvoke()

Commences performing this task and awaits its completion if necessary, without returning its result or throwing its exception.

(Inherited from ForkJoinTask)
QuietlyJoin()

Joins this task, without returning its result or throwing its exception.

(Inherited from ForkJoinTask)
Reinitialize()

Resets the internal bookkeeping state of this task, allowing a subsequent fork.

(Inherited from ForkJoinTask)
SetForkJoinTaskTag(Int16)

Atomically sets the tag value for this task and returns the old value.

(Inherited from ForkJoinTask)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetRawResult(Object)

Forces the given value to be returned as a result.

(Inherited from ForkJoinTask)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
TryUnfork()

Tries to unschedule this task for execution.

(Inherited from ForkJoinTask)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
GetAsync(IFuture, Int64, TimeUnit)
GetAsync(IFuture)

Applies to