10 |
|
* Recursive result-bearing ForkJoinTasks. |
11 |
|
* <p> For a classic example, here is a task computing Fibonacci numbers: |
12 |
|
* |
13 |
< |
* <pre> |
14 |
< |
* class Fibonacci extends RecursiveTask<Integer> { |
13 |
> |
* <pre> {@code |
14 |
> |
* class Fibonacci extends RecursiveTask<Integer> { |
15 |
|
* final int n; |
16 |
< |
* Fibonnaci(int n) { this.n = n; } |
16 |
> |
* Fibonacci(int n) { this.n = n; } |
17 |
|
* Integer compute() { |
18 |
< |
* if (n <= 1) |
18 |
> |
* if (n <= 1) |
19 |
|
* return n; |
20 |
|
* Fibonacci f1 = new Fibonacci(n - 1); |
21 |
|
* f1.fork(); |
22 |
|
* Fibonacci f2 = new Fibonacci(n - 2); |
23 |
|
* return f2.compute() + f1.join(); |
24 |
|
* } |
25 |
< |
* } |
26 |
< |
* </pre> |
25 |
> |
* }}</pre> |
26 |
|
* |
27 |
|
* However, besides being a dumb way to compute Fibonacci functions |
28 |
|
* (there is a simple fast linear algorithm that you'd use in |
30 |
|
* subtasks are too small to be worthwhile splitting up. Instead, as |
31 |
|
* is the case for nearly all fork/join applications, you'd pick some |
32 |
|
* minimum granularity size (for example 10 here) for which you always |
33 |
< |
* sequentially solve rather than subdividing. |
33 |
> |
* sequentially solve rather than subdividing. |
34 |
|
* |
35 |
+ |
* @since 1.7 |
36 |
+ |
* @author Doug Lea |
37 |
|
*/ |
38 |
|
public abstract class RecursiveTask<V> extends ForkJoinTask<V> { |
39 |
+ |
private static final long serialVersionUID = 5232453952276485270L; |
40 |
|
|
41 |
|
/** |
42 |
< |
* Empty contructor for use by subclasses. |
41 |
< |
*/ |
42 |
< |
protected RecursiveTask() { |
43 |
< |
} |
44 |
< |
|
45 |
< |
/** |
46 |
< |
* The result returned by compute method. |
42 |
> |
* The result of the computation. |
43 |
|
*/ |
44 |
|
V result; |
45 |
|
|
46 |
|
/** |
47 |
< |
* The main computation performed by this task. |
47 |
> |
* The main computation performed by this task. |
48 |
|
*/ |
49 |
|
protected abstract V compute(); |
50 |
|
|
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* Implements execution conventions for RecursiveTask |
60 |
> |
* Implements execution conventions for RecursiveTask. |
61 |
|
*/ |
62 |
|
protected final boolean exec() { |
63 |
|
result = compute(); |