- 所有已实现的接口:
Serializable,Future<Void>
递归无结果
ForkJoinTask 。此类建立了将无结果操作参数化为 Void ForkJoinTask s 的约定。因为 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));
}
}
}
以下示例说明了一些可能会带来更好性能的改进和习惯用法: RecursiveActions 不需要完全递归,只要它们保持基本的分而治之方法即可。这是一个类,通过仅将重复除以 2 的右侧细分,并使用 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
- 参见:
-
内部类总结
在接口 java.util.concurrent.Future 中声明的嵌套类/接口
Future.State -
构造方法总结
构造方法 -
方法总结
修饰符和类型方法描述protected abstract voidcompute()此任务执行的主要计算。protected final booleanexec()实现 RecursiveActions 的执行约定。final Void总是返回null。protected final voidsetRawResult(Void mustBeNull) 需要空完成值。在类 java.util.concurrent.ForkJoinTask 中声明的方法
adapt, adapt, adapt, adaptInterruptible, 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, quietlyJoin, quietlyJoinUninterruptibly, reinitialize, setForkJoinTaskTag, tryUnfork在类 java.lang.Object 中声明的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait在接口 java.util.concurrent.Future 中声明的方法
exceptionNow, resultNow, state
-
构造方法详细信息
-
RecursiveAction
public RecursiveAction()子类调用的构造方法。
-
-
方法详情
-
compute
protected abstract void compute()此任务执行的主要计算。 -
getRawResult
总是返回null。- 指定者:
getRawResult在类ForkJoinTask<Void>中- 返回:
null总是
-
setRawResult
需要空完成值。- 指定者:
setRawResult在类ForkJoinTask<Void>中- 参数:
mustBeNull- 值
-
exec
protected final boolean exec()实现 RecursiveActions 的执行约定。- 指定者:
exec在类ForkJoinTask<Void>中- 返回:
true如果已知此任务已正常完成
-