6 |
|
|
7 |
|
package jsr166y; |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
10 |
– |
|
9 |
|
import java.io.Serializable; |
10 |
|
import java.util.Collection; |
11 |
|
import java.util.Collections; |
13 |
|
import java.util.RandomAccess; |
14 |
|
import java.util.Map; |
15 |
|
import java.util.WeakHashMap; |
16 |
+ |
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
+ |
import java.util.concurrent.ExecutionException; |
19 |
+ |
import java.util.concurrent.Executor; |
20 |
+ |
import java.util.concurrent.ExecutorService; |
21 |
+ |
import java.util.concurrent.Future; |
22 |
+ |
import java.util.concurrent.RejectedExecutionException; |
23 |
+ |
import java.util.concurrent.RunnableFuture; |
24 |
+ |
import java.util.concurrent.TimeUnit; |
25 |
+ |
import java.util.concurrent.TimeoutException; |
26 |
|
|
27 |
|
/** |
28 |
|
* Abstract base class for tasks that run within a {@link ForkJoinPool}. |
36 |
|
* start other subtasks. As indicated by the name of this class, |
37 |
|
* many programs using {@code ForkJoinTask} employ only methods |
38 |
|
* {@link #fork} and {@link #join}, or derivatives such as {@link |
39 |
< |
* #invokeAll}. However, this class also provides a number of other |
40 |
< |
* methods that can come into play in advanced usages, as well as |
41 |
< |
* extension mechanics that allow support of new forms of fork/join |
42 |
< |
* processing. |
39 |
> |
* #invokeAll(ForkJoinTask...) invokeAll}. However, this class also |
40 |
> |
* provides a number of other methods that can come into play in |
41 |
> |
* advanced usages, as well as extension mechanics that allow |
42 |
> |
* support of new forms of fork/join processing. |
43 |
|
* |
44 |
|
* <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}. |
45 |
|
* The efficiency of {@code ForkJoinTask}s stems from a set of |
108 |
|
* ForkJoinTasks (as may be determined using method {@link |
109 |
|
* #inForkJoinPool}). Attempts to invoke them in other contexts |
110 |
|
* result in exceptions or errors, possibly including |
111 |
< |
* ClassCastException. |
111 |
> |
* {@code ClassCastException}. |
112 |
|
* |
113 |
|
* <p>Most base support methods are {@code final}, to prevent |
114 |
|
* overriding of implementations that are intrinsically tied to the |
152 |
|
* status maintenance (2) execution and awaiting completion (3) |
153 |
|
* user-level methods that additionally report results. This is |
154 |
|
* sometimes hard to see because this file orders exported methods |
155 |
< |
* in a way that flows well in javadocs. |
155 |
> |
* in a way that flows well in javadocs. In particular, most |
156 |
> |
* join mechanics are in method quietlyJoin, below. |
157 |
|
*/ |
158 |
|
|
159 |
|
/* |
161 |
|
* single int to minimize footprint and to ensure atomicity (via |
162 |
|
* CAS). Status is initially zero, and takes on nonnegative |
163 |
|
* values until completed, upon which status holds value |
164 |
< |
* COMPLETED. CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
164 |
> |
* NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
165 |
|
* waits by other threads have the SIGNAL bit set. Completion of |
166 |
|
* a stolen task with SIGNAL set awakens any waiters via |
167 |
|
* notifyAll. Even though suboptimal for some purposes, we use |
173 |
|
* them. |
174 |
|
*/ |
175 |
|
|
176 |
< |
/** Run status of this task */ |
176 |
> |
/** The run status of this task */ |
177 |
|
volatile int status; // accessed directly by pool and workers |
178 |
|
|
179 |
|
private static final int NORMAL = -1; |
200 |
|
* also clearing signal request bits. |
201 |
|
* |
202 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
194 |
– |
* @return status on exit |
203 |
|
*/ |
204 |
< |
private int setCompletion(int completion) { |
204 |
> |
private void setCompletion(int completion) { |
205 |
|
int s; |
206 |
|
while ((s = status) >= 0) { |
207 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
208 |
|
if (s != 0) |
209 |
|
synchronized (this) { notifyAll(); } |
210 |
< |
return completion; |
210 |
> |
break; |
211 |
|
} |
212 |
|
} |
205 |
– |
return s; |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
< |
* Record exception and set exceptional completion |
216 |
> |
* Records exception and sets exceptional completion. |
217 |
> |
* |
218 |
|
* @return status on exit |
219 |
|
*/ |
220 |
< |
private int setExceptionalCompletion(Throwable rex) { |
220 |
> |
private void setExceptionalCompletion(Throwable rex) { |
221 |
|
exceptionMap.put(this, rex); |
222 |
< |
return setCompletion(EXCEPTIONAL); |
222 |
> |
setCompletion(EXCEPTIONAL); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* Blocks a worker thread until completion. Called only by pool. |
226 |
> |
* Blocks a worker thread until completion. Called only by |
227 |
> |
* pool. Currently unused -- pool-based waits use timeout |
228 |
> |
* version below. |
229 |
|
*/ |
230 |
|
final void internalAwaitDone() { |
231 |
|
int s; // the odd construction reduces lock bias effects |
232 |
|
while ((s = status) >= 0) { |
233 |
|
try { |
234 |
< |
synchronized(this) { |
234 |
> |
synchronized (this) { |
235 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
236 |
|
wait(); |
237 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
/** |
245 |
< |
* Blocks a non-worker-thread until completion. |
245 |
> |
* Blocks a worker thread until completed or timed out. Called |
246 |
> |
* only by pool. |
247 |
> |
* |
248 |
|
* @return status on exit |
249 |
|
*/ |
250 |
< |
private int externalAwaitDone() { |
250 |
> |
final int internalAwaitDone(long millis, int nanos) { |
251 |
> |
int s; |
252 |
> |
if ((s = status) >= 0) { |
253 |
> |
try { |
254 |
> |
synchronized (this) { |
255 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
256 |
> |
wait(millis, nanos); |
257 |
> |
} |
258 |
> |
} catch (InterruptedException ie) { |
259 |
> |
cancelIfTerminating(); |
260 |
> |
} |
261 |
> |
s = status; |
262 |
> |
} |
263 |
> |
return s; |
264 |
> |
} |
265 |
> |
|
266 |
> |
/** |
267 |
> |
* Blocks a non-worker-thread until completion. |
268 |
> |
*/ |
269 |
> |
private void externalAwaitDone() { |
270 |
|
int s; |
271 |
|
while ((s = status) >= 0) { |
272 |
< |
synchronized(this) { |
273 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)){ |
272 |
> |
synchronized (this) { |
273 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
274 |
|
boolean interrupted = false; |
275 |
< |
while ((s = status) >= 0) { |
275 |
> |
while (status >= 0) { |
276 |
|
try { |
277 |
|
wait(); |
278 |
|
} catch (InterruptedException ie) { |
285 |
|
} |
286 |
|
} |
287 |
|
} |
257 |
– |
return s; |
288 |
|
} |
289 |
|
|
290 |
|
/** |
292 |
|
* doesn't wait for completion otherwise. Primary execution method |
293 |
|
* for ForkJoinWorkerThread. |
294 |
|
*/ |
295 |
< |
final void tryExec() { |
295 |
> |
final void quietlyExec() { |
296 |
|
try { |
297 |
|
if (status < 0 || !exec()) |
298 |
|
return; |
303 |
|
setCompletion(NORMAL); // must be outside try block |
304 |
|
} |
305 |
|
|
276 |
– |
/** |
277 |
– |
* If not done and this task is next in worker queue, runs it, |
278 |
– |
* else waits for it. |
279 |
– |
* @return status on exit |
280 |
– |
*/ |
281 |
– |
private int doJoin() { |
282 |
– |
int stat; |
283 |
– |
Thread t; |
284 |
– |
ForkJoinWorkerThread w; |
285 |
– |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
286 |
– |
if ((stat = status) < 0) |
287 |
– |
return stat; |
288 |
– |
if ((w = (ForkJoinWorkerThread) t).unpushTask(this)) { |
289 |
– |
boolean completed; |
290 |
– |
try { |
291 |
– |
completed = exec(); |
292 |
– |
} catch (Throwable rex) { |
293 |
– |
return setExceptionalCompletion(rex); |
294 |
– |
} |
295 |
– |
if (completed) |
296 |
– |
return setCompletion(NORMAL); |
297 |
– |
} |
298 |
– |
return w.joinTask(this); |
299 |
– |
} |
300 |
– |
return externalAwaitDone(); |
301 |
– |
} |
302 |
– |
|
303 |
– |
/** |
304 |
– |
* Unless done, calls exec and records status if completed, or |
305 |
– |
* waits for completion otherwise. |
306 |
– |
* @return status on exit |
307 |
– |
*/ |
308 |
– |
private int doInvoke() { |
309 |
– |
int stat; |
310 |
– |
if ((stat = status) >= 0) { |
311 |
– |
boolean completed; |
312 |
– |
try { |
313 |
– |
completed = exec(); |
314 |
– |
} catch (Throwable rex) { |
315 |
– |
return setExceptionalCompletion(rex); |
316 |
– |
} |
317 |
– |
stat = completed ? setCompletion(NORMAL) : doJoin(); |
318 |
– |
} |
319 |
– |
return stat; |
320 |
– |
} |
321 |
– |
|
322 |
– |
/** |
323 |
– |
* Returns result or throws exception associated with given status. |
324 |
– |
* @param s the status |
325 |
– |
*/ |
326 |
– |
private V reportResult(int s) { |
327 |
– |
Throwable ex; |
328 |
– |
if (s < NORMAL && (ex = getException()) != null) |
329 |
– |
UNSAFE.throwException(ex); |
330 |
– |
return getRawResult(); |
331 |
– |
} |
332 |
– |
|
306 |
|
// public methods |
307 |
|
|
308 |
|
/** |
338 |
|
* @return the computed result |
339 |
|
*/ |
340 |
|
public final V join() { |
341 |
< |
return reportResult(doJoin()); |
341 |
> |
quietlyJoin(); |
342 |
> |
Throwable ex; |
343 |
> |
if (status < NORMAL && (ex = getException()) != null) |
344 |
> |
UNSAFE.throwException(ex); |
345 |
> |
return getRawResult(); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
|
* Commences performing this task, awaits its completion if |
350 |
< |
* necessary, and return its result, or throws an (unchecked) |
351 |
< |
* exception if the underlying computation did so. |
350 |
> |
* necessary, and returns its result, or throws an (unchecked) |
351 |
> |
* {@code RuntimeException} or {@code Error} if the underlying |
352 |
> |
* computation did so. |
353 |
|
* |
354 |
|
* @return the computed result |
355 |
|
*/ |
356 |
|
public final V invoke() { |
357 |
< |
return reportResult(doInvoke()); |
357 |
> |
quietlyInvoke(); |
358 |
> |
Throwable ex; |
359 |
> |
if (status < NORMAL && (ex = getException()) != null) |
360 |
> |
UNSAFE.throwException(ex); |
361 |
> |
return getRawResult(); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
366 |
|
* each task or an (unchecked) exception is encountered, in which |
367 |
< |
* case the exception is rethrown. If either task encounters an |
368 |
< |
* exception, the other one may be, but is not guaranteed to be, |
369 |
< |
* cancelled. If both tasks throw an exception, then this method |
370 |
< |
* throws one of them. The individual status of each task may be |
371 |
< |
* checked using {@link #getException()} and related methods. |
367 |
> |
* case the exception is rethrown. If more than one task |
368 |
> |
* encounters an exception, then this method throws any one of |
369 |
> |
* these exceptions. If any task encounters an exception, the |
370 |
> |
* other may be cancelled. However, the execution status of |
371 |
> |
* individual tasks is not guaranteed upon exceptional return. The |
372 |
> |
* status of each task may be obtained using {@link |
373 |
> |
* #getException()} and related methods to check if they have been |
374 |
> |
* cancelled, completed normally or exceptionally, or left |
375 |
> |
* unprocessed. |
376 |
|
* |
377 |
|
* <p>This method may be invoked only from within {@code |
378 |
|
* ForkJoinTask} computations (as may be determined using method |
393 |
|
/** |
394 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
395 |
|
* each task or an (unchecked) exception is encountered, in which |
396 |
< |
* case the exception is rethrown. If any task encounters an |
397 |
< |
* exception, others may be, but are not guaranteed to be, |
398 |
< |
* cancelled. If more than one task encounters an exception, then |
399 |
< |
* this method throws any one of these exceptions. The individual |
400 |
< |
* status of each task may be checked using {@link #getException()} |
401 |
< |
* and related methods. |
396 |
> |
* case the exception is rethrown. If more than one task |
397 |
> |
* encounters an exception, then this method throws any one of |
398 |
> |
* these exceptions. If any task encounters an exception, others |
399 |
> |
* may be cancelled. However, the execution status of individual |
400 |
> |
* tasks is not guaranteed upon exceptional return. The status of |
401 |
> |
* each task may be obtained using {@link #getException()} and |
402 |
> |
* related methods to check if they have been cancelled, completed |
403 |
> |
* normally or exceptionally, or left unprocessed. |
404 |
|
* |
405 |
|
* <p>This method may be invoked only from within {@code |
406 |
|
* ForkJoinTask} computations (as may be determined using method |
422 |
|
} |
423 |
|
else if (i != 0) |
424 |
|
t.fork(); |
425 |
< |
else if (t.doInvoke() < NORMAL && ex == null) |
426 |
< |
ex = t.getException(); |
425 |
> |
else { |
426 |
> |
t.quietlyInvoke(); |
427 |
> |
if (ex == null && t.status < NORMAL) |
428 |
> |
ex = t.getException(); |
429 |
> |
} |
430 |
|
} |
431 |
|
for (int i = 1; i <= last; ++i) { |
432 |
|
ForkJoinTask<?> t = tasks[i]; |
433 |
|
if (t != null) { |
434 |
|
if (ex != null) |
435 |
|
t.cancel(false); |
436 |
< |
else if (t.doJoin() < NORMAL && ex == null) |
437 |
< |
ex = t.getException(); |
436 |
> |
else { |
437 |
> |
t.quietlyJoin(); |
438 |
> |
if (ex == null && t.status < NORMAL) |
439 |
> |
ex = t.getException(); |
440 |
> |
} |
441 |
|
} |
442 |
|
} |
443 |
|
if (ex != null) |
447 |
|
/** |
448 |
|
* Forks all tasks in the specified collection, returning when |
449 |
|
* {@code isDone} holds for each task or an (unchecked) exception |
450 |
< |
* is encountered. If any task encounters an exception, others |
451 |
< |
* may be, but are not guaranteed to be, cancelled. If more than |
452 |
< |
* one task encounters an exception, then this method throws any |
453 |
< |
* one of these exceptions. The individual status of each task |
454 |
< |
* may be checked using {@link #getException()} and related |
455 |
< |
* methods. The behavior of this operation is undefined if the |
456 |
< |
* specified collection is modified while the operation is in |
457 |
< |
* progress. |
450 |
> |
* is encountered, in which case the exception is rethrown. If |
451 |
> |
* more than one task encounters an exception, then this method |
452 |
> |
* throws any one of these exceptions. If any task encounters an |
453 |
> |
* exception, others may be cancelled. However, the execution |
454 |
> |
* status of individual tasks is not guaranteed upon exceptional |
455 |
> |
* return. The status of each task may be obtained using {@link |
456 |
> |
* #getException()} and related methods to check if they have been |
457 |
> |
* cancelled, completed normally or exceptionally, or left |
458 |
> |
* unprocessed. |
459 |
|
* |
460 |
|
* <p>This method may be invoked only from within {@code |
461 |
|
* ForkJoinTask} computations (as may be determined using method |
485 |
|
} |
486 |
|
else if (i != 0) |
487 |
|
t.fork(); |
488 |
< |
else if (t.doInvoke() < NORMAL && ex == null) |
489 |
< |
ex = t.getException(); |
488 |
> |
else { |
489 |
> |
t.quietlyInvoke(); |
490 |
> |
if (ex == null && t.status < NORMAL) |
491 |
> |
ex = t.getException(); |
492 |
> |
} |
493 |
|
} |
494 |
|
for (int i = 1; i <= last; ++i) { |
495 |
|
ForkJoinTask<?> t = ts.get(i); |
496 |
|
if (t != null) { |
497 |
|
if (ex != null) |
498 |
|
t.cancel(false); |
499 |
< |
else if (t.doJoin() < NORMAL && ex == null) |
500 |
< |
ex = t.getException(); |
499 |
> |
else { |
500 |
> |
t.quietlyJoin(); |
501 |
> |
if (ex == null && t.status < NORMAL) |
502 |
> |
ex = t.getException(); |
503 |
> |
} |
504 |
|
} |
505 |
|
} |
506 |
|
if (ex != null) |
533 |
|
* @return {@code true} if this task is now cancelled |
534 |
|
*/ |
535 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
536 |
< |
return setCompletion(CANCELLED) == CANCELLED; |
536 |
> |
setCompletion(CANCELLED); |
537 |
> |
return status == CANCELLED; |
538 |
|
} |
539 |
|
|
540 |
|
/** |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
< |
* Cancels ignoring exceptions if worker is terminating |
554 |
> |
* Cancels if current thread is a terminating worker thread, |
555 |
> |
* ignoring any exceptions thrown by cancel. |
556 |
|
*/ |
557 |
|
final void cancelIfTerminating() { |
558 |
|
Thread t = Thread.currentThread(); |
629 |
|
|
630 |
|
/** |
631 |
|
* Completes this task, and if not already aborted or cancelled, |
632 |
< |
* returning a {@code null} result upon {@code join} and related |
633 |
< |
* operations. This method may be used to provide results for |
634 |
< |
* asynchronous tasks, or to provide alternative handling for |
635 |
< |
* tasks that would not otherwise complete normally. Its use in |
636 |
< |
* other situations is discouraged. This method is |
637 |
< |
* overridable, but overridden versions must invoke {@code super} |
638 |
< |
* implementation to maintain guarantees. |
632 |
> |
* returning the given value as the result of subsequent |
633 |
> |
* invocations of {@code join} and related operations. This method |
634 |
> |
* may be used to provide results for asynchronous tasks, or to |
635 |
> |
* provide alternative handling for tasks that would not otherwise |
636 |
> |
* complete normally. Its use in other situations is |
637 |
> |
* discouraged. This method is overridable, but overridden |
638 |
> |
* versions must invoke {@code super} implementation to maintain |
639 |
> |
* guarantees. |
640 |
|
* |
641 |
|
* @param value the result value for this task |
642 |
|
*/ |
650 |
|
setCompletion(NORMAL); |
651 |
|
} |
652 |
|
|
653 |
+ |
/** |
654 |
+ |
* Waits if necessary for the computation to complete, and then |
655 |
+ |
* retrieves its result. |
656 |
+ |
* |
657 |
+ |
* @return the computed result |
658 |
+ |
* @throws CancellationException if the computation was cancelled |
659 |
+ |
* @throws ExecutionException if the computation threw an |
660 |
+ |
* exception |
661 |
+ |
* @throws InterruptedException if the current thread is not a |
662 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
663 |
+ |
*/ |
664 |
|
public final V get() throws InterruptedException, ExecutionException { |
665 |
< |
int s = doJoin(); |
666 |
< |
if (Thread.interrupted()) |
667 |
< |
throw new InterruptedException(); |
665 |
> |
int s; |
666 |
> |
if (Thread.currentThread() instanceof ForkJoinWorkerThread) { |
667 |
> |
quietlyJoin(); |
668 |
> |
s = status; |
669 |
> |
} |
670 |
> |
else { |
671 |
> |
while ((s = status) >= 0) { |
672 |
> |
synchronized (this) { // interruptible form of awaitDone |
673 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
674 |
> |
s, SIGNAL)) { |
675 |
> |
while (status >= 0) |
676 |
> |
wait(); |
677 |
> |
} |
678 |
> |
} |
679 |
> |
} |
680 |
> |
} |
681 |
|
if (s < NORMAL) { |
682 |
|
Throwable ex; |
683 |
|
if (s == CANCELLED) |
688 |
|
return getRawResult(); |
689 |
|
} |
690 |
|
|
691 |
+ |
/** |
692 |
+ |
* Waits if necessary for at most the given time for the computation |
693 |
+ |
* to complete, and then retrieves its result, if available. |
694 |
+ |
* |
695 |
+ |
* @param timeout the maximum time to wait |
696 |
+ |
* @param unit the time unit of the timeout argument |
697 |
+ |
* @return the computed result |
698 |
+ |
* @throws CancellationException if the computation was cancelled |
699 |
+ |
* @throws ExecutionException if the computation threw an |
700 |
+ |
* exception |
701 |
+ |
* @throws InterruptedException if the current thread is not a |
702 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
703 |
+ |
* @throws TimeoutException if the wait timed out |
704 |
+ |
*/ |
705 |
|
public final V get(long timeout, TimeUnit unit) |
706 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
707 |
< |
Thread t = Thread.currentThread(); |
708 |
< |
ForkJoinPool pool; |
709 |
< |
if (t instanceof ForkJoinWorkerThread) { |
710 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
711 |
< |
if (status >= 0 && w.unpushTask(this)) |
712 |
< |
tryExec(); |
713 |
< |
pool = w.pool; |
714 |
< |
} |
715 |
< |
else |
716 |
< |
pool = null; |
717 |
< |
/* |
718 |
< |
* Timed wait loop intermixes cases for FJ (pool != null) and |
719 |
< |
* non FJ threads. For FJ, decrement pool count but don't try |
720 |
< |
* for replacement; increment count on completion. For non-FJ, |
721 |
< |
* deal with interrupts. This is messy, but a little less so |
722 |
< |
* than is splitting the FJ and nonFJ cases. |
723 |
< |
*/ |
682 |
< |
boolean interrupted = false; |
683 |
< |
boolean dec = false; // true if pool count decremented |
684 |
< |
for (;;) { |
685 |
< |
if (Thread.interrupted() && pool == null) { |
686 |
< |
interrupted = true; |
687 |
< |
break; |
707 |
> |
long nanos = unit.toNanos(timeout); |
708 |
> |
if (status >= 0) { |
709 |
> |
Thread t = Thread.currentThread(); |
710 |
> |
if (t instanceof ForkJoinWorkerThread) { |
711 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
712 |
> |
boolean completed = false; // timed variant of quietlyJoin |
713 |
> |
if (w.unpushTask(this)) { |
714 |
> |
try { |
715 |
> |
completed = exec(); |
716 |
> |
} catch (Throwable rex) { |
717 |
> |
setExceptionalCompletion(rex); |
718 |
> |
} |
719 |
> |
} |
720 |
> |
if (completed) |
721 |
> |
setCompletion(NORMAL); |
722 |
> |
else if (status >= 0) |
723 |
> |
w.joinTask(this, true, nanos); |
724 |
|
} |
725 |
< |
int s = status; |
726 |
< |
if (s < 0) |
727 |
< |
break; |
692 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
725 |
> |
else if (Thread.interrupted()) |
726 |
> |
throw new InterruptedException(); |
727 |
> |
else { |
728 |
|
long startTime = System.nanoTime(); |
729 |
< |
long nanos = unit.toNanos(timeout); |
730 |
< |
long nt; // wait time |
696 |
< |
while (status >= 0 && |
729 |
> |
int s; long nt; |
730 |
> |
while ((s = status) >= 0 && |
731 |
|
(nt = nanos - (System.nanoTime() - startTime)) > 0) { |
732 |
< |
if (pool != null && !dec) |
733 |
< |
dec = pool.tryDecrementRunningCount(); |
700 |
< |
else { |
732 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, |
733 |
> |
SIGNAL)) { |
734 |
|
long ms = nt / 1000000; |
735 |
|
int ns = (int) (nt % 1000000); |
736 |
< |
try { |
737 |
< |
synchronized(this) { |
738 |
< |
if (status >= 0) |
706 |
< |
wait(ms, ns); |
707 |
< |
} |
708 |
< |
} catch (InterruptedException ie) { |
709 |
< |
if (pool != null) |
710 |
< |
cancelIfTerminating(); |
711 |
< |
else { |
712 |
< |
interrupted = true; |
713 |
< |
break; |
714 |
< |
} |
736 |
> |
synchronized (this) { |
737 |
> |
if (status >= 0) |
738 |
> |
wait(ms, ns); // exit on IE throw |
739 |
|
} |
740 |
|
} |
741 |
|
} |
718 |
– |
break; |
742 |
|
} |
743 |
|
} |
721 |
– |
if (pool != null && dec) |
722 |
– |
pool.incrementRunningCount(); |
723 |
– |
if (interrupted) |
724 |
– |
throw new InterruptedException(); |
744 |
|
int es = status; |
745 |
|
if (es != NORMAL) { |
746 |
|
Throwable ex; |
754 |
|
} |
755 |
|
|
756 |
|
/** |
757 |
< |
* Joins this task, without returning its result or throwing an |
757 |
> |
* Joins this task, without returning its result or throwing its |
758 |
|
* exception. This method may be useful when processing |
759 |
|
* collections of tasks when some have been cancelled or otherwise |
760 |
|
* known to have aborted. |
761 |
|
*/ |
762 |
|
public final void quietlyJoin() { |
763 |
< |
doJoin(); |
763 |
> |
Thread t; |
764 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
765 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
766 |
> |
if (status >= 0) { |
767 |
> |
if (w.unpushTask(this)) { |
768 |
> |
boolean completed; |
769 |
> |
try { |
770 |
> |
completed = exec(); |
771 |
> |
} catch (Throwable rex) { |
772 |
> |
setExceptionalCompletion(rex); |
773 |
> |
return; |
774 |
> |
} |
775 |
> |
if (completed) { |
776 |
> |
setCompletion(NORMAL); |
777 |
> |
return; |
778 |
> |
} |
779 |
> |
} |
780 |
> |
w.joinTask(this, false, 0L); |
781 |
> |
} |
782 |
> |
} |
783 |
> |
else |
784 |
> |
externalAwaitDone(); |
785 |
|
} |
786 |
|
|
787 |
|
/** |
788 |
|
* Commences performing this task and awaits its completion if |
789 |
< |
* necessary, without returning its result or throwing an |
790 |
< |
* exception. This method may be useful when processing |
751 |
< |
* collections of tasks when some have been cancelled or otherwise |
752 |
< |
* known to have aborted. |
789 |
> |
* necessary, without returning its result or throwing its |
790 |
> |
* exception. |
791 |
|
*/ |
792 |
|
public final void quietlyInvoke() { |
793 |
< |
doInvoke(); |
793 |
> |
if (status >= 0) { |
794 |
> |
boolean completed; |
795 |
> |
try { |
796 |
> |
completed = exec(); |
797 |
> |
} catch (Throwable rex) { |
798 |
> |
setExceptionalCompletion(rex); |
799 |
> |
return; |
800 |
> |
} |
801 |
> |
if (completed) |
802 |
> |
setCompletion(NORMAL); |
803 |
> |
else |
804 |
> |
quietlyJoin(); |
805 |
> |
} |
806 |
|
} |
807 |
|
|
808 |
|
/** |
832 |
|
* under any other usage conditions are not guaranteed. |
833 |
|
* This method may be useful when executing |
834 |
|
* pre-constructed trees of subtasks in loops. |
835 |
+ |
* |
836 |
+ |
* <p>Upon completion of this method, {@code isDone()} reports |
837 |
+ |
* {@code false}, and {@code getException()} reports {@code |
838 |
+ |
* null}. However, the value returned by {@code getRawResult} is |
839 |
+ |
* unaffected. To clear this value, you can invoke {@code |
840 |
+ |
* setRawResult(null)}. |
841 |
|
*/ |
842 |
|
public void reinitialize() { |
843 |
|
if (status == EXCEPTIONAL) |
1130 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1131 |
|
|
1132 |
|
/** |
1133 |
< |
* Saves the state to a stream. |
1133 |
> |
* Saves the state to a stream (that is, serializes it). |
1134 |
|
* |
1135 |
|
* @serialData the current run status and the exception thrown |
1136 |
|
* during execution, or {@code null} if none |
1143 |
|
} |
1144 |
|
|
1145 |
|
/** |
1146 |
< |
* Reconstitutes the instance from a stream. |
1146 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1147 |
|
* |
1148 |
|
* @param s the stream |
1149 |
|
*/ |