public abstract class RecursiveAction extends ForkJoinTask<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不需要完全递归,只要他们保持基本的分而治之的方法。这里是一个类,和一双数组的每个元素的平方,细分出只有右手边的重复区,并跟踪他们的连锁next
参考。它采用了一种基于动态阈值的方法getSurplusQueuedTaskCount
,但抵消潜在过剩的直接执行叶行动unstolen任务而不是进一步细分的分区。
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;
}
}
Constructor and Description |
---|
RecursiveAction() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
compute()
此任务执行的主要计算。
|
protected boolean |
exec()
贯彻执行公约recursiveactions。
|
Void |
getRawResult()
总是返回
null 。
|
protected void |
setRawResult(Void mustBeNull)
需要空完成值。
|
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, pollTask, quietlyComplete, quietlyInvoke, quietlyJoin, reinitialize, setForkJoinTaskTag, tryUnfork
protected abstract void compute()
public final Void getRawResult()
null
。
getRawResult
方法重写,继承类
ForkJoinTask<Void>
null
总是
protected final void setRawResult(Void mustBeNull)
setRawResult
方法重写,继承类
ForkJoinTask<Void>
mustBeNull
-价值
protected final boolean exec()
exec
方法重写,继承类
ForkJoinTask<Void>
true
如果这个任务已经完成正常
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.