11 |
|
/** |
12 |
|
* AsyncActions that may be linked in parent-child relationships. |
13 |
|
* |
14 |
< |
* <p>Upon construction, an LinkedAsyncAction may register as a |
14 |
> |
* <p>Upon construction, a LinkedAsyncAction may register as a |
15 |
|
* subtask of a given parent task. In this case, completion of this |
16 |
|
* task will propagate to its parent. If the parent's pending subtask |
17 |
|
* completion count becomes zero, it too will complete. |
18 |
< |
* LinkedAsyncActions rarely use methods <tt>join</tt> or |
19 |
< |
* <tt>invoke</tt> but instead propagate completion to parents |
20 |
< |
* implicitly via <tt>complete</tt>. While typical, it is not necessary |
21 |
< |
* for each task to <tt>complete</tt> itself. For example, it is |
18 |
> |
* LinkedAsyncActions rarely use methods {@code join} or |
19 |
> |
* {@code invoke} but instead propagate completion to parents |
20 |
> |
* implicitly via {@code complete}. While typical, it is not necessary |
21 |
> |
* for each task to {@code complete} itself. For example, it is |
22 |
|
* possible to treat one subtask as a continuation of the current task |
23 |
|
* by not registering it on construction. In this case, a |
24 |
< |
* <tt>complete</tt> of the subtask will trigger <tt>complete</tt> of the |
24 |
> |
* {@code complete} of the subtask will trigger {@code complete} of the |
25 |
|
* parent without the parent explicitly doing so. |
26 |
|
* |
27 |
|
* <p>In addition to supporting these different computation styles |
29 |
|
* stack space footprints while executing, but may have greater |
30 |
|
* per-task overhead. |
31 |
|
* |
32 |
< |
* <p><b>Sample Usage.</b> Here is a sketch of an LinkedAsyncAction |
32 |
> |
* <p><b>Sample Usage.</b> Here is a sketch of a LinkedAsyncAction |
33 |
|
* that visits all of the nodes of a graph. The details of the graph's |
34 |
|
* Node and Edge classes are omitted, but we assume each node contains |
35 |
< |
* an <tt>AtomicBoolean</tt> mark that starts out false. To execute |
35 |
> |
* an {@code AtomicBoolean} mark that starts out false. To execute |
36 |
|
* this, you would create a GraphVisitor for the root node with null |
37 |
< |
* parent, and <tt>invoke</tt> in a ForkJoinPool. Upon return, all |
37 |
> |
* parent, and {@code invoke} in a ForkJoinPool. Upon return, all |
38 |
|
* reachable nodes will have been visited. |
39 |
|
* |
40 |
|
* <pre> |
77 |
|
|
78 |
|
/** |
79 |
|
* Creates a new action with no parent. (You can add a parent |
80 |
< |
* later (but before forking) via <tt>reinitialize</tt>). |
80 |
> |
* later (but before forking) via {@code reinitialize}). |
81 |
|
*/ |
82 |
|
protected LinkedAsyncAction() { |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
|
* Creates a new action with the given parent. If the parent is |
87 |
< |
* non-null, this tasks registers with the parent, in which case, |
87 |
> |
* non-null, this task registers with the parent, in which case, |
88 |
|
* the parent task cannot complete until this task completes. |
89 |
|
* @param parent the parent task, or null if none |
90 |
|
*/ |
97 |
|
/** |
98 |
|
* Creates a new action with the given parent, optionally |
99 |
|
* registering with the parent. If the parent is non-null and |
100 |
< |
* <tt>register</tt> is true, this tasks registers with the |
100 |
> |
* {@code register} is true, this task registers with the |
101 |
|
* parent, in which case, the parent task cannot complete until |
102 |
|
* this task completes. |
103 |
|
* @param parent the parent task, or null if none |
114 |
|
* Creates a new action with the given parent, optionally |
115 |
|
* registering with the parent, and setting the pending join count |
116 |
|
* to the given value. If the parent is non-null and |
117 |
< |
* <tt>register</tt> is true, this tasks registers with the |
117 |
> |
* {@code register} is true, this task registers with the |
118 |
|
* parent, in which case, the parent task cannot complete until |
119 |
|
* this task completes. Setting the pending join count requires |
120 |
|
* care -- it is correct only if child tasks do not themselves |
137 |
|
protected final void setRawResult(Void mustBeNull) { } |
138 |
|
|
139 |
|
/** |
140 |
< |
* Overridable callback action triggered by <tt>complete</tt>. Upon |
140 |
> |
* Overridable callback action triggered by {@code complete}. Upon |
141 |
|
* invocation, all subtasks have completed. After return, this |
142 |
< |
* task <tt>isDone</tt> and is joinable by other tasks. The |
142 |
> |
* task {@code isDone} and is joinable by other tasks. The |
143 |
|
* default version of this method does nothing. But it may be |
144 |
|
* overridden in subclasses to perform some action when this task |
145 |
|
* is about to complete. |
149 |
|
|
150 |
|
/** |
151 |
|
* Overridable callback action triggered by |
152 |
< |
* <tt>completeExceptionally</tt>. Upon invocation, this task has |
152 |
> |
* {@code completeExceptionally}. Upon invocation, this task has |
153 |
|
* aborted due to an exception (accessible via |
154 |
< |
* <tt>getException</tt>). If this method returns <tt>true</tt>, |
154 |
> |
* {@code getException}). If this method returns {@code true}, |
155 |
|
* the exception propagates to the current task's |
156 |
|
* parent. Otherwise, normal completion is propagated. The |
157 |
|
* default version of this method does nothing and returns |
158 |
< |
* <tt>true</tt>. |
158 |
> |
* {@code true}. |
159 |
|
* @return true if this task's exception should be propagated to |
160 |
< |
* this tasks parent. |
160 |
> |
* this task's parent |
161 |
|
*/ |
162 |
|
protected boolean onException() { |
163 |
|
return true; |
175 |
|
|
176 |
|
/** |
177 |
|
* Completes this task. If the pending subtask completion count is |
178 |
< |
* zero, invokes <tt>onCompletion</tt>, then causes this task to |
179 |
< |
* be joinable (<tt>isDone</tt> becomes true), and then |
180 |
< |
* recursively applies to this tasks's parent, if it exists. If an |
181 |
< |
* exception is encountered in any <tt>onCompletion</tt> |
178 |
> |
* zero, invokes {@code onCompletion}, then causes this task to |
179 |
> |
* be joinable ({@code isDone} becomes true), and then |
180 |
> |
* recursively applies to this task's parent, if it exists. If an |
181 |
> |
* exception is encountered in any {@code onCompletion} |
182 |
|
* invocation, that task and its ancestors |
183 |
< |
* <tt>completeExceptionally</tt>. |
183 |
> |
* {@code completeExceptionally}. |
184 |
|
*/ |
185 |
|
public final void complete() { |
186 |
|
LinkedAsyncAction a = this; |
204 |
|
/** |
205 |
|
* Completes this task abnormally. Unless this task already |
206 |
|
* cancelled or aborted, upon invocation, this method invokes |
207 |
< |
* <tt>onException</tt>, and then, depending on its return value, |
208 |
< |
* completees parent (if one exists) exceptionally or normally. To |
207 |
> |
* {@code onException}, and then, depending on its return value, |
208 |
> |
* completes parent (if one exists) exceptionally or normally. To |
209 |
|
* avoid unbounded exception loops, this method aborts if an |
210 |
< |
* exception is encountered in any <tt>onException</tt> |
210 |
> |
* exception is encountered in any {@code onException} |
211 |
|
* invocation. |
212 |
|
* @param ex the exception to throw when joining this task |
213 |
|
* @throws NullPointerException if ex is null |
214 |
|
* @throws Throwable if any invocation of |
215 |
< |
* <tt>onException</tt> does so. |
215 |
> |
* {@code onException} does so |
216 |
|
*/ |
217 |
|
public final void completeExceptionally(Throwable ex) { |
218 |
|
LinkedAsyncAction a = this; |
231 |
|
|
232 |
|
/** |
233 |
|
* Returns this task's parent, or null if none. |
234 |
< |
* @return this task's parent, or null if none. |
234 |
> |
* @return this task's parent, or null if none |
235 |
|
*/ |
236 |
|
public final LinkedAsyncAction getParent() { |
237 |
|
return parent; |
239 |
|
|
240 |
|
/** |
241 |
|
* Returns the number of subtasks that have not yet completed. |
242 |
< |
* @return the number of subtasks that have not yet completed. |
242 |
> |
* @return the number of subtasks that have not yet completed |
243 |
|
*/ |
244 |
|
public final int getPendingSubtaskCount() { |
245 |
|
return getControlState(); |
340 |
|
controlStateUpdater.decrementAndGet(this); |
341 |
|
} |
342 |
|
|
343 |
– |
|
343 |
|
} |