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, an 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(); |
101 |
|
* Overridable callback action triggered upon <tt>complete</tt> of |
102 |
|
* subtasks. Upon invocation, both subtasks have completed. |
103 |
|
* After return, this task <tt>isDone</tt> and is joinable by |
104 |
< |
* other tasks. The default version of this method does |
105 |
< |
* nothing. But it may may be overridden in subclasses to perform |
106 |
< |
* some action (for example a reduction) when this task is |
107 |
< |
* completes. |
104 |
> |
* other tasks. The default version of this method does nothing. |
105 |
> |
* But it may be overridden in subclasses to perform some action |
106 |
> |
* (for example a reduction) when this task is completes. |
107 |
|
* @param x one subtask |
108 |
|
* @param y the other subtask |
109 |
|
*/ |
173 |
|
break; |
174 |
|
try { |
175 |
|
p.onComplete(a, s); |
176 |
< |
} catch(Throwable rex) { |
176 |
> |
} catch (Throwable rex) { |
177 |
|
p.completeExceptionally(rex); |
178 |
|
return; |
179 |
|
} |
250 |
|
* @param update the new value |
251 |
|
* @return true if successful |
252 |
|
*/ |
253 |
< |
protected final boolean compareAndSetControlState(int expect, |
253 |
> |
protected final boolean compareAndSetControlState(int expect, |
254 |
|
int update) { |
255 |
|
return controlStateUpdater.compareAndSet(this, expect, update); |
256 |
|
} |