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>Method {@link #join} and its variants are appropriate for use |
114 |
> |
* only when completion dependencies are acyclic; that is, the |
115 |
> |
* parallel computation can be described as a directed acyclic graph |
116 |
> |
* (DAG). Otherwise, executions may encounter a form of deadlock as |
117 |
> |
* tasks cyclically wait for each other. However, this framework |
118 |
> |
* supports other methods and techniques (for example the use of |
119 |
> |
* {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that |
120 |
> |
* may be of use in constructing custom subclasses for problems that |
121 |
> |
* are not statically structured as DAGs. |
122 |
|
* |
123 |
|
* <p>Most base support methods are {@code final}, to prevent |
124 |
|
* overriding of implementations that are intrinsically tied to the |
162 |
|
* status maintenance (2) execution and awaiting completion (3) |
163 |
|
* user-level methods that additionally report results. This is |
164 |
|
* sometimes hard to see because this file orders exported methods |
165 |
< |
* in a way that flows well in javadocs. |
165 |
> |
* in a way that flows well in javadocs. In particular, most |
166 |
> |
* join mechanics are in method quietlyJoin, below. |
167 |
|
*/ |
168 |
|
|
169 |
|
/* |
171 |
|
* single int to minimize footprint and to ensure atomicity (via |
172 |
|
* CAS). Status is initially zero, and takes on nonnegative |
173 |
|
* values until completed, upon which status holds value |
174 |
< |
* COMPLETED. CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
174 |
> |
* NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
175 |
|
* waits by other threads have the SIGNAL bit set. Completion of |
176 |
|
* a stolen task with SIGNAL set awakens any waiters via |
177 |
|
* notifyAll. Even though suboptimal for some purposes, we use |
183 |
|
* them. |
184 |
|
*/ |
185 |
|
|
186 |
< |
/** Run status of this task */ |
186 |
> |
/** The run status of this task */ |
187 |
|
volatile int status; // accessed directly by pool and workers |
188 |
|
|
189 |
|
private static final int NORMAL = -1; |
210 |
|
* also clearing signal request bits. |
211 |
|
* |
212 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
194 |
– |
* @return status on exit |
213 |
|
*/ |
214 |
< |
private int setCompletion(int completion) { |
214 |
> |
private void setCompletion(int completion) { |
215 |
|
int s; |
216 |
|
while ((s = status) >= 0) { |
217 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
218 |
|
if (s != 0) |
219 |
|
synchronized (this) { notifyAll(); } |
220 |
< |
return completion; |
220 |
> |
break; |
221 |
|
} |
222 |
|
} |
205 |
– |
return s; |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* Record exception and set exceptional completion |
226 |
> |
* Records exception and sets exceptional completion. |
227 |
> |
* |
228 |
|
* @return status on exit |
229 |
|
*/ |
230 |
< |
private int setExceptionalCompletion(Throwable rex) { |
230 |
> |
private void setExceptionalCompletion(Throwable rex) { |
231 |
|
exceptionMap.put(this, rex); |
232 |
< |
return setCompletion(EXCEPTIONAL); |
232 |
> |
setCompletion(EXCEPTIONAL); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* Blocks a worker thread until completion. Called only by pool. |
236 |
> |
* Blocks a worker thread until completion. Called only by |
237 |
> |
* pool. Currently unused -- pool-based waits use timeout |
238 |
> |
* version below. |
239 |
|
*/ |
240 |
|
final void internalAwaitDone() { |
241 |
|
int s; // the odd construction reduces lock bias effects |
242 |
|
while ((s = status) >= 0) { |
243 |
|
try { |
244 |
< |
synchronized(this) { |
244 |
> |
synchronized (this) { |
245 |
|
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
246 |
|
wait(); |
247 |
|
} |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* Blocks a non-worker-thread until completion. |
255 |
> |
* Blocks a worker thread until completed or timed out. Called |
256 |
> |
* only by pool. |
257 |
> |
* |
258 |
|
* @return status on exit |
259 |
|
*/ |
260 |
< |
private int externalAwaitDone() { |
260 |
> |
final int internalAwaitDone(long millis, int nanos) { |
261 |
> |
int s; |
262 |
> |
if ((s = status) >= 0) { |
263 |
> |
try { |
264 |
> |
synchronized (this) { |
265 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s,SIGNAL)) |
266 |
> |
wait(millis, nanos); |
267 |
> |
} |
268 |
> |
} catch (InterruptedException ie) { |
269 |
> |
cancelIfTerminating(); |
270 |
> |
} |
271 |
> |
s = status; |
272 |
> |
} |
273 |
> |
return s; |
274 |
> |
} |
275 |
> |
|
276 |
> |
/** |
277 |
> |
* Blocks a non-worker-thread until completion. |
278 |
> |
*/ |
279 |
> |
private void externalAwaitDone() { |
280 |
|
int s; |
281 |
|
while ((s = status) >= 0) { |
282 |
< |
synchronized(this) { |
283 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)){ |
282 |
> |
synchronized (this) { |
283 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
284 |
|
boolean interrupted = false; |
285 |
< |
while ((s = status) >= 0) { |
285 |
> |
while (status >= 0) { |
286 |
|
try { |
287 |
|
wait(); |
288 |
|
} catch (InterruptedException ie) { |
295 |
|
} |
296 |
|
} |
297 |
|
} |
257 |
– |
return s; |
298 |
|
} |
299 |
|
|
300 |
|
/** |
302 |
|
* doesn't wait for completion otherwise. Primary execution method |
303 |
|
* for ForkJoinWorkerThread. |
304 |
|
*/ |
305 |
< |
final void tryExec() { |
305 |
> |
final void quietlyExec() { |
306 |
|
try { |
307 |
|
if (status < 0 || !exec()) |
308 |
|
return; |
313 |
|
setCompletion(NORMAL); // must be outside try block |
314 |
|
} |
315 |
|
|
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 |
– |
|
316 |
|
// public methods |
317 |
|
|
318 |
|
/** |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* Returns the result of the computation when it {@link #isDone is done}. |
344 |
< |
* This method differs from {@link #get()} in that |
343 |
> |
* Returns the result of the computation when it {@link #isDone is |
344 |
> |
* done}. This method differs from {@link #get()} in that |
345 |
|
* abnormal completion results in {@code RuntimeException} or |
346 |
< |
* {@code Error}, not {@code ExecutionException}. |
346 |
> |
* {@code Error}, not {@code ExecutionException}, and that |
347 |
> |
* interrupts of the calling thread do <em>not</em> cause the |
348 |
> |
* method to abruptly return by throwing {@code |
349 |
> |
* InterruptedException}. |
350 |
|
* |
351 |
|
* @return the computed result |
352 |
|
*/ |
353 |
|
public final V join() { |
354 |
< |
return reportResult(doJoin()); |
354 |
> |
quietlyJoin(); |
355 |
> |
Throwable ex; |
356 |
> |
if (status < NORMAL && (ex = getException()) != null) |
357 |
> |
UNSAFE.throwException(ex); |
358 |
> |
return getRawResult(); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
|
* Commences performing this task, awaits its completion if |
363 |
< |
* necessary, and return its result, or throws an (unchecked) |
364 |
< |
* exception if the underlying computation did so. |
363 |
> |
* necessary, and returns its result, or throws an (unchecked) |
364 |
> |
* {@code RuntimeException} or {@code Error} if the underlying |
365 |
> |
* computation did so. |
366 |
|
* |
367 |
|
* @return the computed result |
368 |
|
*/ |
369 |
|
public final V invoke() { |
370 |
< |
return reportResult(doInvoke()); |
370 |
> |
quietlyInvoke(); |
371 |
> |
Throwable ex; |
372 |
> |
if (status < NORMAL && (ex = getException()) != null) |
373 |
> |
UNSAFE.throwException(ex); |
374 |
> |
return getRawResult(); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
379 |
|
* each task or an (unchecked) exception is encountered, in which |
380 |
< |
* case the exception is rethrown. If either task encounters an |
381 |
< |
* exception, the other one may be, but is not guaranteed to be, |
382 |
< |
* cancelled. If both tasks throw an exception, then this method |
383 |
< |
* throws one of them. The individual status of each task may be |
384 |
< |
* checked using {@link #getException()} and related methods. |
380 |
> |
* case the exception is rethrown. If more than one task |
381 |
> |
* encounters an exception, then this method throws any one of |
382 |
> |
* these exceptions. If any task encounters an exception, the |
383 |
> |
* other may be cancelled. However, the execution status of |
384 |
> |
* individual tasks is not guaranteed upon exceptional return. The |
385 |
> |
* status of each task may be obtained using {@link |
386 |
> |
* #getException()} and related methods to check if they have been |
387 |
> |
* cancelled, completed normally or exceptionally, or left |
388 |
> |
* unprocessed. |
389 |
|
* |
390 |
|
* <p>This method may be invoked only from within {@code |
391 |
< |
* ForkJoinTask} computations (as may be determined using method |
391 |
> |
* ForkJoinPool} computations (as may be determined using method |
392 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
393 |
|
* result in exceptions or errors, possibly including {@code |
394 |
|
* ClassCastException}. |
406 |
|
/** |
407 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
408 |
|
* each task or an (unchecked) exception is encountered, in which |
409 |
< |
* case the exception is rethrown. If any task encounters an |
410 |
< |
* exception, others may be, but are not guaranteed to be, |
411 |
< |
* cancelled. If more than one task encounters an exception, then |
412 |
< |
* this method throws any one of these exceptions. The individual |
413 |
< |
* status of each task may be checked using {@link #getException()} |
414 |
< |
* and related methods. |
409 |
> |
* case the exception is rethrown. If more than one task |
410 |
> |
* encounters an exception, then this method throws any one of |
411 |
> |
* these exceptions. If any task encounters an exception, others |
412 |
> |
* may be cancelled. However, the execution status of individual |
413 |
> |
* tasks is not guaranteed upon exceptional return. The status of |
414 |
> |
* each task may be obtained using {@link #getException()} and |
415 |
> |
* related methods to check if they have been cancelled, completed |
416 |
> |
* normally or exceptionally, or left unprocessed. |
417 |
|
* |
418 |
|
* <p>This method may be invoked only from within {@code |
419 |
< |
* ForkJoinTask} computations (as may be determined using method |
419 |
> |
* ForkJoinPool} computations (as may be determined using method |
420 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
421 |
|
* result in exceptions or errors, possibly including {@code |
422 |
|
* ClassCastException}. |
435 |
|
} |
436 |
|
else if (i != 0) |
437 |
|
t.fork(); |
438 |
< |
else if (t.doInvoke() < NORMAL && ex == null) |
439 |
< |
ex = t.getException(); |
438 |
> |
else { |
439 |
> |
t.quietlyInvoke(); |
440 |
> |
if (ex == null && t.status < NORMAL) |
441 |
> |
ex = t.getException(); |
442 |
> |
} |
443 |
|
} |
444 |
|
for (int i = 1; i <= last; ++i) { |
445 |
|
ForkJoinTask<?> t = tasks[i]; |
446 |
|
if (t != null) { |
447 |
|
if (ex != null) |
448 |
|
t.cancel(false); |
449 |
< |
else if (t.doJoin() < NORMAL && ex == null) |
450 |
< |
ex = t.getException(); |
449 |
> |
else { |
450 |
> |
t.quietlyJoin(); |
451 |
> |
if (ex == null && t.status < NORMAL) |
452 |
> |
ex = t.getException(); |
453 |
> |
} |
454 |
|
} |
455 |
|
} |
456 |
|
if (ex != null) |
460 |
|
/** |
461 |
|
* Forks all tasks in the specified collection, returning when |
462 |
|
* {@code isDone} holds for each task or an (unchecked) exception |
463 |
< |
* is encountered. If any task encounters an exception, others |
464 |
< |
* may be, but are not guaranteed to be, cancelled. If more than |
465 |
< |
* one task encounters an exception, then this method throws any |
466 |
< |
* one of these exceptions. The individual status of each task |
467 |
< |
* may be checked using {@link #getException()} and related |
468 |
< |
* methods. The behavior of this operation is undefined if the |
469 |
< |
* specified collection is modified while the operation is in |
470 |
< |
* progress. |
463 |
> |
* is encountered, in which case the exception is rethrown. If |
464 |
> |
* more than one task encounters an exception, then this method |
465 |
> |
* throws any one of these exceptions. If any task encounters an |
466 |
> |
* exception, others may be cancelled. However, the execution |
467 |
> |
* status of individual tasks is not guaranteed upon exceptional |
468 |
> |
* return. The status of each task may be obtained using {@link |
469 |
> |
* #getException()} and related methods to check if they have been |
470 |
> |
* cancelled, completed normally or exceptionally, or left |
471 |
> |
* unprocessed. |
472 |
|
* |
473 |
|
* <p>This method may be invoked only from within {@code |
474 |
< |
* ForkJoinTask} computations (as may be determined using method |
474 |
> |
* ForkJoinPool} computations (as may be determined using method |
475 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
476 |
|
* result in exceptions or errors, possibly including {@code |
477 |
|
* ClassCastException}. |
498 |
|
} |
499 |
|
else if (i != 0) |
500 |
|
t.fork(); |
501 |
< |
else if (t.doInvoke() < NORMAL && ex == null) |
502 |
< |
ex = t.getException(); |
501 |
> |
else { |
502 |
> |
t.quietlyInvoke(); |
503 |
> |
if (ex == null && t.status < NORMAL) |
504 |
> |
ex = t.getException(); |
505 |
> |
} |
506 |
|
} |
507 |
|
for (int i = 1; i <= last; ++i) { |
508 |
|
ForkJoinTask<?> t = ts.get(i); |
509 |
|
if (t != null) { |
510 |
|
if (ex != null) |
511 |
|
t.cancel(false); |
512 |
< |
else if (t.doJoin() < NORMAL && ex == null) |
513 |
< |
ex = t.getException(); |
512 |
> |
else { |
513 |
> |
t.quietlyJoin(); |
514 |
> |
if (ex == null && t.status < NORMAL) |
515 |
> |
ex = t.getException(); |
516 |
> |
} |
517 |
|
} |
518 |
|
} |
519 |
|
if (ex != null) |
523 |
|
|
524 |
|
/** |
525 |
|
* Attempts to cancel execution of this task. This attempt will |
526 |
< |
* fail if the task has already completed, has already been |
527 |
< |
* cancelled, or could not be cancelled for some other reason. If |
528 |
< |
* successful, and this task has not started when cancel is |
529 |
< |
* called, execution of this task is suppressed, {@link |
530 |
< |
* #isCancelled} will report true, and {@link #join} will result |
531 |
< |
* in a {@code CancellationException} being thrown. |
526 |
> |
* fail if the task has already completed or could not be |
527 |
> |
* cancelled for some other reason. If successful, and this task |
528 |
> |
* has not started when {@code cancel} is called, execution of |
529 |
> |
* this task is suppressed. After this method returns |
530 |
> |
* successfully, unless there is an intervening call to {@link |
531 |
> |
* #reinitialize}, subsequent calls to {@link #isCancelled}, |
532 |
> |
* {@link #isDone}, and {@code cancel} will return {@code true} |
533 |
> |
* and calls to {@link #join} and related methods will result in |
534 |
> |
* {@code CancellationException}. |
535 |
|
* |
536 |
|
* <p>This method may be overridden in subclasses, but if so, must |
537 |
< |
* still ensure that these minimal properties hold. In particular, |
538 |
< |
* the {@code cancel} method itself must not throw exceptions. |
537 |
> |
* still ensure that these properties hold. In particular, the |
538 |
> |
* {@code cancel} method itself must not throw exceptions. |
539 |
|
* |
540 |
|
* <p>This method is designed to be invoked by <em>other</em> |
541 |
|
* tasks. To terminate the current task, you can just return or |
542 |
|
* throw an unchecked exception from its computation method, or |
543 |
|
* invoke {@link #completeExceptionally}. |
544 |
|
* |
545 |
< |
* @param mayInterruptIfRunning this value is ignored in the |
546 |
< |
* default implementation because tasks are not |
547 |
< |
* cancelled via interruption |
545 |
> |
* @param mayInterruptIfRunning this value has no effect in the |
546 |
> |
* default implementation because interrupts are not used to |
547 |
> |
* control cancellation. |
548 |
|
* |
549 |
|
* @return {@code true} if this task is now cancelled |
550 |
|
*/ |
551 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
552 |
< |
return setCompletion(CANCELLED) == CANCELLED; |
552 |
> |
setCompletion(CANCELLED); |
553 |
> |
return status == CANCELLED; |
554 |
|
} |
555 |
|
|
556 |
|
/** |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
< |
* Cancels ignoring exceptions if worker is terminating |
570 |
> |
* Cancels if current thread is a terminating worker thread, |
571 |
> |
* ignoring any exceptions thrown by cancel. |
572 |
|
*/ |
573 |
|
final void cancelIfTerminating() { |
574 |
|
Thread t = Thread.currentThread(); |
645 |
|
|
646 |
|
/** |
647 |
|
* Completes this task, and if not already aborted or cancelled, |
648 |
< |
* returning a {@code null} result upon {@code join} and related |
649 |
< |
* operations. This method may be used to provide results for |
650 |
< |
* asynchronous tasks, or to provide alternative handling for |
651 |
< |
* tasks that would not otherwise complete normally. Its use in |
652 |
< |
* other situations is discouraged. This method is |
653 |
< |
* overridable, but overridden versions must invoke {@code super} |
654 |
< |
* implementation to maintain guarantees. |
648 |
> |
* returning the given value as the result of subsequent |
649 |
> |
* invocations of {@code join} and related operations. This method |
650 |
> |
* may be used to provide results for asynchronous tasks, or to |
651 |
> |
* provide alternative handling for tasks that would not otherwise |
652 |
> |
* complete normally. Its use in other situations is |
653 |
> |
* discouraged. This method is overridable, but overridden |
654 |
> |
* versions must invoke {@code super} implementation to maintain |
655 |
> |
* guarantees. |
656 |
|
* |
657 |
|
* @param value the result value for this task |
658 |
|
*/ |
666 |
|
setCompletion(NORMAL); |
667 |
|
} |
668 |
|
|
669 |
+ |
/** |
670 |
+ |
* Waits if necessary for the computation to complete, and then |
671 |
+ |
* retrieves its result. |
672 |
+ |
* |
673 |
+ |
* @return the computed result |
674 |
+ |
* @throws CancellationException if the computation was cancelled |
675 |
+ |
* @throws ExecutionException if the computation threw an |
676 |
+ |
* exception |
677 |
+ |
* @throws InterruptedException if the current thread is not a |
678 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
679 |
+ |
*/ |
680 |
|
public final V get() throws InterruptedException, ExecutionException { |
681 |
< |
int s = doJoin(); |
682 |
< |
if (Thread.interrupted()) |
683 |
< |
throw new InterruptedException(); |
681 |
> |
int s; |
682 |
> |
if (Thread.currentThread() instanceof ForkJoinWorkerThread) { |
683 |
> |
quietlyJoin(); |
684 |
> |
s = status; |
685 |
> |
} |
686 |
> |
else { |
687 |
> |
while ((s = status) >= 0) { |
688 |
> |
synchronized (this) { // interruptible form of awaitDone |
689 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, |
690 |
> |
s, SIGNAL)) { |
691 |
> |
while (status >= 0) |
692 |
> |
wait(); |
693 |
> |
} |
694 |
> |
} |
695 |
> |
} |
696 |
> |
} |
697 |
|
if (s < NORMAL) { |
698 |
|
Throwable ex; |
699 |
|
if (s == CANCELLED) |
704 |
|
return getRawResult(); |
705 |
|
} |
706 |
|
|
707 |
+ |
/** |
708 |
+ |
* Waits if necessary for at most the given time for the computation |
709 |
+ |
* to complete, and then retrieves its result, if available. |
710 |
+ |
* |
711 |
+ |
* @param timeout the maximum time to wait |
712 |
+ |
* @param unit the time unit of the timeout argument |
713 |
+ |
* @return the computed result |
714 |
+ |
* @throws CancellationException if the computation was cancelled |
715 |
+ |
* @throws ExecutionException if the computation threw an |
716 |
+ |
* exception |
717 |
+ |
* @throws InterruptedException if the current thread is not a |
718 |
+ |
* member of a ForkJoinPool and was interrupted while waiting |
719 |
+ |
* @throws TimeoutException if the wait timed out |
720 |
+ |
*/ |
721 |
|
public final V get(long timeout, TimeUnit unit) |
722 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
723 |
< |
Thread t = Thread.currentThread(); |
724 |
< |
ForkJoinPool pool; |
725 |
< |
if (t instanceof ForkJoinWorkerThread) { |
726 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
727 |
< |
if (status >= 0 && w.unpushTask(this)) |
728 |
< |
tryExec(); |
729 |
< |
pool = w.pool; |
730 |
< |
} |
731 |
< |
else |
732 |
< |
pool = null; |
733 |
< |
/* |
734 |
< |
* Timed wait loop intermixes cases for FJ (pool != null) and |
735 |
< |
* non FJ threads. For FJ, decrement pool count but don't try |
736 |
< |
* for replacement; increment count on completion. For non-FJ, |
737 |
< |
* deal with interrupts. This is messy, but a little less so |
738 |
< |
* than is splitting the FJ and nonFJ cases. |
739 |
< |
*/ |
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; |
723 |
> |
long nanos = unit.toNanos(timeout); |
724 |
> |
if (status >= 0) { |
725 |
> |
Thread t = Thread.currentThread(); |
726 |
> |
if (t instanceof ForkJoinWorkerThread) { |
727 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
728 |
> |
boolean completed = false; // timed variant of quietlyJoin |
729 |
> |
if (w.unpushTask(this)) { |
730 |
> |
try { |
731 |
> |
completed = exec(); |
732 |
> |
} catch (Throwable rex) { |
733 |
> |
setExceptionalCompletion(rex); |
734 |
> |
} |
735 |
> |
} |
736 |
> |
if (completed) |
737 |
> |
setCompletion(NORMAL); |
738 |
> |
else if (status >= 0) |
739 |
> |
w.joinTask(this, true, nanos); |
740 |
|
} |
741 |
< |
int s = status; |
742 |
< |
if (s < 0) |
743 |
< |
break; |
692 |
< |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, SIGNAL)) { |
741 |
> |
else if (Thread.interrupted()) |
742 |
> |
throw new InterruptedException(); |
743 |
> |
else { |
744 |
|
long startTime = System.nanoTime(); |
745 |
< |
long nanos = unit.toNanos(timeout); |
746 |
< |
long nt; // wait time |
696 |
< |
while (status >= 0 && |
745 |
> |
int s; long nt; |
746 |
> |
while ((s = status) >= 0 && |
747 |
|
(nt = nanos - (System.nanoTime() - startTime)) > 0) { |
748 |
< |
if (pool != null && !dec) |
749 |
< |
dec = pool.tryDecrementRunningCount(); |
700 |
< |
else { |
748 |
> |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, |
749 |
> |
SIGNAL)) { |
750 |
|
long ms = nt / 1000000; |
751 |
|
int ns = (int) (nt % 1000000); |
752 |
< |
try { |
753 |
< |
synchronized(this) { |
754 |
< |
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 |
< |
} |
752 |
> |
synchronized (this) { |
753 |
> |
if (status >= 0) |
754 |
> |
wait(ms, ns); // exit on IE throw |
755 |
|
} |
756 |
|
} |
757 |
|
} |
718 |
– |
break; |
758 |
|
} |
759 |
|
} |
721 |
– |
if (pool != null && dec) |
722 |
– |
pool.incrementRunningCount(); |
723 |
– |
if (interrupted) |
724 |
– |
throw new InterruptedException(); |
760 |
|
int es = status; |
761 |
|
if (es != NORMAL) { |
762 |
|
Throwable ex; |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* Joins this task, without returning its result or throwing an |
773 |
> |
* Joins this task, without returning its result or throwing its |
774 |
|
* exception. This method may be useful when processing |
775 |
|
* collections of tasks when some have been cancelled or otherwise |
776 |
|
* known to have aborted. |
777 |
|
*/ |
778 |
|
public final void quietlyJoin() { |
779 |
< |
doJoin(); |
779 |
> |
Thread t; |
780 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
781 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
782 |
> |
if (status >= 0) { |
783 |
> |
if (w.unpushTask(this)) { |
784 |
> |
boolean completed; |
785 |
> |
try { |
786 |
> |
completed = exec(); |
787 |
> |
} catch (Throwable rex) { |
788 |
> |
setExceptionalCompletion(rex); |
789 |
> |
return; |
790 |
> |
} |
791 |
> |
if (completed) { |
792 |
> |
setCompletion(NORMAL); |
793 |
> |
return; |
794 |
> |
} |
795 |
> |
} |
796 |
> |
w.joinTask(this, false, 0L); |
797 |
> |
} |
798 |
> |
} |
799 |
> |
else |
800 |
> |
externalAwaitDone(); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* Commences performing this task and awaits its completion if |
805 |
< |
* necessary, without returning its result or throwing an |
806 |
< |
* exception. This method may be useful when processing |
751 |
< |
* collections of tasks when some have been cancelled or otherwise |
752 |
< |
* known to have aborted. |
805 |
> |
* necessary, without returning its result or throwing its |
806 |
> |
* exception. |
807 |
|
*/ |
808 |
|
public final void quietlyInvoke() { |
809 |
< |
doInvoke(); |
809 |
> |
if (status >= 0) { |
810 |
> |
boolean completed; |
811 |
> |
try { |
812 |
> |
completed = exec(); |
813 |
> |
} catch (Throwable rex) { |
814 |
> |
setExceptionalCompletion(rex); |
815 |
> |
return; |
816 |
> |
} |
817 |
> |
if (completed) |
818 |
> |
setCompletion(NORMAL); |
819 |
> |
else |
820 |
> |
quietlyJoin(); |
821 |
> |
} |
822 |
|
} |
823 |
|
|
824 |
|
/** |
829 |
|
* processed. |
830 |
|
* |
831 |
|
* <p>This method may be invoked only from within {@code |
832 |
< |
* ForkJoinTask} computations (as may be determined using method |
832 |
> |
* ForkJoinPool} computations (as may be determined using method |
833 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
834 |
|
* result in exceptions or errors, possibly including {@code |
835 |
|
* ClassCastException}. |
848 |
|
* under any other usage conditions are not guaranteed. |
849 |
|
* This method may be useful when executing |
850 |
|
* pre-constructed trees of subtasks in loops. |
851 |
+ |
* |
852 |
+ |
* <p>Upon completion of this method, {@code isDone()} reports |
853 |
+ |
* {@code false}, and {@code getException()} reports {@code |
854 |
+ |
* null}. However, the value returned by {@code getRawResult} is |
855 |
+ |
* unaffected. To clear this value, you can invoke {@code |
856 |
+ |
* setRawResult(null)}. |
857 |
|
*/ |
858 |
|
public void reinitialize() { |
859 |
|
if (status == EXCEPTIONAL) |
894 |
|
* were not, stolen. |
895 |
|
* |
896 |
|
* <p>This method may be invoked only from within {@code |
897 |
< |
* ForkJoinTask} computations (as may be determined using method |
897 |
> |
* ForkJoinPool} computations (as may be determined using method |
898 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
899 |
|
* result in exceptions or errors, possibly including {@code |
900 |
|
* ClassCastException}. |
913 |
|
* fork other tasks. |
914 |
|
* |
915 |
|
* <p>This method may be invoked only from within {@code |
916 |
< |
* ForkJoinTask} computations (as may be determined using method |
916 |
> |
* ForkJoinPool} computations (as may be determined using method |
917 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
918 |
|
* result in exceptions or errors, possibly including {@code |
919 |
|
* ClassCastException}. |
936 |
|
* exceeded. |
937 |
|
* |
938 |
|
* <p>This method may be invoked only from within {@code |
939 |
< |
* ForkJoinTask} computations (as may be determined using method |
939 |
> |
* ForkJoinPool} computations (as may be determined using method |
940 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
941 |
|
* result in exceptions or errors, possibly including {@code |
942 |
|
* ClassCastException}. |
994 |
|
* otherwise. |
995 |
|
* |
996 |
|
* <p>This method may be invoked only from within {@code |
997 |
< |
* ForkJoinTask} computations (as may be determined using method |
997 |
> |
* ForkJoinPool} computations (as may be determined using method |
998 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
999 |
|
* result in exceptions or errors, possibly including {@code |
1000 |
|
* ClassCastException}. |
1013 |
|
* be useful otherwise. |
1014 |
|
* |
1015 |
|
* <p>This method may be invoked only from within {@code |
1016 |
< |
* ForkJoinTask} computations (as may be determined using method |
1016 |
> |
* ForkJoinPool} computations (as may be determined using method |
1017 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1018 |
|
* result in exceptions or errors, possibly including {@code |
1019 |
|
* ClassCastException}. |
1036 |
|
* otherwise. |
1037 |
|
* |
1038 |
|
* <p>This method may be invoked only from within {@code |
1039 |
< |
* ForkJoinTask} computations (as may be determined using method |
1039 |
> |
* ForkJoinPool} computations (as may be determined using method |
1040 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1041 |
|
* result in exceptions or errors, possibly including {@code |
1042 |
|
* ClassCastException}. |
1146 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1147 |
|
|
1148 |
|
/** |
1149 |
< |
* Saves the state to a stream. |
1149 |
> |
* Saves the state to a stream (that is, serializes it). |
1150 |
|
* |
1151 |
|
* @serialData the current run status and the exception thrown |
1152 |
|
* during execution, or {@code null} if none |
1159 |
|
} |
1160 |
|
|
1161 |
|
/** |
1162 |
< |
* Reconstitutes the instance from a stream. |
1162 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1163 |
|
* |
1164 |
|
* @param s the stream |
1165 |
|
*/ |