1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.concurrent.*; |
15 |
|
* BinaryAsyncActions are simpler to use and have less overhead in |
16 |
|
* typical uasges but are restricted to binary computation trees. |
17 |
|
* |
18 |
< |
* <p> Upon construction, an BinaryAsyncAction does not bear any |
18 |
> |
* <p>Upon construction, a BinaryAsyncAction does not bear any |
19 |
|
* linkages. For non-root tasks, links must be established using |
20 |
|
* method <tt>linkSubtasks</tt> before use. |
21 |
|
* |
22 |
< |
* <p> <b>Sample Usage.</b> A version of Fibonacci: |
22 |
> |
* <p><b>Sample Usage.</b> A version of Fibonacci: |
23 |
|
* <pre> |
24 |
|
* class Fib extends BinaryAsyncAction { |
25 |
|
* final int n; |
27 |
|
* Fib(int n) { this.n = n; } |
28 |
|
* protected void compute() { |
29 |
|
* if (n > 1) { |
30 |
< |
* linkAndForkSubtasks(new Fib(n-1), new Fib(n-2)); |
30 |
> |
* linkAndForkSubtasks(new Fib(n-1), new Fib(n-2)); |
31 |
|
* else { |
32 |
< |
* result = n; // fib(0)==0; fib(1)==1 |
33 |
< |
* complete(); |
32 |
> |
* result = n; // fib(0)==0; fib(1)==1 |
33 |
> |
* complete(); |
34 |
|
* } |
35 |
|
* } |
36 |
|
* protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
37 |
< |
* result = ((Fib)x).result + ((Fib)y).result; |
37 |
> |
* result = ((Fib)x).result + ((Fib)y).result; |
38 |
|
* } |
39 |
|
* } |
40 |
|
* </pre> |
44 |
|
* protected void compute() { |
45 |
|
* Fib f = this; |
46 |
|
* while (f.n > 1) { |
47 |
< |
* Fib left = new Fib(f.n - 1); |
48 |
< |
* Fib right = new Fib(f.n - 2); |
49 |
< |
* f.linkSubtasks(left, right); |
50 |
< |
* right.fork(); // fork right |
51 |
< |
* f = left; // loop on left |
47 |
> |
* Fib left = new Fib(f.n - 1); |
48 |
> |
* Fib right = new Fib(f.n - 2); |
49 |
> |
* f.linkSubtasks(left, right); |
50 |
> |
* right.fork(); // fork right |
51 |
> |
* f = left; // loop on left |
52 |
|
* } |
53 |
|
* f.result = f.n; |
54 |
|
* f.complete(); |
120 |
|
* default version of this method does nothing and returns |
121 |
|
* <tt>true</tt>. |
122 |
|
* @return true if this task's exception should be propagated to |
123 |
< |
* this tasks parent. |
123 |
> |
* this task's parent. |
124 |
|
*/ |
125 |
|
protected boolean onException() { |
126 |
|
return true; |