Most visited

Recently visited

Added in API level 21

RecursiveTask

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.

Summary

Public constructors

RecursiveTask()

Public methods

final V getRawResult()

返回 join()返回的结果,即使此任务异常完成,或 null如果此任务未知完成)。

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

Public constructors

RecursiveTask

Added in API level 21
RecursiveTask ()

Public methods

getRawResult

Added in API level 21
V getRawResult ()

返回join()返回的结果,即使此任务异常完成,或null如果此任务未知完成。 此方法旨在帮助调试以及支持扩展。 不鼓励在任何其他情况下使用它。

Returns
V the result, or null if not completed

Protected methods

compute

Added in API level 21
V compute ()

此任务执行的主要计算。

Returns
V the result of the computation

exec

Added in API level 21
boolean exec ()

为RecursiveTask实现执行约定。

Returns
boolean true if this task is known to have completed normally

setRawResult

Added in API level 21
void setRawResult (V value)

因此强制返回给定的值。 此方法旨在支持扩展,通常不应另行调用。

Parameters
value V: the value

Hooray!