13 |
|
import java.lang.reflect.*; |
14 |
|
|
15 |
|
/** |
16 |
< |
* Abstract base class for tasks that run within a ForkJoinPool. A |
17 |
< |
* ForkJoinTask is a thread-like entity that is much lighter weight |
18 |
< |
* than a normal thread. Huge numbers of tasks and subtasks may be |
19 |
< |
* hosted by a small number of actual threads in a ForkJoinPool, |
20 |
< |
* at the price of some usage limitations. |
16 |
> |
* Abstract base class for tasks that run within a {@link |
17 |
> |
* ForkJoinPool}. A ForkJoinTask is a thread-like entity that is much |
18 |
> |
* lighter weight than a normal thread. Huge numbers of tasks and |
19 |
> |
* subtasks may be hosted by a small number of actual threads in a |
20 |
> |
* ForkJoinPool, at the price of some usage limitations. |
21 |
|
* |
22 |
< |
* <p> ForkJoinTasks are forms of <tt>Futures</tt> supporting a |
23 |
< |
* limited range of use. The "lightness" of ForkJoinTasks is due to a |
24 |
< |
* set of restrictions (that are only partially statically |
25 |
< |
* enforceable) reflecting their intended use as computational tasks |
26 |
< |
* calculating pure functions or operating on purely isolated objects. |
27 |
< |
* The primary coordination mechanisms supported for ForkJoinTasks are |
28 |
< |
* <tt>fork</tt>, that arranges asynchronous execution, and |
29 |
< |
* <tt>join</tt>, that doesn't proceed until the task's result has |
30 |
< |
* been computed. (Cancellation is also supported). The computation |
31 |
< |
* defined in the <tt>compute</tt> method should avoid |
32 |
< |
* <tt>synchronized</tt> methods or blocks, and should minimize |
33 |
< |
* blocking synchronization apart from joining other tasks or using |
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} |
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. |
31 |
> |
* |
32 |
> |
* <p>A ForkJoinTask is a lightweight form of {@link Future}. The |
33 |
> |
* efficiency of ForkJoinTasks stems from a set of restrictions (that |
34 |
> |
* are only partially statically enforceable) reflecting their |
35 |
> |
* intended use as computational tasks calculating pure functions or |
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} |
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 |
43 |
|
* fork/join scheduling. Tasks should also not perform blocking IO, |
44 |
|
* and should ideally access variables that are completely independent |
46 |
|
* restrictions, for example using shared output streams, may be |
47 |
|
* tolerable in practice, but frequent use may result in poor |
48 |
|
* performance, and the potential to indefinitely stall if the number |
49 |
< |
* of threads not waiting for external synchronization becomes |
50 |
< |
* exhausted. This usage restriction is in part enforced by not |
51 |
< |
* permitting checked exceptions such as IOExceptions to be |
52 |
< |
* thrown. However, computations may still encounter unchecked |
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} |
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 |
55 |
|
* RejectedExecutionExceptions stemming from internal resource |
56 |
|
* exhaustion such as failure to allocate internal task queues. |
57 |
|
* |
58 |
< |
* <p> The <tt>ForkJoinTask</tt> class is not usually directly |
59 |
< |
* subclassed. Instead, you subclass one of the abstract classes that |
60 |
< |
* support different styles of fork/join processing. Normally, a |
61 |
< |
* concrete ForkJoinTask subclass declares fields comprising its |
62 |
< |
* parameters, established in a constructor, and then defines a |
63 |
< |
* <tt>compute</tt> method that somehow uses the control methods |
64 |
< |
* supplied by this base class. While these methods have |
65 |
< |
* <tt>public</tt> access, some of them may only be called from within |
66 |
< |
* other ForkJoinTasks. Attempts to invoke them in other contexts |
67 |
< |
* result in exceptions or errors including ClassCastException. The |
68 |
< |
* only way to invoke a "main" driver task is to submit it to a |
69 |
< |
* ForkJoinPool. Once started, this will usually in turn start other |
70 |
< |
* subtasks. |
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} |
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()} 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} (available in multiple versions) |
72 |
> |
* performs the most common form of parallel invocation: forking a set |
73 |
> |
* of tasks and joining them all. |
74 |
> |
* |
75 |
> |
* <p> The ForkJoinTask class is not usually directly subclassed. |
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} |
80 |
> |
* method that somehow uses the control methods supplied by this base |
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 <tt>final</tt> 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 protected methods <tt>exec</tt>, |
92 |
< |
* <tt>setRawResult</tt>, and <tt>getRawResult</tt>, while also |
93 |
< |
* introducing an abstract computational method that can be |
94 |
< |
* implemented in its subclasses. To support such extensions, |
95 |
< |
* instances of ForkJoinTasks maintain an atomically updated |
96 |
< |
* <tt>short</tt> representing user-defined control state. Control |
74 |
< |
* state is guaranteed initially to be zero, and to be negative upon |
75 |
< |
* completion, but may otherwise be used for any other control |
76 |
< |
* purposes, such as maintaining join counts. The {@link |
77 |
< |
* ForkJoinWorkerThread} class supports additional inspection and |
78 |
< |
* tuning methods that can be useful when developing extensions. |
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} methods provided |
96 |
> |
* by this class. |
97 |
|
* |
98 |
|
* <p>ForkJoinTasks should perform relatively small amounts of |
99 |
< |
* computations, othewise splitting into smaller tasks. As a very |
99 |
> |
* computations, otherwise splitting into smaller tasks. As a very |
100 |
|
* rough rule of thumb, a task should perform more than 100 and less |
101 |
|
* than 10000 basic computational steps. If tasks are too big, then |
102 |
< |
* parellelism cannot improve throughput. If too small, then memory |
102 |
> |
* parallelism cannot improve throughput. If too small, then memory |
103 |
|
* and internal task maintenance overhead may overwhelm processing. |
104 |
|
* |
105 |
< |
* <p>ForkJoinTasks are <tt>Serializable</tt>, which enables them to |
106 |
< |
* be used in extensions such as remote execution frameworks. However, |
107 |
< |
* it is in general safe to serialize tasks only before or after, but |
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 |
109 |
|
* execution itself. |
110 |
|
*/ |
111 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
112 |
+ |
|
113 |
|
/** |
114 |
< |
* Status field holding all run status. We pack this into a single |
115 |
< |
* int both to minimize footprint overhead and to ensure atomicity |
116 |
< |
* (updates are via CAS). |
98 |
< |
* |
99 |
< |
* Status is initially zero, and takes on nonnegative values until |
114 |
> |
* Run control status bits packed into a single int to minimize |
115 |
> |
* footprint and to ensure atomicity (via CAS). Status is |
116 |
> |
* initially zero, and takes on nonnegative values until |
117 |
|
* completed, upon which status holds COMPLETED. CANCELLED, or |
118 |
|
* EXCEPTIONAL, which use the top 3 bits. Tasks undergoing |
119 |
|
* blocking waits by other threads have SIGNAL_MASK bits set -- |
128 |
|
* currently unused. Also value 0x80000000 is available as spare |
129 |
|
* completion value. |
130 |
|
*/ |
131 |
< |
volatile int status; // accessed directy by pool and workers |
131 |
> |
volatile int status; // accessed directly by pool and workers |
132 |
|
|
133 |
|
static final int COMPLETION_MASK = 0xe0000000; |
134 |
|
static final int NORMAL = 0xe0000000; // == mask |
141 |
|
/** |
142 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
143 |
|
* callers. Because exceptions are rare, we don't directly keep |
144 |
< |
* them with task objects, but instead us a weak ref table. Note |
144 |
> |
* them with task objects, but instead use a weak ref table. Note |
145 |
|
* that cancellation exceptions don't appear in the table, but are |
146 |
|
* instead recorded as status values. |
147 |
< |
* Todo: Use ConcurrentReferenceHashMap |
147 |
> |
* TODO: Use ConcurrentReferenceHashMap |
148 |
|
*/ |
149 |
|
static final Map<ForkJoinTask<?>, Throwable> exceptionMap = |
150 |
|
Collections.synchronizedMap |
153 |
|
// within-package utilities |
154 |
|
|
155 |
|
/** |
156 |
< |
* Get current worker thread, or null if not a worker thread |
156 |
> |
* Gets current worker thread, or null if not a worker thread. |
157 |
|
*/ |
158 |
|
static ForkJoinWorkerThread getWorker() { |
159 |
|
Thread t = Thread.currentThread(); |
161 |
|
(ForkJoinWorkerThread)t : null); |
162 |
|
} |
163 |
|
|
147 |
– |
/** |
148 |
– |
* Get pool of current worker thread, or null if not a worker thread |
149 |
– |
*/ |
150 |
– |
static ForkJoinPool getWorkerPool() { |
151 |
– |
Thread t = Thread.currentThread(); |
152 |
– |
return ((t instanceof ForkJoinWorkerThread)? |
153 |
– |
((ForkJoinWorkerThread)t).pool : null); |
154 |
– |
} |
155 |
– |
|
164 |
|
final boolean casStatus(int cmp, int val) { |
165 |
< |
return _unsafe.compareAndSwapInt(this, statusOffset, cmp, val); |
165 |
> |
return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
170 |
|
*/ |
171 |
|
static void rethrowException(Throwable ex) { |
172 |
|
if (ex != null) |
173 |
< |
_unsafe.throwException(ex); |
173 |
> |
UNSAFE.throwException(ex); |
174 |
|
} |
175 |
|
|
176 |
|
// Setting completion status |
177 |
|
|
178 |
|
/** |
179 |
< |
* Mark completion and wake up threads waiting to join this task. |
179 |
> |
* Marks completion and wakes up threads waiting to join this task. |
180 |
> |
* |
181 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
182 |
|
*/ |
183 |
|
final void setCompletion(int completion) { |
184 |
< |
ForkJoinPool pool = getWorkerPool(); |
184 |
> |
ForkJoinPool pool = getPool(); |
185 |
|
if (pool != null) { |
186 |
|
int s; // Clear signal bits while setting completion status |
187 |
|
do;while ((s = status) >= 0 && !casStatus(s, completion)); |
213 |
|
final void setNormalCompletion() { |
214 |
|
// Try typical fast case -- single CAS, no signal, not already done. |
215 |
|
// Manually expand casStatus to improve chances of inlining it |
216 |
< |
if (!_unsafe.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
216 |
> |
if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
217 |
|
setCompletion(NORMAL); |
218 |
|
} |
219 |
|
|
256 |
|
/** |
257 |
|
* Sets status to indicate there is joiner, then waits for join, |
258 |
|
* surrounded with pool notifications. |
259 |
+ |
* |
260 |
|
* @return status upon exit |
261 |
|
*/ |
262 |
< |
final int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) { |
262 |
> |
private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) { |
263 |
|
ForkJoinPool pool = w == null? null : w.pool; |
264 |
|
int s; |
265 |
|
while ((s = status) >= 0) { |
278 |
|
* Timed version of awaitDone |
279 |
|
* @return status upon exit |
280 |
|
*/ |
281 |
< |
final int awaitDone(ForkJoinWorkerThread w, long nanos) { |
281 |
> |
private int awaitDone(ForkJoinWorkerThread w, long nanos) { |
282 |
|
ForkJoinPool pool = w == null? null : w.pool; |
283 |
|
int s; |
284 |
|
while ((s = status) >= 0) { |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
< |
* Notify pool that thread is unblocked. Called by signalled |
302 |
> |
* Notifies pool that thread is unblocked. Called by signalled |
303 |
|
* threads when woken by non-FJ threads (which is atypical). |
304 |
|
*/ |
305 |
|
private void adjustPoolCountsOnUnblock(ForkJoinPool pool) { |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* Notify pool to adjust counts on cancelled or timed out wait |
313 |
> |
* Notifies pool to adjust counts on cancelled or timed out wait. |
314 |
|
*/ |
315 |
|
private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) { |
316 |
|
if (pool != null) { |
324 |
|
} |
325 |
|
} |
326 |
|
|
327 |
+ |
/** |
328 |
+ |
* Handles interruptions during waits. |
329 |
+ |
*/ |
330 |
|
private void onInterruptedWait() { |
331 |
< |
Thread t = Thread.currentThread(); |
332 |
< |
if (t instanceof ForkJoinWorkerThread) { |
333 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread)t; |
334 |
< |
if (w.isTerminating()) |
335 |
< |
cancelIgnoreExceptions(); |
336 |
< |
} |
324 |
< |
else { // re-interrupt |
325 |
< |
try { |
326 |
< |
t.interrupt(); |
327 |
< |
} catch (SecurityException ignore) { |
328 |
< |
} |
329 |
< |
} |
331 |
> |
ForkJoinWorkerThread w = getWorker(); |
332 |
> |
if (w == null) |
333 |
> |
Thread.currentThread().interrupt(); // re-interrupt |
334 |
> |
else if (w.isTerminating()) |
335 |
> |
cancelIgnoringExceptions(); |
336 |
> |
// else if FJworker, ignore interrupt |
337 |
|
} |
338 |
|
|
339 |
|
// Recording and reporting exceptions |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* Throws the exception associated with status s; |
347 |
> |
* Throws the exception associated with status s. |
348 |
> |
* |
349 |
|
* @throws the exception |
350 |
|
*/ |
351 |
|
private void reportException(int s) { |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
< |
* Returns result or throws exception using j.u.c.Future conventions |
361 |
> |
* Returns result or throws exception using j.u.c.Future conventions. |
362 |
|
* Only call when isDone known to be true. |
363 |
|
*/ |
364 |
|
private V reportFutureResult() |
378 |
|
|
379 |
|
/** |
380 |
|
* Returns result or throws exception using j.u.c.Future conventions |
381 |
< |
* with timeouts |
381 |
> |
* with timeouts. |
382 |
|
*/ |
383 |
|
private V reportTimedFutureResult() |
384 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
399 |
|
|
400 |
|
/** |
401 |
|
* Calls exec, recording completion, and rethrowing exception if |
402 |
< |
* encountered. Caller should normally check status before calling |
402 |
> |
* encountered. Caller should normally check status before calling. |
403 |
> |
* |
404 |
|
* @return true if completed normally |
405 |
|
*/ |
406 |
|
private boolean tryExec() { |
418 |
|
|
419 |
|
/** |
420 |
|
* Main execution method used by worker threads. Invokes |
421 |
< |
* base computation unless already complete |
421 |
> |
* base computation unless already complete. |
422 |
|
*/ |
423 |
|
final void quietlyExec() { |
424 |
|
if (status >= 0) { |
434 |
|
} |
435 |
|
|
436 |
|
/** |
437 |
< |
* Calls exec, recording but not rethrowing exception |
438 |
< |
* Caller should normally check status before calling |
437 |
> |
* Calls exec(), recording but not rethrowing exception. |
438 |
> |
* Caller should normally check status before calling. |
439 |
> |
* |
440 |
|
* @return true if completed normally |
441 |
|
*/ |
442 |
|
private boolean tryQuietlyInvoke() { |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
< |
* Cancel, ignoring any exceptions it throws |
455 |
> |
* Cancels, ignoring any exceptions it throws. |
456 |
|
*/ |
457 |
< |
final void cancelIgnoreExceptions() { |
457 |
> |
final void cancelIgnoringExceptions() { |
458 |
|
try { |
459 |
|
cancel(false); |
460 |
|
} catch(Throwable ignore) { |
461 |
|
} |
462 |
|
} |
463 |
|
|
464 |
+ |
/** |
465 |
+ |
* Main implementation of helpJoin |
466 |
+ |
*/ |
467 |
+ |
private int busyJoin(ForkJoinWorkerThread w) { |
468 |
+ |
int s; |
469 |
+ |
ForkJoinTask<?> t; |
470 |
+ |
while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) |
471 |
+ |
t.quietlyExec(); |
472 |
+ |
return (s >= 0)? awaitDone(w, false) : s; // block if no work |
473 |
+ |
} |
474 |
+ |
|
475 |
|
// public methods |
476 |
|
|
477 |
|
/** |
478 |
|
* Arranges to asynchronously execute this task. While it is not |
479 |
|
* necessarily enforced, it is a usage error to fork a task more |
480 |
|
* than once unless it has completed and been reinitialized. This |
481 |
< |
* method may be invoked only from within other ForkJoinTask |
481 |
> |
* method may be invoked only from within ForkJoinTask |
482 |
|
* computations. Attempts to invoke in other contexts result in |
483 |
< |
* exceptions or errors including ClassCastException. |
483 |
> |
* exceptions or errors possibly including ClassCastException. |
484 |
|
*/ |
485 |
|
public final void fork() { |
486 |
|
((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this); |
488 |
|
|
489 |
|
/** |
490 |
|
* Returns the result of the computation when it is ready. |
491 |
< |
* This method differs from <tt>get</tt> in that abnormal |
491 |
> |
* This method differs from {@code get} in that abnormal |
492 |
|
* completion results in RuntimeExceptions or Errors, not |
493 |
|
* ExecutionExceptions. |
494 |
|
* |
501 |
|
return getRawResult(); |
502 |
|
} |
503 |
|
|
483 |
– |
public final V get() throws InterruptedException, ExecutionException { |
484 |
– |
ForkJoinWorkerThread w = getWorker(); |
485 |
– |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
486 |
– |
awaitDone(w, true); |
487 |
– |
return reportFutureResult(); |
488 |
– |
} |
489 |
– |
|
490 |
– |
public final V get(long timeout, TimeUnit unit) |
491 |
– |
throws InterruptedException, ExecutionException, TimeoutException { |
492 |
– |
ForkJoinWorkerThread w = getWorker(); |
493 |
– |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
494 |
– |
awaitDone(w, unit.toNanos(timeout)); |
495 |
– |
return reportTimedFutureResult(); |
496 |
– |
} |
497 |
– |
|
504 |
|
/** |
505 |
< |
* Possibly executes other tasks until this task is ready, then |
506 |
< |
* returns the result of the computation. This method may be more |
507 |
< |
* efficient than <tt>join</tt>, but is only applicable when there |
502 |
< |
* are no potemtial dependencies between continuation of the |
503 |
< |
* current task and that of any other task that might be executed |
504 |
< |
* while helping. (This usually holds for pure divide-and-conquer |
505 |
< |
* tasks). |
506 |
< |
* @return the computed result |
507 |
< |
*/ |
508 |
< |
public final V helpJoin() { |
509 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); |
510 |
< |
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
511 |
< |
reportException(w.helpJoinTask(this)); |
512 |
< |
return getRawResult(); |
513 |
< |
} |
514 |
< |
|
515 |
< |
/** |
516 |
< |
* Performs this task, awaits its completion if necessary, and |
517 |
< |
* return its result. |
505 |
> |
* Commences performing this task, awaits its completion if |
506 |
> |
* necessary, and return its result. |
507 |
> |
* |
508 |
|
* @throws Throwable (a RuntimeException, Error, or unchecked |
509 |
< |
* exception) if the underlying computation did so. |
509 |
> |
* exception) if the underlying computation did so |
510 |
|
* @return the computed result |
511 |
|
*/ |
512 |
|
public final V invoke() { |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
< |
* Joins this task, without returning its result or throwing an |
521 |
< |
* exception. This method may be useful when processing |
522 |
< |
* collections of tasks when some have been cancelled or otherwise |
523 |
< |
* known to have aborted. |
520 |
> |
* Forks both tasks, returning when {@code isDone} holds for |
521 |
> |
* both of them or an exception is encountered. This method may be |
522 |
> |
* invoked only from within ForkJoinTask computations. Attempts to |
523 |
> |
* invoke in other contexts result in exceptions or errors |
524 |
> |
* possibly including ClassCastException. |
525 |
> |
* |
526 |
> |
* @param t1 one task |
527 |
> |
* @param t2 the other task |
528 |
> |
* @throws NullPointerException if t1 or t2 are null |
529 |
> |
* @throws RuntimeException or Error if either task did so |
530 |
|
*/ |
531 |
< |
public final void quietlyJoin() { |
532 |
< |
if (status >= 0) { |
533 |
< |
ForkJoinWorkerThread w = getWorker(); |
534 |
< |
if (w == null || !w.unpushTask(this) || !tryQuietlyInvoke()) |
539 |
< |
awaitDone(w, true); |
540 |
< |
} |
531 |
> |
public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) { |
532 |
> |
t2.fork(); |
533 |
> |
t1.invoke(); |
534 |
> |
t2.join(); |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
< |
* Possibly executes other tasks until this task is ready. |
538 |
> |
* Forks the given tasks, returning when {@code isDone} holds |
539 |
> |
* for all of them. If any task encounters an exception, others |
540 |
> |
* may be cancelled. This method may be invoked only from within |
541 |
> |
* ForkJoinTask computations. Attempts to invoke in other contexts |
542 |
> |
* result in exceptions or errors possibly including ClassCastException. |
543 |
> |
* |
544 |
> |
* @param tasks the array of tasks |
545 |
> |
* @throws NullPointerException if tasks or any element are null |
546 |
> |
* @throws RuntimeException or Error if any task did so |
547 |
|
*/ |
548 |
< |
public final void quietlyHelpJoin() { |
549 |
< |
if (status >= 0) { |
550 |
< |
ForkJoinWorkerThread w = |
551 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
552 |
< |
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
553 |
< |
w.helpJoinTask(this); |
548 |
> |
public static void invokeAll(ForkJoinTask<?>... tasks) { |
549 |
> |
Throwable ex = null; |
550 |
> |
int last = tasks.length - 1; |
551 |
> |
for (int i = last; i >= 0; --i) { |
552 |
> |
ForkJoinTask<?> t = tasks[i]; |
553 |
> |
if (t == null) { |
554 |
> |
if (ex == null) |
555 |
> |
ex = new NullPointerException(); |
556 |
> |
} |
557 |
> |
else if (i != 0) |
558 |
> |
t.fork(); |
559 |
> |
else { |
560 |
> |
t.quietlyInvoke(); |
561 |
> |
if (ex == null) |
562 |
> |
ex = t.getException(); |
563 |
> |
} |
564 |
|
} |
565 |
+ |
for (int i = 1; i <= last; ++i) { |
566 |
+ |
ForkJoinTask<?> t = tasks[i]; |
567 |
+ |
if (t != null) { |
568 |
+ |
if (ex != null) |
569 |
+ |
t.cancel(false); |
570 |
+ |
else { |
571 |
+ |
t.quietlyJoin(); |
572 |
+ |
if (ex == null) |
573 |
+ |
ex = t.getException(); |
574 |
+ |
} |
575 |
+ |
} |
576 |
+ |
} |
577 |
+ |
if (ex != null) |
578 |
+ |
rethrowException(ex); |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
< |
* Performs this task and awaits its completion if necessary, |
583 |
< |
* without returning its result or throwing an exception. This |
584 |
< |
* method may be useful when processing collections of tasks when |
585 |
< |
* some have been cancelled or otherwise known to have aborted. |
582 |
> |
* Forks all tasks in the collection, returning when |
583 |
> |
* {@code isDone} holds for all of them. If any task |
584 |
> |
* encounters an exception, others may be cancelled. This method |
585 |
> |
* may be invoked only from within ForkJoinTask |
586 |
> |
* computations. Attempts to invoke in other contexts result in |
587 |
> |
* exceptions or errors possibly including ClassCastException. |
588 |
> |
* |
589 |
> |
* @param tasks the collection of tasks |
590 |
> |
* @throws NullPointerException if tasks or any element are null |
591 |
> |
* @throws RuntimeException or Error if any task did so |
592 |
|
*/ |
593 |
< |
public final void quietlyInvoke() { |
594 |
< |
if (status >= 0 && !tryQuietlyInvoke()) |
595 |
< |
quietlyJoin(); |
593 |
> |
public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) { |
594 |
> |
if (!(tasks instanceof List)) { |
595 |
> |
invokeAll(tasks.toArray(new ForkJoinTask[tasks.size()])); |
596 |
> |
return; |
597 |
> |
} |
598 |
> |
List<? extends ForkJoinTask<?>> ts = |
599 |
> |
(List<? extends ForkJoinTask<?>>)tasks; |
600 |
> |
Throwable ex = null; |
601 |
> |
int last = ts.size() - 1; |
602 |
> |
for (int i = last; i >= 0; --i) { |
603 |
> |
ForkJoinTask<?> t = ts.get(i); |
604 |
> |
if (t == null) { |
605 |
> |
if (ex == null) |
606 |
> |
ex = new NullPointerException(); |
607 |
> |
} |
608 |
> |
else if (i != 0) |
609 |
> |
t.fork(); |
610 |
> |
else { |
611 |
> |
t.quietlyInvoke(); |
612 |
> |
if (ex == null) |
613 |
> |
ex = t.getException(); |
614 |
> |
} |
615 |
> |
} |
616 |
> |
for (int i = 1; i <= last; ++i) { |
617 |
> |
ForkJoinTask<?> t = ts.get(i); |
618 |
> |
if (t != null) { |
619 |
> |
if (ex != null) |
620 |
> |
t.cancel(false); |
621 |
> |
else { |
622 |
> |
t.quietlyJoin(); |
623 |
> |
if (ex == null) |
624 |
> |
ex = t.getException(); |
625 |
> |
} |
626 |
> |
} |
627 |
> |
} |
628 |
> |
if (ex != null) |
629 |
> |
rethrowException(ex); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
633 |
|
* Returns true if the computation performed by this task has |
634 |
|
* completed (or has been cancelled). |
635 |
+ |
* |
636 |
|
* @return true if this computation has completed |
637 |
|
*/ |
638 |
|
public final boolean isDone() { |
641 |
|
|
642 |
|
/** |
643 |
|
* Returns true if this task was cancelled. |
644 |
+ |
* |
645 |
|
* @return true if this task was cancelled |
646 |
|
*/ |
647 |
|
public final boolean isCancelled() { |
649 |
|
} |
650 |
|
|
651 |
|
/** |
584 |
– |
* Returns true if this task threw an exception or was cancelled |
585 |
– |
* @return true if this task threw an exception or was cancelled |
586 |
– |
*/ |
587 |
– |
public final boolean completedAbnormally() { |
588 |
– |
return (status & COMPLETION_MASK) < NORMAL; |
589 |
– |
} |
590 |
– |
|
591 |
– |
/** |
592 |
– |
* Returns the exception thrown by the base computation, or a |
593 |
– |
* CancellationException if cancelled, or null if none or if the |
594 |
– |
* method has not yet completed. |
595 |
– |
* @return the exception, or null if none |
596 |
– |
*/ |
597 |
– |
public final Throwable getException() { |
598 |
– |
int s = status & COMPLETION_MASK; |
599 |
– |
if (s >= NORMAL) |
600 |
– |
return null; |
601 |
– |
if (s == CANCELLED) |
602 |
– |
return new CancellationException(); |
603 |
– |
return exceptionMap.get(this); |
604 |
– |
} |
605 |
– |
|
606 |
– |
/** |
652 |
|
* Asserts that the results of this task's computation will not be |
653 |
< |
* used. If a cancellation occurs before this task is processed, |
654 |
< |
* then its <tt>compute</tt> method will not be executed, |
655 |
< |
* <tt>isCancelled</tt> will report true, and <tt>join</tt> will |
656 |
< |
* result in a CancellationException being thrown. Otherwise, when |
653 |
> |
* used. If a cancellation occurs before attempting to execute this |
654 |
> |
* task, then execution will be suppressed, {@code isCancelled} |
655 |
> |
* will report true, and {@code join} will result in a |
656 |
> |
* {@code CancellationException} being thrown. Otherwise, when |
657 |
|
* cancellation races with completion, there are no guarantees |
658 |
< |
* about whether <tt>isCancelled</tt> will report true, whether |
659 |
< |
* <tt>join</tt> will return normally or via an exception, or |
658 |
> |
* about whether {@code isCancelled} will report true, whether |
659 |
> |
* {@code join} will return normally or via an exception, or |
660 |
|
* whether these behaviors will remain consistent upon repeated |
661 |
|
* invocation. |
662 |
|
* |
667 |
|
* <p> This method is designed to be invoked by <em>other</em> |
668 |
|
* tasks. To terminate the current task, you can just return or |
669 |
|
* throw an unchecked exception from its computation method, or |
670 |
< |
* invoke <tt>completeExceptionally(someException)</tt>. |
670 |
> |
* invoke {@code completeExceptionally}. |
671 |
|
* |
672 |
|
* @param mayInterruptIfRunning this value is ignored in the |
673 |
|
* default implementation because tasks are not in general |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
+ |
* Returns true if this task threw an exception or was cancelled. |
685 |
+ |
* |
686 |
+ |
* @return true if this task threw an exception or was cancelled |
687 |
+ |
*/ |
688 |
+ |
public final boolean isCompletedAbnormally() { |
689 |
+ |
return (status & COMPLETION_MASK) < NORMAL; |
690 |
+ |
} |
691 |
+ |
|
692 |
+ |
/** |
693 |
+ |
* Returns the exception thrown by the base computation, or a |
694 |
+ |
* CancellationException if cancelled, or null if none or if the |
695 |
+ |
* method has not yet completed. |
696 |
+ |
* |
697 |
+ |
* @return the exception, or null if none |
698 |
+ |
*/ |
699 |
+ |
public final Throwable getException() { |
700 |
+ |
int s = status & COMPLETION_MASK; |
701 |
+ |
if (s >= NORMAL) |
702 |
+ |
return null; |
703 |
+ |
if (s == CANCELLED) |
704 |
+ |
return new CancellationException(); |
705 |
+ |
return exceptionMap.get(this); |
706 |
+ |
} |
707 |
+ |
|
708 |
+ |
/** |
709 |
|
* Completes this task abnormally, and if not already aborted or |
710 |
|
* cancelled, causes it to throw the given exception upon |
711 |
< |
* <tt>join</tt> and related operations. This method may be used |
711 |
> |
* {@code join} and related operations. This method may be used |
712 |
|
* to induce exceptions in asynchronous tasks, or to force |
713 |
< |
* completion of tasks that would not otherwise complete. This |
714 |
< |
* method is overridable, but overridden versions must invoke |
715 |
< |
* <tt>super</tt> implementation to maintain guarantees. |
713 |
> |
* completion of tasks that would not otherwise complete. Its use |
714 |
> |
* in other situations is likely to be wrong. This method is |
715 |
> |
* overridable, but overridden versions must invoke {@code super} |
716 |
> |
* implementation to maintain guarantees. |
717 |
> |
* |
718 |
|
* @param ex the exception to throw. If this exception is |
719 |
|
* not a RuntimeException or Error, the actual exception thrown |
720 |
|
* will be a RuntimeException with cause ex. |
727 |
|
|
728 |
|
/** |
729 |
|
* Completes this task, and if not already aborted or cancelled, |
730 |
< |
* returning a <tt>null</tt> result upon <tt>join</tt> and related |
730 |
> |
* returning a {@code null} result upon {@code join} and related |
731 |
|
* operations. This method may be used to provide results for |
732 |
|
* asynchronous tasks, or to provide alternative handling for |
733 |
< |
* tasks that would not otherwise complete normally. |
733 |
> |
* tasks that would not otherwise complete normally. Its use in |
734 |
> |
* other situations is likely to be wrong. This method is |
735 |
> |
* overridable, but overridden versions must invoke {@code super} |
736 |
> |
* implementation to maintain guarantees. |
737 |
|
* |
738 |
< |
* @param value the result value for this task. |
738 |
> |
* @param value the result value for this task |
739 |
|
*/ |
740 |
|
public void complete(V value) { |
741 |
|
try { |
747 |
|
setNormalCompletion(); |
748 |
|
} |
749 |
|
|
750 |
< |
/** |
751 |
< |
* Resets the internal bookkeeping state of this task, allowing a |
752 |
< |
* subsequent <tt>fork</tt>. This method allows repeated reuse of |
753 |
< |
* this task, but only if reuse occurs when this task has either |
754 |
< |
* never been forked, or has been forked, then completed and all |
755 |
< |
* outstanding joins of this task have also completed. Effects |
756 |
< |
* under any other usage conditions are not guaranteed, and are |
757 |
< |
* almost surely wrong. This method may be useful when executing |
758 |
< |
* pre-constructed trees of subtasks in loops. |
759 |
< |
*/ |
760 |
< |
public void reinitialize() { |
761 |
< |
if ((status & COMPLETION_MASK) == EXCEPTIONAL) |
762 |
< |
exceptionMap.remove(this); |
688 |
< |
status = 0; |
750 |
> |
public final V get() throws InterruptedException, ExecutionException { |
751 |
> |
ForkJoinWorkerThread w = getWorker(); |
752 |
> |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
753 |
> |
awaitDone(w, true); |
754 |
> |
return reportFutureResult(); |
755 |
> |
} |
756 |
> |
|
757 |
> |
public final V get(long timeout, TimeUnit unit) |
758 |
> |
throws InterruptedException, ExecutionException, TimeoutException { |
759 |
> |
ForkJoinWorkerThread w = getWorker(); |
760 |
> |
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
761 |
> |
awaitDone(w, unit.toNanos(timeout)); |
762 |
> |
return reportTimedFutureResult(); |
763 |
|
} |
764 |
|
|
765 |
|
/** |
766 |
< |
* Tries to unschedule this task for execution. This method will |
767 |
< |
* typically succeed if this task is the next task that would be |
768 |
< |
* executed by the current thread, and will typically fail (return |
769 |
< |
* false) otherwise. This method may be useful when arranging |
770 |
< |
* faster local processing of tasks that could have been, but were |
771 |
< |
* not, stolen. |
772 |
< |
* @return true if unforked |
766 |
> |
* Possibly executes other tasks until this task is ready, then |
767 |
> |
* returns the result of the computation. This method may be more |
768 |
> |
* efficient than {@code join}, but is only applicable when |
769 |
> |
* there are no potential dependencies between continuation of the |
770 |
> |
* current task and that of any other task that might be executed |
771 |
> |
* while helping. (This usually holds for pure divide-and-conquer |
772 |
> |
* tasks). This method may be invoked only from within |
773 |
> |
* ForkJoinTask computations. Attempts to invoke in other contexts |
774 |
> |
* result in exceptions or errors possibly including ClassCastException. |
775 |
> |
* |
776 |
> |
* @return the computed result |
777 |
|
*/ |
778 |
< |
public boolean tryUnfork() { |
779 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).unpushTask(this); |
778 |
> |
public final V helpJoin() { |
779 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); |
780 |
> |
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
781 |
> |
reportException(busyJoin(w)); |
782 |
> |
return getRawResult(); |
783 |
|
} |
784 |
|
|
785 |
|
/** |
786 |
< |
* Forks both tasks, returning when <tt>isDone</tt> holds for both |
787 |
< |
* of them or an exception is encountered. This method may be |
707 |
< |
* invoked only from within other ForkJoinTask |
786 |
> |
* Possibly executes other tasks until this task is ready. This |
787 |
> |
* method may be invoked only from within ForkJoinTask |
788 |
|
* computations. Attempts to invoke in other contexts result in |
789 |
< |
* exceptions or errors including ClassCastException. |
710 |
< |
* @param t1 one task |
711 |
< |
* @param t2 the other task |
712 |
< |
* @throws NullPointerException if t1 or t2 are null |
713 |
< |
* @throws RuntimeException or Error if either task did so. |
789 |
> |
* exceptions or errors possibly including ClassCastException. |
790 |
|
*/ |
791 |
< |
public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) { |
792 |
< |
t2.fork(); |
793 |
< |
t1.invoke(); |
794 |
< |
t2.join(); |
791 |
> |
public final void quietlyHelpJoin() { |
792 |
> |
if (status >= 0) { |
793 |
> |
ForkJoinWorkerThread w = |
794 |
> |
(ForkJoinWorkerThread)(Thread.currentThread()); |
795 |
> |
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
796 |
> |
busyJoin(w); |
797 |
> |
} |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
< |
* Forks the given tasks, returning when <tt>isDone</tt> holds for |
802 |
< |
* all of them. If any task encounters an exception, others may be |
803 |
< |
* cancelled. This method may be invoked only from within other |
804 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
726 |
< |
* result in exceptions or errors including ClassCastException. |
727 |
< |
* @param tasks the array of tasks |
728 |
< |
* @throws NullPointerException if tasks or any element are null. |
729 |
< |
* @throws RuntimeException or Error if any task did so. |
801 |
> |
* Joins this task, without returning its result or throwing an |
802 |
> |
* exception. This method may be useful when processing |
803 |
> |
* collections of tasks when some have been cancelled or otherwise |
804 |
> |
* known to have aborted. |
805 |
|
*/ |
806 |
< |
public static void invokeAll(ForkJoinTask<?>... tasks) { |
807 |
< |
Throwable ex = null; |
808 |
< |
int last = tasks.length - 1; |
809 |
< |
for (int i = last; i >= 0; --i) { |
810 |
< |
ForkJoinTask<?> t = tasks[i]; |
736 |
< |
if (t == null) { |
737 |
< |
if (ex == null) |
738 |
< |
ex = new NullPointerException(); |
739 |
< |
} |
740 |
< |
else if (i != 0) |
741 |
< |
t.fork(); |
742 |
< |
else { |
743 |
< |
t.quietlyInvoke(); |
744 |
< |
if (ex == null) |
745 |
< |
ex = t.getException(); |
746 |
< |
} |
747 |
< |
} |
748 |
< |
for (int i = 1; i <= last; ++i) { |
749 |
< |
ForkJoinTask<?> t = tasks[i]; |
750 |
< |
if (t != null) { |
751 |
< |
if (ex != null) |
752 |
< |
t.cancel(false); |
753 |
< |
else { |
754 |
< |
t.quietlyJoin(); |
755 |
< |
if (ex == null) |
756 |
< |
ex = t.getException(); |
757 |
< |
} |
758 |
< |
} |
806 |
> |
public final void quietlyJoin() { |
807 |
> |
if (status >= 0) { |
808 |
> |
ForkJoinWorkerThread w = getWorker(); |
809 |
> |
if (w == null || !w.unpushTask(this) || !tryQuietlyInvoke()) |
810 |
> |
awaitDone(w, true); |
811 |
|
} |
760 |
– |
if (ex != null) |
761 |
– |
rethrowException(ex); |
812 |
|
} |
813 |
|
|
814 |
|
/** |
815 |
< |
* Forks all tasks in the collection, returning when |
816 |
< |
* <tt>isDone</tt> holds for all of them. If any task encounters |
817 |
< |
* an exception, others may be cancelled. This method may be |
818 |
< |
* invoked only from within other ForkJoinTask |
819 |
< |
* computations. Attempts to invoke in other contexts result in |
770 |
< |
* exceptions or errors including ClassCastException. |
771 |
< |
* @param tasks the collection of tasks |
772 |
< |
* @throws NullPointerException if tasks or any element are null. |
773 |
< |
* @throws RuntimeException or Error if any task did so. |
815 |
> |
* Commences performing this task and awaits its completion if |
816 |
> |
* necessary, without returning its result or throwing an |
817 |
> |
* exception. This method may be useful when processing |
818 |
> |
* collections of tasks when some have been cancelled or otherwise |
819 |
> |
* known to have aborted. |
820 |
|
*/ |
821 |
< |
public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) { |
822 |
< |
if (!(tasks instanceof List)) { |
823 |
< |
invokeAll(tasks.toArray(new ForkJoinTask[tasks.size()])); |
778 |
< |
return; |
779 |
< |
} |
780 |
< |
List<? extends ForkJoinTask<?>> ts = |
781 |
< |
(List<? extends ForkJoinTask<?>>)tasks; |
782 |
< |
Throwable ex = null; |
783 |
< |
int last = ts.size() - 1; |
784 |
< |
for (int i = last; i >= 0; --i) { |
785 |
< |
ForkJoinTask<?> t = ts.get(i); |
786 |
< |
if (t == null) { |
787 |
< |
if (ex == null) |
788 |
< |
ex = new NullPointerException(); |
789 |
< |
} |
790 |
< |
else if (i != 0) |
791 |
< |
t.fork(); |
792 |
< |
else { |
793 |
< |
t.quietlyInvoke(); |
794 |
< |
if (ex == null) |
795 |
< |
ex = t.getException(); |
796 |
< |
} |
797 |
< |
} |
798 |
< |
for (int i = 1; i <= last; ++i) { |
799 |
< |
ForkJoinTask<?> t = ts.get(i); |
800 |
< |
if (t != null) { |
801 |
< |
if (ex != null) |
802 |
< |
t.cancel(false); |
803 |
< |
else { |
804 |
< |
t.quietlyJoin(); |
805 |
< |
if (ex == null) |
806 |
< |
ex = t.getException(); |
807 |
< |
} |
808 |
< |
} |
809 |
< |
} |
810 |
< |
if (ex != null) |
811 |
< |
rethrowException(ex); |
821 |
> |
public final void quietlyInvoke() { |
822 |
> |
if (status >= 0 && !tryQuietlyInvoke()) |
823 |
> |
quietlyJoin(); |
824 |
|
} |
825 |
|
|
826 |
|
/** |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
< |
* Returns a estimate of how many more locally queued tasks are |
838 |
> |
* Resets the internal bookkeeping state of this task, allowing a |
839 |
> |
* subsequent {@code fork}. This method allows repeated reuse of |
840 |
> |
* this task, but only if reuse occurs when this task has either |
841 |
> |
* never been forked, or has been forked, then completed and all |
842 |
> |
* outstanding joins of this task have also completed. Effects |
843 |
> |
* under any other usage conditions are not guaranteed, and are |
844 |
> |
* almost surely wrong. This method may be useful when executing |
845 |
> |
* pre-constructed trees of subtasks in loops. |
846 |
> |
*/ |
847 |
> |
public void reinitialize() { |
848 |
> |
if ((status & COMPLETION_MASK) == EXCEPTIONAL) |
849 |
> |
exceptionMap.remove(this); |
850 |
> |
status = 0; |
851 |
> |
} |
852 |
> |
|
853 |
> |
/** |
854 |
> |
* Returns the pool hosting the current task execution, or null |
855 |
> |
* if this task is executing outside of any pool. |
856 |
> |
* |
857 |
> |
* @return the pool, or null if none |
858 |
> |
*/ |
859 |
> |
public static ForkJoinPool getPool() { |
860 |
> |
Thread t = Thread.currentThread(); |
861 |
> |
return ((t instanceof ForkJoinWorkerThread)? |
862 |
> |
((ForkJoinWorkerThread)t).pool : null); |
863 |
> |
} |
864 |
> |
|
865 |
> |
/** |
866 |
> |
* Tries to unschedule this task for execution. This method will |
867 |
> |
* typically succeed if this task is the most recently forked task |
868 |
> |
* by the current thread, and has not commenced executing in |
869 |
> |
* another thread. This method may be useful when arranging |
870 |
> |
* alternative local processing of tasks that could have been, but |
871 |
> |
* were not, stolen. This method may be invoked only from within |
872 |
> |
* ForkJoinTask computations. Attempts to invoke in other contexts |
873 |
> |
* result in exceptions or errors possibly including ClassCastException. |
874 |
> |
* |
875 |
> |
* @return true if unforked |
876 |
> |
*/ |
877 |
> |
public boolean tryUnfork() { |
878 |
> |
return ((ForkJoinWorkerThread)(Thread.currentThread())).unpushTask(this); |
879 |
> |
} |
880 |
> |
|
881 |
> |
/** |
882 |
> |
* Returns an estimate of the number of tasks that have been |
883 |
> |
* forked by the current worker thread but not yet executed. This |
884 |
> |
* value may be useful for heuristic decisions about whether to |
885 |
> |
* fork other tasks. |
886 |
> |
* |
887 |
> |
* @return the number of tasks |
888 |
> |
*/ |
889 |
> |
public static int getQueuedTaskCount() { |
890 |
> |
return ((ForkJoinWorkerThread)(Thread.currentThread())). |
891 |
> |
getQueueSize(); |
892 |
> |
} |
893 |
> |
|
894 |
> |
/** |
895 |
> |
* Returns an estimate of how many more locally queued tasks are |
896 |
|
* held by the current worker thread than there are other worker |
897 |
< |
* threads that might want to steal them. This value may be |
898 |
< |
* useful for heuristic decisions about whether to fork other |
899 |
< |
* tasks. In many usages of ForkJoinTasks, at steady state, each |
900 |
< |
* worker should aim to maintain a small constant surplus (for |
901 |
< |
* example, 3) of tasks, and to process computations locally if |
902 |
< |
* this threshold is exceeded. |
897 |
> |
* threads that might steal them. This value may be useful for |
898 |
> |
* heuristic decisions about whether to fork other tasks. In many |
899 |
> |
* usages of ForkJoinTasks, at steady state, each worker should |
900 |
> |
* aim to maintain a small constant surplus (for example, 3) of |
901 |
> |
* tasks, and to process computations locally if this threshold is |
902 |
> |
* exceeded. |
903 |
> |
* |
904 |
|
* @return the surplus number of tasks, which may be negative |
905 |
|
*/ |
906 |
< |
public static int surplus() { |
906 |
> |
public static int getSurplusQueuedTaskCount() { |
907 |
|
return ((ForkJoinWorkerThread)(Thread.currentThread())) |
908 |
|
.getEstimatedSurplusTaskCount(); |
909 |
|
} |
910 |
|
|
911 |
< |
// Extension kit |
911 |
> |
// Extension methods |
912 |
|
|
913 |
|
/** |
914 |
< |
* Returns the result that would be returned by <tt>join</tt>, or |
915 |
< |
* null if this task is not known to have been completed. This |
916 |
< |
* method is designed to aid debugging, as well as to support |
917 |
< |
* extensions. Its use in any other context is discouraged. |
914 |
> |
* Returns the result that would be returned by {@code join}, |
915 |
> |
* even if this task completed abnormally, or null if this task is |
916 |
> |
* not known to have been completed. This method is designed to |
917 |
> |
* aid debugging, as well as to support extensions. Its use in any |
918 |
> |
* other context is discouraged. |
919 |
|
* |
920 |
< |
* @return the result, or null if not completed. |
920 |
> |
* @return the result, or null if not completed |
921 |
|
*/ |
922 |
|
public abstract V getRawResult(); |
923 |
|
|
936 |
|
* called otherwise. The return value controls whether this task |
937 |
|
* is considered to be done normally. It may return false in |
938 |
|
* asynchronous actions that require explicit invocations of |
939 |
< |
* <tt>complete</tt> to become joinable. It may throw exceptions |
939 |
> |
* {@code complete} to become joinable. It may throw exceptions |
940 |
|
* to indicate abnormal exit. |
941 |
+ |
* |
942 |
|
* @return true if completed normally |
943 |
|
* @throws Error or RuntimeException if encountered during computation |
944 |
|
*/ |
945 |
|
protected abstract boolean exec(); |
946 |
|
|
947 |
+ |
/** |
948 |
+ |
* Returns, but does not unschedule or execute, the task queued by |
949 |
+ |
* the current thread but not yet executed, if one is |
950 |
+ |
* available. There is no guarantee that this task will actually |
951 |
+ |
* be polled or executed next. This method is designed primarily |
952 |
+ |
* to support extensions, and is unlikely to be useful otherwise. |
953 |
+ |
* This method may be invoked only from within ForkJoinTask |
954 |
+ |
* computations. Attempts to invoke in other contexts result in |
955 |
+ |
* exceptions or errors possibly including ClassCastException. |
956 |
+ |
* |
957 |
+ |
* @return the next task, or null if none are available |
958 |
+ |
*/ |
959 |
+ |
protected static ForkJoinTask<?> peekNextLocalTask() { |
960 |
+ |
return ((ForkJoinWorkerThread)(Thread.currentThread())).peekTask(); |
961 |
+ |
} |
962 |
+ |
|
963 |
+ |
/** |
964 |
+ |
* Unschedules and returns, without executing, the next task |
965 |
+ |
* queued by the current thread but not yet executed. This method |
966 |
+ |
* is designed primarily to support extensions, and is unlikely to |
967 |
+ |
* be useful otherwise. This method may be invoked only from |
968 |
+ |
* within ForkJoinTask computations. Attempts to invoke in other |
969 |
+ |
* contexts result in exceptions or errors possibly including |
970 |
+ |
* ClassCastException. |
971 |
+ |
* |
972 |
+ |
* @return the next task, or null if none are available |
973 |
+ |
*/ |
974 |
+ |
protected static ForkJoinTask<?> pollNextLocalTask() { |
975 |
+ |
return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask(); |
976 |
+ |
} |
977 |
+ |
|
978 |
+ |
/** |
979 |
+ |
* Unschedules and returns, without executing, the next task |
980 |
+ |
* queued by the current thread but not yet executed, if one is |
981 |
+ |
* available, or if not available, a task that was forked by some |
982 |
+ |
* other thread, if available. Availability may be transient, so a |
983 |
+ |
* {@code null} result does not necessarily imply quiescence |
984 |
+ |
* of the pool this task is operating in. This method is designed |
985 |
+ |
* primarily to support extensions, and is unlikely to be useful |
986 |
+ |
* otherwise. This method may be invoked only from within |
987 |
+ |
* ForkJoinTask computations. Attempts to invoke in other contexts |
988 |
+ |
* result in exceptions or errors possibly including |
989 |
+ |
* ClassCastException. |
990 |
+ |
* |
991 |
+ |
* @return a task, or null if none are available |
992 |
+ |
*/ |
993 |
+ |
protected static ForkJoinTask<?> pollTask() { |
994 |
+ |
return ((ForkJoinWorkerThread)(Thread.currentThread())). |
995 |
+ |
pollTask(); |
996 |
+ |
} |
997 |
+ |
|
998 |
|
// Serialization support |
999 |
|
|
1000 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1003 |
|
* Save the state to a stream. |
1004 |
|
* |
1005 |
|
* @serialData the current run status and the exception thrown |
1006 |
< |
* during execution, or null if none. |
1006 |
> |
* during execution, or null if none |
1007 |
|
* @param s the stream |
1008 |
|
*/ |
1009 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1014 |
|
|
1015 |
|
/** |
1016 |
|
* Reconstitute the instance from a stream. |
1017 |
+ |
* |
1018 |
|
* @param s the stream |
1019 |
|
*/ |
1020 |
|
private void readObject(java.io.ObjectInputStream s) |
1021 |
|
throws java.io.IOException, ClassNotFoundException { |
1022 |
|
s.defaultReadObject(); |
1023 |
< |
// status &= ~INTERNAL_SIGNAL_MASK; // todo: define policy |
1023 |
> |
status &= ~INTERNAL_SIGNAL_MASK; // clear internal signal counts |
1024 |
> |
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1025 |
|
Object ex = s.readObject(); |
1026 |
|
if (ex != null) |
1027 |
|
setDoneExceptionally((Throwable)ex); |
1028 |
|
} |
1029 |
|
|
1030 |
|
// Temporary Unsafe mechanics for preliminary release |
1031 |
+ |
private static Unsafe getUnsafe() throws Throwable { |
1032 |
+ |
try { |
1033 |
+ |
return Unsafe.getUnsafe(); |
1034 |
+ |
} catch (SecurityException se) { |
1035 |
+ |
try { |
1036 |
+ |
return java.security.AccessController.doPrivileged |
1037 |
+ |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1038 |
+ |
public Unsafe run() throws Exception { |
1039 |
+ |
return getUnsafePrivileged(); |
1040 |
+ |
}}); |
1041 |
+ |
} catch (java.security.PrivilegedActionException e) { |
1042 |
+ |
throw e.getCause(); |
1043 |
+ |
} |
1044 |
+ |
} |
1045 |
+ |
} |
1046 |
|
|
1047 |
< |
static final Unsafe _unsafe; |
1047 |
> |
private static Unsafe getUnsafePrivileged() |
1048 |
> |
throws NoSuchFieldException, IllegalAccessException { |
1049 |
> |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1050 |
> |
f.setAccessible(true); |
1051 |
> |
return (Unsafe) f.get(null); |
1052 |
> |
} |
1053 |
> |
|
1054 |
> |
private static long fieldOffset(String fieldName) |
1055 |
> |
throws NoSuchFieldException { |
1056 |
> |
return UNSAFE.objectFieldOffset |
1057 |
> |
(ForkJoinTask.class.getDeclaredField(fieldName)); |
1058 |
> |
} |
1059 |
> |
|
1060 |
> |
static final Unsafe UNSAFE; |
1061 |
|
static final long statusOffset; |
1062 |
|
|
1063 |
|
static { |
1064 |
|
try { |
1065 |
< |
if (ForkJoinTask.class.getClassLoader() != null) { |
1066 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1067 |
< |
f.setAccessible(true); |
1068 |
< |
_unsafe = (Unsafe)f.get(null); |
1069 |
< |
} |
917 |
< |
else |
918 |
< |
_unsafe = Unsafe.getUnsafe(); |
919 |
< |
statusOffset = _unsafe.objectFieldOffset |
920 |
< |
(ForkJoinTask.class.getDeclaredField("status")); |
921 |
< |
} catch (Exception ex) { throw new Error(ex); } |
1065 |
> |
UNSAFE = getUnsafe(); |
1066 |
> |
statusOffset = fieldOffset("status"); |
1067 |
> |
} catch (Throwable e) { |
1068 |
> |
throw new RuntimeException("Could not initialize intrinsics", e); |
1069 |
> |
} |
1070 |
|
} |
1071 |
|
|
1072 |
|
} |