- 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>
一个递归结果ForkJoinTask。 该类建立了将无结果动作参数化的约定,如VoidForkJoinTasks。 因为null的类型是唯一有效的值Void,如方法join总是返回null完成时。示例用法 这是一个简单但完整的ForkJoin排序,排序给定的
long[]数组:然后,您可以通过创建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++]; } }new SortTask(anArray)并在ForkJoinPool中调用它来排序anArray。 作为一个更具体的简单示例,以下任务会增加数组的每个元素: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)); } } }以下示例说明了可能导致更好性能的一些改进和习语:递归方法不需要完全递归,只要它们保持基本的划分和征服方法。 这是一个通过将仅重复划分的右手区域除以2的双数组的每个元素的平方相加的类,并用
next链的next跟踪它们。 它使用基于方法getSurplusQueuedTaskCount的动态阈值,但通过直接执行叶片动作来解决潜在的过度分割,而不是进一步细分。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; } }- 从以下版本开始:
- 1.7
- 另请参见:
- Serialized Form
-
-
构造方法摘要
构造方法 Constructor 描述 RecursiveAction()
-
方法摘要
所有方法 接口方法 抽象方法 具体的方法 Modifier and Type 方法 描述 protected abstract voidcompute()这个任务执行的主要计算。protected booleanexec()执行递归法的执行约定。VoidgetRawResult()始终返回null。protected voidsetRawResult(Void mustBeNull)需要空完成值。-
Methods inherited from class java.util.concurrent.ForkJoinTask
adapt, adapt, adapt, cancel, compareAndSetForkJoinTaskTag, complete, completeExceptionally, 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, reinitialize, setForkJoinTaskTag, tryUnfork
-
-
-
-
方法详细信息
-
compute
protected abstract void compute()
这个任务执行的主要计算。
-
getRawResult
public final Void getRawResult()
始终返回null。- Specified by:
-
getRawResult在ForkJoinTask<Void> - 结果
-
null总是
-
setRawResult
protected final void setRawResult(Void mustBeNull)
需要空完成值。- Specified by:
-
setRawResult在ForkJoinTask<Void> - 参数
-
mustBeNull- 值
-
exec
protected final boolean exec()
执行递归法的执行约定。- Specified by:
-
exec在ForkJoinTask<Void> - 结果
-
true如果此任务已知正常完成
-
-