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 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import java.util.*; |
8 |
import java.util.concurrent.*; |
9 |
|
10 |
/** |
11 |
* A computation that is broken into a series of task executions, each |
12 |
* separated by a Phaser arrival. Concrete subclasses must |
13 |
* define method {@code compute}, that performs the action occurring |
14 |
* at each step of the barrier. Upon invocation of this task, the |
15 |
* {@code compute} method is repeatedly invoked until the barrier |
16 |
* {@code isTerminated} or until its execution throws an exception. |
17 |
* |
18 |
* <p><b>Sample Usage.</b> Here is a sketch of a set of CyclicActions |
19 |
* that each perform 500 iterations of an imagined image smoothing |
20 |
* operation. Note that the aggregate ImageSmoother task itself is not |
21 |
* a CyclicTask. |
22 |
* |
23 |
* <pre> |
24 |
* class ImageSmoother extends RecursiveAction { |
25 |
* protected void compute() { |
26 |
* Phaser b = new Phaser() { |
27 |
* protected boolean onAdvance(int cycle, int registeredParties) { |
28 |
* return registeredParties <= 0 || cycle >= 500; |
29 |
* } |
30 |
* } |
31 |
* int n = pool.getParallelismLevel(); |
32 |
* CyclicAction[] actions = new CyclicAction[n]; |
33 |
* for (int i = 0; i < n; ++i) { |
34 |
* action[i] = new CyclicAction(b) { |
35 |
* protected void compute() { |
36 |
* smoothImagePart(i); |
37 |
* } |
38 |
* } |
39 |
* } |
40 |
* invokeAll(actions); |
41 |
* } |
42 |
* } |
43 |
* </pre> |
44 |
*/ |
45 |
public abstract class CyclicAction extends ForkJoinTask<Void> { |
46 |
final Phaser barrier; |
47 |
boolean deregistered; |
48 |
int lastArrived; |
49 |
|
50 |
/** |
51 |
* Constructs a new CyclicAction using the supplied barrier, |
52 |
* registering for this barrier upon construction. |
53 |
* @param barrier the barrier |
54 |
*/ |
55 |
public CyclicAction(Phaser barrier) { |
56 |
this.barrier = barrier; |
57 |
lastArrived = barrier.register() - 1; |
58 |
} |
59 |
|
60 |
/** |
61 |
* The computation performed by this task on each cycle of the |
62 |
* barrier. While you must define this method, you should not in |
63 |
* general call it directly. |
64 |
*/ |
65 |
protected abstract void step(); |
66 |
|
67 |
/** |
68 |
* Returns the barrier |
69 |
*/ |
70 |
public final Phaser getBarrier() { |
71 |
return barrier; |
72 |
} |
73 |
|
74 |
/** |
75 |
* Returns the current cycle of the barrier |
76 |
*/ |
77 |
public final int getCycle() { |
78 |
return barrier.getPhase(); |
79 |
} |
80 |
|
81 |
public final Void getRawResult() { return null; } |
82 |
protected final void setRawResult(Void mustBeNull) { } |
83 |
|
84 |
private void deregister() { |
85 |
if (!deregistered) { |
86 |
deregistered = true; |
87 |
barrier.arriveAndDeregister(); |
88 |
} |
89 |
} |
90 |
|
91 |
protected final boolean exec() { |
92 |
Phaser b = barrier; |
93 |
if (!isDone()) { |
94 |
b.awaitAdvance(lastArrived); |
95 |
if (b.getPhase() >= 0) { |
96 |
try { |
97 |
step(); |
98 |
} catch (Throwable rex) { |
99 |
deregister(); |
100 |
completeExceptionally(rex); |
101 |
return false; |
102 |
} |
103 |
if ((lastArrived = b.arrive()) >= 0) { |
104 |
this.fork(); |
105 |
return false; |
106 |
} |
107 |
} |
108 |
} |
109 |
deregister(); |
110 |
return true; |
111 |
} |
112 |
|
113 |
} |