15 |
|
* CountedCompleter are similar to those of other completion based |
16 |
|
* components (such as {@link java.nio.channels.CompletionHandler}) |
17 |
|
* except that multiple <em>pending</em> completions may be necessary |
18 |
< |
* to trigger the completion action {@link #onCompletion}, not just one. |
18 |
> |
* to trigger the completion action {@link #onCompletion(CountedCompleter)}, |
19 |
> |
* not just one. |
20 |
|
* Unless initialized otherwise, the {@linkplain #getPendingCount pending |
21 |
|
* count} starts at zero, but may be (atomically) changed using |
22 |
|
* methods {@link #setPendingCount}, {@link #addToPendingCount}, and |
41 |
|
* <p>A concrete CountedCompleter class must define method {@link |
42 |
|
* #compute}, that should in most cases (as illustrated below), invoke |
43 |
|
* {@code tryComplete()} once before returning. The class may also |
44 |
< |
* optionally override method {@link #onCompletion} to perform an |
45 |
< |
* action upon normal completion, and method {@link |
46 |
< |
* #onExceptionalCompletion} to perform an action upon any exception. |
44 |
> |
* optionally override method {@link #onCompletion(CountedCompleter)} |
45 |
> |
* to perform an action upon normal completion, and method |
46 |
> |
* {@link #onExceptionalCompletion(Throwable, CountedCompleter)} to |
47 |
> |
* perform an action upon any exception. |
48 |
|
* |
49 |
|
* <p>CountedCompleters most often do not bear results, in which case |
50 |
|
* they are normally declared as {@code CountedCompleter<Void>}, and |
65 |
|
* only as an internal helper for other computations, so its own task |
66 |
|
* status (as reported in methods such as {@link ForkJoinTask#isDone}) |
67 |
|
* is arbitrary; this status changes only upon explicit invocations of |
68 |
< |
* {@link #complete}, {@link ForkJoinTask#cancel}, {@link |
69 |
< |
* ForkJoinTask#completeExceptionally} or upon exceptional completion |
70 |
< |
* of method {@code compute}. Upon any exceptional completion, the |
71 |
< |
* exception may be relayed to a task's completer (and its completer, |
72 |
< |
* and so on), if one exists and it has not otherwise already |
73 |
< |
* completed. Similarly, cancelling an internal CountedCompleter has |
74 |
< |
* only a local effect on that completer, so is not often useful. |
68 |
> |
* {@link #complete}, {@link ForkJoinTask#cancel}, |
69 |
> |
* {@link ForkJoinTask#completeExceptionally(Throwable)} or upon |
70 |
> |
* exceptional completion of method {@code compute}. Upon any |
71 |
> |
* exceptional completion, the exception may be relayed to a task's |
72 |
> |
* completer (and its completer, and so on), if one exists and it has |
73 |
> |
* not otherwise already completed. Similarly, cancelling an internal |
74 |
> |
* CountedCompleter has only a local effect on that completer, so is |
75 |
> |
* not often useful. |
76 |
|
* |
77 |
|
* <p><b>Sample Usages.</b> |
78 |
|
* |
155 |
|
* } |
156 |
|
* }</pre> |
157 |
|
* |
158 |
< |
* As a further improvement, notice that the left task need not even |
159 |
< |
* exist. Instead of creating a new one, we can iterate using the |
160 |
< |
* original task, and add a pending count for each fork. Additionally, |
161 |
< |
* because no task in this tree implements an {@link #onCompletion} |
162 |
< |
* method, {@code tryComplete()} can be replaced with {@link |
160 |
< |
* #propagateCompletion}. |
158 |
> |
* As a further improvement, notice that the left task need not even exist. |
159 |
> |
* Instead of creating a new one, we can iterate using the original task, |
160 |
> |
* and add a pending count for each fork. Additionally, because no task |
161 |
> |
* in this tree implements an {@link #onCompletion(CountedCompleter)} method, |
162 |
> |
* {@code tryComplete()} can be replaced with {@link #propagateCompletion}. |
163 |
|
* |
164 |
|
* <pre> {@code |
165 |
|
* class ForEach<E> ... |
237 |
|
* |
238 |
|
* <p><b>Recording subtasks.</b> CountedCompleter tasks that combine |
239 |
|
* results of multiple subtasks usually need to access these results |
240 |
< |
* in method {@link #onCompletion}. As illustrated in the following |
240 |
> |
* in method {@link #onCompletion(CountedCompleter)}. As illustrated in the following |
241 |
|
* class (that performs a simplified form of map-reduce where mappings |
242 |
|
* and reductions are all of type {@code E}), one way to do this in |
243 |
|
* divide and conquer designs is to have each subtask record its |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* Performs an action when method {@link #completeExceptionally} |
442 |
> |
* Performs an action when method {@link #completeExceptionally(Throwable)} |
443 |
|
* is invoked or method {@link #compute} throws an exception, and |
444 |
|
* this task has not otherwise already completed normally. On |
445 |
|
* entry to this method, this task {@link |
537 |
|
|
538 |
|
/** |
539 |
|
* If the pending count is nonzero, decrements the count; |
540 |
< |
* otherwise invokes {@link #onCompletion} and then similarly |
541 |
< |
* tries to complete this task's completer, if one exists, |
542 |
< |
* else marks this task as complete. |
540 |
> |
* otherwise invokes {@link #onCompletion(CountedCompleter)} |
541 |
> |
* and then similarly tries to complete this task's completer, |
542 |
> |
* if one exists, else marks this task as complete. |
543 |
|
*/ |
544 |
|
public final void tryComplete() { |
545 |
|
CountedCompleter<?> a = this, s = a; |
558 |
|
|
559 |
|
/** |
560 |
|
* Equivalent to {@link #tryComplete} but does not invoke {@link |
561 |
< |
* #onCompletion} along the completion path: If the pending count |
562 |
< |
* is nonzero, decrements the count; otherwise, similarly tries to |
563 |
< |
* complete this task's completer, if one exists, else marks this |
564 |
< |
* task as complete. This method may be useful in cases where |
565 |
< |
* {@code onCompletion} should not, or need not, be invoked for |
566 |
< |
* each completer in a computation. |
561 |
> |
* #onCompletion(CountedCompleter)} along the completion path: |
562 |
> |
* If the pending count is nonzero, decrements the count; |
563 |
> |
* otherwise, similarly tries to complete this task's completer, if |
564 |
> |
* one exists, else marks this task as complete. This method may be |
565 |
> |
* useful in cases where {@code onCompletion} should not, or need |
566 |
> |
* not, be invoked for each completer in a computation. |
567 |
|
*/ |
568 |
|
public final void propagateCompletion() { |
569 |
|
CountedCompleter<?> a = this, s = a; |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
< |
* Regardless of pending count, invokes {@link #onCompletion}, |
584 |
< |
* marks this task as complete and further triggers {@link |
585 |
< |
* #tryComplete} on this task's completer, if one exists. The |
586 |
< |
* given rawResult is used as an argument to {@link #setRawResult} |
587 |
< |
* before invoking {@link #onCompletion} or marking this task as |
588 |
< |
* complete; its value is meaningful only for classes overriding |
589 |
< |
* {@code setRawResult}. |
583 |
> |
* Regardless of pending count, invokes |
584 |
> |
* {@link #onCompletion(CountedCompleter)}, marks this task as |
585 |
> |
* complete and further triggers {@link #tryComplete} on this |
586 |
> |
* task's completer, if one exists. The given rawResult is |
587 |
> |
* used as an argument to {@link #setRawResult} before invoking |
588 |
> |
* {@link #onCompletion(CountedCompleter)} or marking this task |
589 |
> |
* as complete; its value is meaningful only for classes |
590 |
> |
* overriding {@code setRawResult}. |
591 |
|
* |
592 |
|
* <p>This method may be useful when forcing completion as soon as |
593 |
|
* any one (versus all) of several subtask results are obtained. |