87 |
|
* exception (in which case {@link #getException} returns the |
88 |
|
* exception); {@link #isCancelled} is true if the task was cancelled |
89 |
|
* (in which case {@link #getException} returns a {@link |
90 |
< |
* java.util.concurrent.CancellationException}; and {@link |
90 |
> |
* java.util.concurrent.CancellationException}); and {@link |
91 |
|
* #isCompletedAbnormally} is true if a task was either cancelled or |
92 |
< |
* encountered an exception. |
92 |
> |
* encountered an exception. |
93 |
|
* |
94 |
|
* <p>The ForkJoinTask class is not usually directly subclassed. |
95 |
|
* Instead, you subclass one of the abstract classes that support a |
125 |
|
* improve throughput. If too small, then memory and internal task |
126 |
|
* maintenance overhead may overwhelm processing. |
127 |
|
* |
128 |
< |
* <p>This class provides {@code adapt} methods for {@link |
129 |
< |
* java.lang.Runnable} and {@link java.util.concurrent.Callable}, that |
130 |
< |
* may be of use when mixing execution of ForkJoinTasks with other |
131 |
< |
* kinds of tasks. When all tasks are of this form, consider using a |
132 |
< |
* pool in {@link ForkJoinPool#setAsyncMode async mode}. |
128 |
> |
* <p>This class provides {@code adapt} methods for {@link Runnable} |
129 |
> |
* and {@link Callable}, that may be of use when mixing execution of |
130 |
> |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks |
131 |
> |
* are of this form, consider using a pool in |
132 |
> |
* {@linkplain ForkJoinPool#setAsyncMode async mode}. |
133 |
|
* |
134 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
135 |
|
* used in extensions such as remote execution frameworks. It is |
392 |
|
|
393 |
|
/** |
394 |
|
* Returns result or throws exception using j.u.c.Future conventions. |
395 |
< |
* Only call when {@code isDone} known to be true. |
395 |
> |
* Only call when {@code isDone} known to be true or thread known |
396 |
> |
* to be interrupted. |
397 |
|
*/ |
398 |
|
private V reportFutureResult() |
399 |
< |
throws ExecutionException, InterruptedException { |
399 |
> |
throws InterruptedException, ExecutionException { |
400 |
|
if (Thread.interrupted()) |
401 |
|
throw new InterruptedException(); |
402 |
|
int s = status & COMPLETION_MASK; |
422 |
|
int s = status & COMPLETION_MASK; |
423 |
|
if (s == NORMAL) |
424 |
|
return getRawResult(); |
425 |
< |
if (s == CANCELLED) |
425 |
> |
else if (s == CANCELLED) |
426 |
|
throw new CancellationException(); |
427 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
427 |
> |
else if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
428 |
|
throw new ExecutionException(ex); |
429 |
< |
throw new TimeoutException(); |
429 |
> |
else |
430 |
> |
throw new TimeoutException(); |
431 |
|
} |
432 |
|
|
433 |
|
// internal execution methods |
560 |
|
/** |
561 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
562 |
|
* each task or an (unchecked) exception is encountered, in which |
563 |
< |
* case the exception is rethrown. If more than one task |
564 |
< |
* encounters an exception, then this method throws any one of |
565 |
< |
* these exceptions. The individual status of each task may be |
563 |
> |
* case the exception is rethrown. If either task encounters an |
564 |
> |
* exception, the other one may be, but is not guaranteed to be, |
565 |
> |
* cancelled. If both tasks throw an exception, then this method |
566 |
> |
* throws one of them. The individual status of each task may be |
567 |
|
* checked using {@link #getException()} and related methods. |
568 |
|
* |
569 |
|
* <p>This method may be invoked only from within {@code |
726 |
|
return (status & COMPLETION_MASK) == CANCELLED; |
727 |
|
} |
728 |
|
|
726 |
– |
/** |
727 |
– |
* Returns {@code true} if the computation performed by this task |
728 |
– |
* has completed (or has been cancelled). |
729 |
– |
* |
730 |
– |
* @return {@code true} if this computation has completed |
731 |
– |
*/ |
729 |
|
public final boolean isDone() { |
730 |
|
return status < 0; |
731 |
|
} |
732 |
|
|
736 |
– |
/** |
737 |
– |
* Returns {@code true} if this task was cancelled. |
738 |
– |
* |
739 |
– |
* @return {@code true} if this task was cancelled |
740 |
– |
*/ |
733 |
|
public final boolean isCancelled() { |
734 |
|
return (status & COMPLETION_MASK) == CANCELLED; |
735 |
|
} |
772 |
|
*/ |
773 |
|
public final Throwable getException() { |
774 |
|
int s = status & COMPLETION_MASK; |
775 |
< |
if (s >= NORMAL) |
776 |
< |
return null; |
777 |
< |
if (s == CANCELLED) |
786 |
< |
return new CancellationException(); |
787 |
< |
return exceptionMap.get(this); |
775 |
> |
return ((s >= NORMAL) ? null : |
776 |
> |
(s == CANCELLED) ? new CancellationException() : |
777 |
> |
exceptionMap.get(this)); |
778 |
|
} |
779 |
|
|
780 |
|
/** |