22 |
|
* <p> A "main" ForkJoinTask begins execution when submitted to a |
23 |
|
* {@link ForkJoinPool}. Once started, it will usually in turn start |
24 |
|
* other subtasks. As indicated by the name of this class, many |
25 |
< |
* programs using ForkJoinTasks employ only methods <code>fork</code> |
26 |
< |
* and <code>join</code>, or derivatives such as |
27 |
< |
* <code>invokeAll</code>. However, this class also provides a number |
25 |
> |
* programs using ForkJoinTasks employ only methods {@code fork} |
26 |
> |
* and {@code join}, or derivatives such as |
27 |
> |
* {@code invokeAll}. However, this class also provides a number |
28 |
|
* of other methods that can come into play in advanced usages, as |
29 |
|
* well as extension mechanics that allow support of new forms of |
30 |
|
* fork/join processing. |
36 |
|
* operating on purely isolated objects. The primary coordination |
37 |
|
* mechanisms are {@link #fork}, that arranges asynchronous execution, |
38 |
|
* and {@link #join}, that doesn't proceed until the task's result has |
39 |
< |
* been computed. Computations should avoid <code>synchronized</code> |
39 |
> |
* been computed. Computations should avoid {@code synchronized} |
40 |
|
* methods or blocks, and should minimize other blocking |
41 |
|
* synchronization apart from joining other tasks or using |
42 |
|
* synchronizers such as Phasers that are advertised to cooperate with |
48 |
|
* performance, and the potential to indefinitely stall if the number |
49 |
|
* of threads not waiting for IO or other external synchronization |
50 |
|
* becomes exhausted. This usage restriction is in part enforced by |
51 |
< |
* not permitting checked exceptions such as <code>IOExceptions</code> |
51 |
> |
* not permitting checked exceptions such as {@code IOExceptions} |
52 |
|
* to be thrown. However, computations may still encounter unchecked |
53 |
|
* exceptions, that are rethrown to callers attempting join |
54 |
|
* them. These exceptions may additionally include |
58 |
|
* <p>The primary method for awaiting completion and extracting |
59 |
|
* results of a task is {@link #join}, but there are several variants: |
60 |
|
* The {@link Future#get} methods support interruptible and/or timed |
61 |
< |
* waits for completion and report results using <code>Future</code> |
61 |
> |
* waits for completion and report results using {@code Future} |
62 |
|
* conventions. Method {@link #helpJoin} enables callers to actively |
63 |
|
* execute other tasks while awaiting joins, which is sometimes more |
64 |
|
* efficient but only applies when all subtasks are known to be |
65 |
|
* strictly tree-structured. Method {@link #invoke} is semantically |
66 |
< |
* equivalent to <code>fork(); join()</code> but always attempts to |
66 |
> |
* equivalent to {@code fork(); join()} but always attempts to |
67 |
|
* begin execution in the current thread. The "<em>quiet</em>" forms |
68 |
|
* of these methods do not extract results or report exceptions. These |
69 |
|
* may be useful when a set of tasks are being executed, and you need |
70 |
|
* to delay processing of results or exceptions until all complete. |
71 |
< |
* Method <code>invokeAll</code> (available in multiple versions) |
71 |
> |
* Method {@code invokeAll} (available in multiple versions) |
72 |
|
* performs the most common form of parallel invocation: forking a set |
73 |
|
* of tasks and joining them all. |
74 |
|
* |
76 |
|
* Instead, you subclass one of the abstract classes that support a |
77 |
|
* particular style of fork/join processing. Normally, a concrete |
78 |
|
* ForkJoinTask subclass declares fields comprising its parameters, |
79 |
< |
* established in a constructor, and then defines a <code>compute</code> |
79 |
> |
* established in a constructor, and then defines a {@code compute} |
80 |
|
* method that somehow uses the control methods supplied by this base |
81 |
< |
* class. While these methods have <code>public</code> access (to allow |
81 |
> |
* class. While these methods have {@code public} access (to allow |
82 |
|
* instances of different task subclasses to call each others |
83 |
|
* methods), some of them may only be called from within other |
84 |
|
* ForkJoinTasks. Attempts to invoke them in other contexts result in |
85 |
|
* exceptions or errors possibly including ClassCastException. |
86 |
|
* |
87 |
< |
* <p>Most base support methods are <code>final</code> because their |
87 |
> |
* <p>Most base support methods are {@code final} because their |
88 |
|
* implementations are intrinsically tied to the underlying |
89 |
|
* lightweight task scheduling framework, and so cannot be overridden. |
90 |
|
* Developers creating new basic styles of fork/join processing should |
91 |
< |
* minimally implement <code>protected</code> methods |
92 |
< |
* <code>exec</code>, <code>setRawResult</code>, and |
93 |
< |
* <code>getRawResult</code>, while also introducing an abstract |
91 |
> |
* minimally implement {@code protected} methods |
92 |
> |
* {@code exec}, {@code setRawResult}, and |
93 |
> |
* {@code getRawResult}, while also introducing an abstract |
94 |
|
* computational method that can be implemented in its subclasses, |
95 |
< |
* possibly relying on other <code>protected</code> methods provided |
95 |
> |
* possibly relying on other {@code protected} methods provided |
96 |
|
* by this class. |
97 |
|
* |
98 |
|
* <p>ForkJoinTasks should perform relatively small amounts of |
102 |
|
* parellelism cannot improve throughput. If too small, then memory |
103 |
|
* and internal task maintenance overhead may overwhelm processing. |
104 |
|
* |
105 |
< |
* <p>ForkJoinTasks are <code>Serializable</code>, which enables them |
105 |
> |
* <p>ForkJoinTasks are {@code Serializable}, which enables them |
106 |
|
* to be used in extensions such as remote execution frameworks. It is |
107 |
|
* in general sensible to serialize tasks only before or after, but |
108 |
|
* not during execution. Serialization is not relied on during |
483 |
|
|
484 |
|
/** |
485 |
|
* Returns the result of the computation when it is ready. |
486 |
< |
* This method differs from <code>get</code> in that abnormal |
486 |
> |
* This method differs from {@code get} in that abnormal |
487 |
|
* completion results in RuntimeExceptions or Errors, not |
488 |
|
* ExecutionExceptions. |
489 |
|
* |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
< |
* Forks both tasks, returning when <code>isDone</code> holds for |
514 |
> |
* Forks both tasks, returning when {@code isDone} holds for |
515 |
|
* both of them or an exception is encountered. This method may be |
516 |
|
* invoked only from within ForkJoinTask computations. Attempts to |
517 |
|
* invoke in other contexts result in exceptions or errors |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* Forks the given tasks, returning when <code>isDone</code> holds |
531 |
> |
* Forks the given tasks, returning when {@code isDone} holds |
532 |
|
* for all of them. If any task encounters an exception, others |
533 |
|
* may be cancelled. This method may be invoked only from within |
534 |
|
* ForkJoinTask computations. Attempts to invoke in other contexts |
572 |
|
|
573 |
|
/** |
574 |
|
* Forks all tasks in the collection, returning when |
575 |
< |
* <code>isDone</code> holds for all of them. If any task |
575 |
> |
* {@code isDone} holds for all of them. If any task |
576 |
|
* encounters an exception, others may be cancelled. This method |
577 |
|
* may be invoked only from within ForkJoinTask |
578 |
|
* computations. Attempts to invoke in other contexts resul!t in |
640 |
|
/** |
641 |
|
* Asserts that the results of this task's computation will not be |
642 |
|
* used. If a cancellation occurs before atempting to execute this |
643 |
< |
* task, then execution will be suppressed, <code>isCancelled</code> |
644 |
< |
* will report true, and <code>join</code> will result in a |
645 |
< |
* <code>CancellationException</code> being thrown. Otherwise, when |
643 |
> |
* task, then execution will be suppressed, {@code isCancelled} |
644 |
> |
* will report true, and {@code join} will result in a |
645 |
> |
* {@code CancellationException} being thrown. Otherwise, when |
646 |
|
* cancellation races with completion, there are no guarantees |
647 |
< |
* about whether <code>isCancelled</code> will report true, whether |
648 |
< |
* <code>join</code> will return normally or via an exception, or |
647 |
> |
* about whether {@code isCancelled} will report true, whether |
648 |
> |
* {@code join} will return normally or via an exception, or |
649 |
|
* whether these behaviors will remain consistent upon repeated |
650 |
|
* invocation. |
651 |
|
* |
656 |
|
* <p> This method is designed to be invoked by <em>other</em> |
657 |
|
* tasks. To terminate the current task, you can just return or |
658 |
|
* throw an unchecked exception from its computation method, or |
659 |
< |
* invoke <code>completeExceptionally</code>. |
659 |
> |
* invoke {@code completeExceptionally}. |
660 |
|
* |
661 |
|
* @param mayInterruptIfRunning this value is ignored in the |
662 |
|
* default implementation because tasks are not in general |
695 |
|
/** |
696 |
|
* Completes this task abnormally, and if not already aborted or |
697 |
|
* cancelled, causes it to throw the given exception upon |
698 |
< |
* <code>join</code> and related operations. This method may be used |
698 |
> |
* {@code join} and related operations. This method may be used |
699 |
|
* to induce exceptions in asynchronous tasks, or to force |
700 |
|
* completion of tasks that would not otherwise complete. Its use |
701 |
|
* in other situations is likely to be wrong. This method is |
702 |
< |
* overridable, but overridden versions must invoke <code>super</code> |
702 |
> |
* overridable, but overridden versions must invoke {@code super} |
703 |
|
* implementation to maintain guarantees. |
704 |
|
* |
705 |
|
* @param ex the exception to throw. If this exception is |
714 |
|
|
715 |
|
/** |
716 |
|
* Completes this task, and if not already aborted or cancelled, |
717 |
< |
* returning a <code>null</code> result upon <code>join</code> and related |
717 |
> |
* returning a {@code null} result upon {@code join} and related |
718 |
|
* operations. This method may be used to provide results for |
719 |
|
* asynchronous tasks, or to provide alternative handling for |
720 |
|
* tasks that would not otherwise complete normally. Its use in |
721 |
|
* other situations is likely to be wrong. This method is |
722 |
< |
* overridable, but overridden versions must invoke <code>super</code> |
722 |
> |
* overridable, but overridden versions must invoke {@code super} |
723 |
|
* implementation to maintain guarantees. |
724 |
|
* |
725 |
|
* @param value the result value for this task. |
752 |
|
/** |
753 |
|
* Possibly executes other tasks until this task is ready, then |
754 |
|
* returns the result of the computation. This method may be more |
755 |
< |
* efficient than <code>join</code>, but is only applicable when |
755 |
> |
* efficient than {@code join}, but is only applicable when |
756 |
|
* there are no potemtial dependencies between continuation of the |
757 |
|
* current task and that of any other task that might be executed |
758 |
|
* while helping. (This usually holds for pure divide-and-conquer |
822 |
|
|
823 |
|
/** |
824 |
|
* Resets the internal bookkeeping state of this task, allowing a |
825 |
< |
* subsequent <code>fork</code>. This method allows repeated reuse of |
825 |
> |
* subsequent {@code fork}. This method allows repeated reuse of |
826 |
|
* this task, but only if reuse occurs when this task has either |
827 |
|
* never been forked, or has been forked, then completed and all |
828 |
|
* outstanding joins of this task have also completed. Effects |
893 |
|
// Extension methods |
894 |
|
|
895 |
|
/** |
896 |
< |
* Returns the result that would be returned by <code>join</code>, |
896 |
> |
* Returns the result that would be returned by {@code join}, |
897 |
|
* even if this task completed abnormally, or null if this task is |
898 |
|
* not known to have been completed. This method is designed to |
899 |
|
* aid debugging, as well as to support extensions. Its use in any |
918 |
|
* called otherwise. The return value controls whether this task |
919 |
|
* is considered to be done normally. It may return false in |
920 |
|
* asynchronous actions that require explicit invocations of |
921 |
< |
* <code>complete</code> to become joinable. It may throw exceptions |
921 |
> |
* {@code complete} to become joinable. It may throw exceptions |
922 |
|
* to indicate abnormal exit. |
923 |
|
* @return true if completed normally |
924 |
|
* @throws Error or RuntimeException if encountered during computation |
961 |
|
* queued by the current thread but not yet executed, if one is |
962 |
|
* available, or if not available, a task that was forked by some |
963 |
|
* other thread, if available. Availability may be transient, so a |
964 |
< |
* <code>null</code> result does not necessarily imply quiecence |
964 |
> |
* {@code null} result does not necessarily imply quiecence |
965 |
|
* of the pool this task is operating in. This method is designed |
966 |
|
* primarily to support extensions, and is unlikely to be useful |
967 |
|
* otherwise. This method may be invoked only from within |