Class RecursiveAction
- All Implemented Interfaces:
Serializable, Future<Void>
A recursive resultless
ForkJoinTask. This class
establishes conventions to parameterize resultless actions as
Void ForkJoinTasks. 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;
}
}- Since:
- 1.7
- See Also:
-
Nested Class Summary
Nested classes/interfaces declared in interface Future
Future.State -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidcompute()The main computation performed by this task.protected final booleanexec()Implements execution conventions for RecursiveActions.final VoidAlways returnsnull.protected final voidsetRawResult(Void mustBeNull) Requires null completion value.Methods declared in class ForkJoinTask
adapt, adapt, adapt, adaptInterruptible, adaptInterruptible, adaptInterruptible, cancel, compareAndSetForkJoinTaskTag, complete, completeExceptionally, exceptionNow, fork, get, get, getException, getForkJoinTaskTag, getPool, getQueuedTaskCount, getSurplusQueuedTaskCount, helpQuiesce, inForkJoinPool, invoke, invokeAll, invokeAll, invokeAll, isCancelled, isCompletedAbnormally, isCompletedNormally, isDone, join, peekNextLocalTask, pollNextLocalTask, pollSubmission, pollTask, quietlyComplete, quietlyInvoke, quietlyJoin, quietlyJoin, quietlyJoinUninterruptibly, reinitialize, resultNow, setForkJoinTaskTag, state, tryUnforkModifier and TypeMethodDescriptionstatic ForkJoinTask<?> Returns a newForkJoinTaskthat performs therunmethod of the givenRunnableas its action, and returns a null result uponForkJoinTask.join().static <T> ForkJoinTask<T> Returns a newForkJoinTaskthat performs therunmethod of the givenRunnableas its action, and returns the given result uponForkJoinTask.join().static <T> ForkJoinTask<T> Returns a newForkJoinTaskthat performs thecallmethod of the givenCallableas its action, and returns its result uponForkJoinTask.join(), translating any checked exceptions encountered intoRuntimeException.static ForkJoinTask<?> adaptInterruptible(Runnable runnable) Returns a newForkJoinTaskthat performs therunmethod of the givenRunnableas its action, and returns null uponForkJoinTask.join(), translating any checked exceptions encountered intoRuntimeException.static <T> ForkJoinTask<T> adaptInterruptible(Runnable runnable, T result) Returns a newForkJoinTaskthat performs therunmethod of the givenRunnableas its action, and returns the given result uponForkJoinTask.join(), translating any checked exceptions encountered intoRuntimeException.static <T> ForkJoinTask<T> adaptInterruptible(Callable<? extends T> callable) Returns a newForkJoinTaskthat performs thecallmethod of the givenCallableas its action, and returns its result uponForkJoinTask.join(), translating any checked exceptions encountered intoRuntimeException.booleancancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this task.final booleancompareAndSetForkJoinTaskTag(short expect, short update) Atomically conditionally sets the tag value for this task.voidCompletes this task, and if not already aborted or cancelled, returning the given value as the result of subsequent invocations ofjoinand related operations.voidCompletes this task abnormally, and if not already aborted or cancelled, causes it to throw the given exception uponjoinand related operations.Returns the exception thrown by the task, without waiting.final ForkJoinTask<Void> fork()Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using theForkJoinPool.commonPool()if notForkJoinTask.inForkJoinPool().final Voidget()Waits if necessary for the computation to complete, and then retrieves its result.final VoidWaits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.final ThrowableReturns the exception thrown by the base computation, or aCancellationExceptionif cancelled, ornullif none or if the method has not yet completed.final shortReturns the tag for this task.static ForkJoinPoolgetPool()Returns the pool hosting the current thread, ornullif the current thread is executing outside of any ForkJoinPool.static intReturns an estimate of the number of tasks that have been forked by the current worker thread but not yet executed.static intReturns 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 voidPossibly executes tasks until the pool hosting the current task is quiescent.static booleanReturnstrueif the current thread is aForkJoinWorkerThreadexecuting as a ForkJoinPool computation.final Voidinvoke()Commences performing this task, awaits its completion if necessary, and returns its result, or throws an (unchecked)RuntimeExceptionorErrorif the underlying computation did so.static <T extends ForkJoinTask<?>>
Collection<T> invokeAll(Collection<T> tasks) Forks all tasks in the specified collection, returning whenisDoneholds for each task or an (unchecked) exception is encountered, in which case the exception is rethrown.static voidinvokeAll(ForkJoinTask<?>... tasks) Forks the given tasks, returning whenisDoneholds for each task or an (unchecked) exception is encountered, in which case the exception is rethrown.static voidinvokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) Forks the given tasks, returning whenisDoneholds for each task or an (unchecked) exception is encountered, in which case the exception is rethrown.final booleanReturnstrueif this task was cancelled before it completed normally.final booleanReturnstrueif this task threw an exception or was cancelled.final booleanReturnstrueif this task completed without throwing an exception and was not cancelled.final booleanisDone()Returnstrueif this task completed.final Voidjoin()Returns the result of the computation when it is done.protected static ForkJoinTask<?> Returns, but does not unschedule or execute, a task queued by the current thread but not yet executed, if one is immediately available.protected static ForkJoinTask<?> 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.protected static ForkJoinTask<?> If the current thread is operating in a ForkJoinPool, unschedules and returns, without executing, a task externally submitted to the pool, if one is available.protected static 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 voidCompletes this task normally without setting a value.final voidCommences performing this task and awaits its completion if necessary, without returning its result or throwing its exception.final voidJoins this task, without returning its result or throwing its exception.final booleanquietlyJoin(long timeout, TimeUnit unit) Tries to join this task, returning true if it completed (possibly exceptionally) before the given timeout elapsed and the current thread has not been interrupted.final booleanquietlyJoinUninterruptibly(long timeout, TimeUnit unit) Tries to join this task, returning true if it completed (possibly exceptionally) before the given timeout elapsed.voidResets the internal bookkeeping state of this task, allowing a subsequentfork.Returns the computed result, without waiting.final shortsetForkJoinTaskTag(short newValue) Atomically sets the tag value for this task and returns the old value.state()Returns the computation state.booleanTries to unschedule this task for execution.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?> getClass()Returns the runtime class of thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.toString()Returns a string representation of the object.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(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 voidwait(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.
-
Constructor Details
-
RecursiveAction
public RecursiveAction()Constructor for subclasses to call.
-
-
Method Details
-
compute
protected abstract void compute()The main computation performed by this task. -
getRawResult
Always returnsnull.- Specified by:
getRawResultin classForkJoinTask<Void>- Returns:
nullalways
-
setRawResult
Requires null completion value.- Specified by:
setRawResultin classForkJoinTask<Void>- Parameters:
mustBeNull- the value
-
exec
protected final boolean exec()Implements execution conventions for RecursiveActions.- Specified by:
execin classForkJoinTask<Void>- Returns:
trueif this task is known to have completed normally
-