- java.lang.Object
-
- java.util.concurrent.ForkJoinTask<Void>
-
- java.util.concurrent.RecursiveAction
-
- All Implemented Interfaces:
- Serializable, Future<Void>
public abstract class RecursiveAction extends ForkJoinTask<Void>
A recursive resultlessForkJoinTask
. This class establishes conventions to parameterize resultless actions asVoid
ForkJoinTask
s. Becausenull
is the only valid value of typeVoid
, methods such as join always returnnull
upon completion.Sample Usages. Here is a sketch of a ForkJoin sort that sorts a given
long[]
array:class SortTask extends RecursiveAction { final long[] array; final int lo; final int hi; SortTask(long[] array, int lo, int hi) { this.array = array; this.lo = lo; this.hi = hi; } protected void compute() { if (hi - lo < THRESHOLD) sequentiallySort(array, lo, hi); else { int mid = (lo + hi) >>> 1; invokeAll(new SortTask(array, lo, mid), new SortTask(array, mid, hi)); merge(array, lo, hi); } } }
anArray
by creatingnew SortTask(anArray, 0, anArray.length-1)
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; final int 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 methodgetSurplusQueuedTaskCount
, 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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description RecursiveAction()
-
Method Summary
Methods Modifier and Type Method and Description protected abstract void
compute()
The main computation performed by this task.protected boolean
exec()
Implements execution conventions for RecursiveActions.Void
getRawResult()
Always returnsnull
.protected void
setRawResult(Void mustBeNull)
Requires null completion value.-
Methods inherited from class java.util.concurrent.ForkJoinTask
adapt, adapt, adapt, cancel, complete, completeExceptionally, fork, get, get, getException, getPool, getQueuedTaskCount, getSurplusQueuedTaskCount, helpQuiesce, inForkJoinPool, invoke, invokeAll, invokeAll, invokeAll, isCancelled, isCompletedAbnormally, isCompletedNormally, isDone, join, peekNextLocalTask, pollNextLocalTask, pollTask, quietlyInvoke, quietlyJoin, reinitialize, tryUnfork
-
-
-
-
Method Detail
-
compute
protected abstract void compute()
The main computation performed by this task.
-
getRawResult
public final Void getRawResult()
Always returnsnull
.- Specified by:
getRawResult
in classForkJoinTask<Void>
- Returns:
null
always
-
setRawResult
protected final void setRawResult(Void mustBeNull)
Requires null completion value.- Specified by:
setRawResult
in classForkJoinTask<Void>
- Parameters:
mustBeNull
- the value
-
exec
protected final boolean exec()
Implements execution conventions for RecursiveActions.- Specified by:
exec
in classForkJoinTask<Void>
- Returns:
true
if completed normally
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
25/11/2024 14:10:57 Cette version de la page est en cache (à la date du 25/11/2024 14:10:57) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/util/concurrent/RecursiveAction.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.