public abstract class RecursiveTask
extends ForkJoinTask<V>
java.lang.Object | ||
↳ | java.util.concurrent.ForkJoinTask<V> | |
↳ | java.util.concurrent.RecursiveTask<V> |
递归结果 - 方位 ForkJoinTask
。
举一个典型的例子,这是一个计算斐波那契数的任务:
class Fibonacci extends RecursiveTask<Integer> {
final int n;
Fibonacci(int n) { this.n = n; }
protected Integer compute() {
if (n <= 1)
return n;
Fibonacci f1 = new Fibonacci(n - 1);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2);
return f2.compute() + f1.join();
}
}
However, besides being a dumb way to compute Fibonacci functions (there is a simple fast linear algorithm that you'd use in practice), this is likely to perform poorly because the smallest subtasks are too small to be worthwhile splitting up. Instead, as is the case for nearly all fork/join applications, you'd pick some minimum granularity size (for example 10 here) for which you always sequentially solve rather than subdividing.
Public constructors |
|
---|---|
RecursiveTask() |
Public methods |
|
---|---|
final V |
getRawResult() 返回 |
Protected methods |
|
---|---|
abstract V |
compute() 此任务执行的主要计算。 |
final boolean |
exec() 为RecursiveTask实现执行约定。 |
final void |
setRawResult(V value) 因此强制返回给定的值。 |
Inherited methods |
|
---|---|
From class java.util.concurrent.ForkJoinTask
|
|
From class java.lang.Object
|
|
From interface java.util.concurrent.Future
|
V getRawResult ()
返回join()
返回的结果,即使此任务异常完成,或null
如果此任务未知完成。 此方法旨在帮助调试以及支持扩展。 不鼓励在任何其他情况下使用它。
Returns | |
---|---|
V |
the result, or null if not completed |
boolean exec ()
为RecursiveTask实现执行约定。
Returns | |
---|---|
boolean |
true if this task is known to have completed normally |
void setRawResult (V value)
因此强制返回给定的值。 此方法旨在支持扩展,通常不应另行调用。
Parameters | |
---|---|
value |
V : the value |