12 |
|
import java.util.Collection; |
13 |
|
import java.util.Collections; |
14 |
|
import java.util.List; |
15 |
+ |
import java.util.RandomAccess; |
16 |
|
import java.util.Map; |
17 |
|
import java.util.WeakHashMap; |
18 |
|
|
55 |
|
* restriction is in part enforced by not permitting checked |
56 |
|
* exceptions such as {@code IOExceptions} to be thrown. However, |
57 |
|
* computations may still encounter unchecked exceptions, that are |
58 |
< |
* rethrown to callers attempting join them. These exceptions may |
58 |
> |
* rethrown to callers attempting to join them. These exceptions may |
59 |
|
* additionally include RejectedExecutionExceptions stemming from |
60 |
|
* internal resource exhaustion such as failure to allocate internal |
61 |
|
* task queues. |
86 |
|
* established in a constructor, and then defines a {@code compute} |
87 |
|
* method that somehow uses the control methods supplied by this base |
88 |
|
* class. While these methods have {@code public} access (to allow |
89 |
< |
* instances of different task subclasses to call each others |
89 |
> |
* instances of different task subclasses to call each other's |
90 |
|
* methods), some of them may only be called from within other |
91 |
|
* ForkJoinTasks (as may be determined using method {@link |
92 |
|
* #inForkJoinPool}). Attempts to invoke them in other contexts |
93 |
|
* result in exceptions or errors, possibly including |
94 |
|
* ClassCastException. |
95 |
|
* |
96 |
< |
* <p>Most base support methods are {@code final} because their |
97 |
< |
* implementations are intrinsically tied to the underlying |
98 |
< |
* lightweight task scheduling framework, and so cannot be overridden. |
99 |
< |
* Developers creating new basic styles of fork/join processing should |
100 |
< |
* minimally implement {@code protected} methods |
101 |
< |
* {@link #exec}, {@link #setRawResult}, and |
102 |
< |
* {@link #getRawResult}, while also introducing an abstract |
103 |
< |
* computational method that can be implemented in its subclasses, |
104 |
< |
* possibly relying on other {@code protected} methods provided |
104 |
< |
* by this class. |
96 |
> |
* <p>Most base support methods are {@code final}, to prevent |
97 |
> |
* overriding of implementations that are intrinsically tied to the |
98 |
> |
* underlying lightweight task scheduling framework. Developers |
99 |
> |
* creating new basic styles of fork/join processing should minimally |
100 |
> |
* implement {@code protected} methods {@link #exec}, {@link |
101 |
> |
* #setRawResult}, and {@link #getRawResult}, while also introducing |
102 |
> |
* an abstract computational method that can be implemented in its |
103 |
> |
* subclasses, possibly relying on other {@code protected} methods |
104 |
> |
* provided by this class. |
105 |
|
* |
106 |
|
* <p>ForkJoinTasks should perform relatively small amounts of |
107 |
< |
* computations, otherwise splitting into smaller tasks. As a very |
108 |
< |
* rough rule of thumb, a task should perform more than 100 and less |
109 |
< |
* than 10000 basic computational steps. If tasks are too big, then |
110 |
< |
* parallelism cannot improve throughput. If too small, then memory |
111 |
< |
* and internal task maintenance overhead may overwhelm processing. |
107 |
> |
* computation. Large tasks should be split into smaller subtasks, |
108 |
> |
* usually via recursive decomposition. As a very rough rule of thumb, |
109 |
> |
* a task should perform more than 100 and less than 10000 basic |
110 |
> |
* computational steps. If tasks are too big, then parallelism cannot |
111 |
> |
* improve throughput. If too small, then memory and internal task |
112 |
> |
* maintenance overhead may overwhelm processing. |
113 |
|
* |
114 |
|
* <p>This class provides {@code adapt} methods for {@link |
115 |
|
* java.lang.Runnable} and {@link java.util.concurrent.Callable}, that |
117 |
|
* kinds of tasks. When all tasks are of this form, consider using a |
118 |
|
* pool in {@link ForkJoinPool#setAsyncMode}. |
119 |
|
* |
120 |
< |
* <p>ForkJoinTasks are {@code Serializable}, which enables them |
121 |
< |
* to be used in extensions such as remote execution frameworks. It is |
122 |
< |
* in general sensible to serialize tasks only before or after, but |
123 |
< |
* not during execution. Serialization is not relied on during |
123 |
< |
* execution itself. |
120 |
> |
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
121 |
> |
* used in extensions such as remote execution frameworks. It is |
122 |
> |
* sensible to serialize tasks only before or after, but not during, |
123 |
> |
* execution. Serialization is not relied on during execution itself. |
124 |
|
* |
125 |
|
* @since 1.7 |
126 |
|
* @author Doug Lea |
575 |
|
* result in exceptions or errors, possibly including {@code |
576 |
|
* ClassCastException}. |
577 |
|
* |
578 |
– |
* <p>Overloadings of this method exist for the special cases |
579 |
– |
* of one to four arguments. |
580 |
– |
* |
578 |
|
* @param tasks the tasks |
579 |
|
* @throws NullPointerException if tasks or any element are null |
580 |
|
* @throws RuntimeException or Error if any task did so |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
< |
* Forks all tasks in the collection, returning when {@code |
617 |
< |
* isDone} holds for each task or an exception is encountered. |
618 |
< |
* If any task encounters an exception, others may be, but are |
619 |
< |
* not guaranteed to be, cancelled. |
616 |
> |
* Forks all tasks in the specified collection, returning when |
617 |
> |
* {@code isDone} holds for each task or an exception is |
618 |
> |
* encountered. If any task encounters an exception, others may |
619 |
> |
* be, but are not guaranteed to be, cancelled. The behavior of |
620 |
> |
* this operation is undefined if the specified collection is |
621 |
> |
* modified while the operation is in progress. |
622 |
|
* |
623 |
|
* <p>This method may be invoked only from within {@code |
624 |
|
* ForkJoinTask} computations (as may be determined using method |
632 |
|
* @throws RuntimeException or Error if any task did so |
633 |
|
*/ |
634 |
|
public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) { |
635 |
< |
if (!(tasks instanceof List<?>)) { |
635 |
> |
if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) { |
636 |
|
invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()])); |
637 |
|
return tasks; |
638 |
|
} |
831 |
|
} |
832 |
|
|
833 |
|
/** |
834 |
< |
* Possibly executes other tasks until this task is ready. |
834 |
> |
* Possibly executes other tasks until this task is ready. This |
835 |
> |
* method may be useful when processing collections of tasks when |
836 |
> |
* some have been cancelled or otherwise known to have aborted. |
837 |
|
* |
838 |
|
* <p>This method may be invoked only from within {@code |
839 |
|
* ForkJoinTask} computations (as may be determined using method |