RecursiveTask Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
A recursive result-bearing ForkJoinTask
.
[Android.Runtime.Register("java/util/concurrent/RecursiveTask", DoNotGenerateAcw=true)]
[Java.Interop.JavaTypeParameters(new System.String[] { "V" })]
public abstract class RecursiveTask : Java.Util.Concurrent.ForkJoinTask
[<Android.Runtime.Register("java/util/concurrent/RecursiveTask", DoNotGenerateAcw=true)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "V" })>]
type RecursiveTask = class
inherit ForkJoinTask
- Inheritance
- Attributes
Remarks
A recursive result-bearing ForkJoinTask
.
For example, here is a task-based program for computing Factorials:
{@code
import java.util.concurrent.RecursiveTask;
import java.math.BigInteger;
public class Factorial {
static class FactorialTask extends RecursiveTask<BigInteger> {
private final int from, to;
FactorialTask(int from, int to) { this.from = from; this.to = to; }
protected BigInteger compute() {
int range = to - from;
if (range == 0) { // base case
return BigInteger.valueOf(from);
} else if (range == 1) { // too small to parallelize
return BigInteger.valueOf(from).multiply(BigInteger.valueOf(to));
} else { // split in half
int mid = from + range / 2;
FactorialTask leftTask = new FactorialTask(from, mid);
leftTask.fork(); // perform about half the work locally
return new FactorialTask(mid + 1, to).compute()
.multiply(leftTask.join());
}
}
}
static BigInteger factorial(int n) { // uses ForkJoinPool.commonPool()
return (n <= 1) ? BigInteger.ONE : new FactorialTask(1, n).invoke();
}
public static void main(String[] args) {
System.out.println(factorial(Integer.parseInt(args[0])));
}
}}
Added in 1.7.
Java documentation for java.util.concurrent.RecursiveTask
.
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
RecursiveTask() |
Constructor for subclasses to call. |
RecursiveTask(IntPtr, JniHandleOwnership) |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
Properties
Class |
Returns the runtime class of this |
Exception |
Returns the exception thrown by the base computation, or a
|
ForkJoinTaskTag |
Returns the tag for this task. (Inherited from ForkJoinTask) |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
IsCancelled |
Returns |
IsCompletedAbnormally |
Returns |
IsCompletedNormally |
Returns |
IsDone |
Returns |
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 |
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 |
CompleteExceptionally(Throwable) |
Completes this task abnormally, and if not already aborted or
cancelled, causes it to throw the given exception upon
|
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) |
ExceptionNow() | (Inherited from ForkJoinTask) |
Exec() |
Implements execution conventions for RecursiveTask. |
Fork() |
Arranges to asynchronously execute this task in the pool the
current task is running in, if applicable, or using the |
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)
|
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) |
QuietlyJoin(Int64, TimeUnit) |
Tries to join this task, returning true if it completed (possibly exceptionally) before the given timeout and the current thread has not been interrupted. (Inherited from ForkJoinTask) |
QuietlyJoinUninterruptibly(Int64, TimeUnit) |
Tries to join this task, returning true if it completed (possibly exceptionally) before the given timeout. (Inherited from ForkJoinTask) |
Reinitialize() |
Resets the internal bookkeeping state of this task, allowing a
subsequent |
ResultNow() | (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) |
State() | (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) |
Gets the JNI name of the type of the instance |
JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |
GetAsync(IFuture, Int64, TimeUnit) | |
GetAsync(IFuture) |