115 |
|
* java.lang.Runnable} and {@link java.util.concurrent.Callable}, that |
116 |
|
* may be of use when mixing execution of ForkJoinTasks with other |
117 |
|
* kinds of tasks. When all tasks are of this form, consider using a |
118 |
< |
* pool in {@link ForkJoinPool#setAsyncMode}. |
118 |
> |
* pool in {@link ForkJoinPool#setAsyncMode async mode}. |
119 |
|
* |
120 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
121 |
|
* used in extensions such as remote execution frameworks. It is |
382 |
|
*/ |
383 |
|
private V reportFutureResult() |
384 |
|
throws ExecutionException, InterruptedException { |
385 |
+ |
if (Thread.interrupted()) |
386 |
+ |
throw new InterruptedException(); |
387 |
|
int s = status & COMPLETION_MASK; |
388 |
|
if (s < NORMAL) { |
389 |
|
Throwable ex; |
391 |
|
throw new CancellationException(); |
392 |
|
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
393 |
|
throw new ExecutionException(ex); |
392 |
– |
if (Thread.interrupted()) |
393 |
– |
throw new InterruptedException(); |
394 |
|
} |
395 |
|
return getRawResult(); |
396 |
|
} |
401 |
|
*/ |
402 |
|
private V reportTimedFutureResult() |
403 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
404 |
+ |
if (Thread.interrupted()) |
405 |
+ |
throw new InterruptedException(); |
406 |
|
Throwable ex; |
407 |
|
int s = status & COMPLETION_MASK; |
408 |
|
if (s == NORMAL) |
411 |
|
throw new CancellationException(); |
412 |
|
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
413 |
|
throw new ExecutionException(ex); |
412 |
– |
if (Thread.interrupted()) |
413 |
– |
throw new InterruptedException(); |
414 |
|
throw new TimeoutException(); |
415 |
|
} |
416 |
|
|
529 |
|
|
530 |
|
/** |
531 |
|
* Commences performing this task, awaits its completion if |
532 |
< |
* necessary, and return its result. |
532 |
> |
* necessary, and return its result, or throws an (unchecked) |
533 |
> |
* exception if the underlying computation did so. |
534 |
|
* |
534 |
– |
* @throws Throwable (a RuntimeException, Error, or unchecked |
535 |
– |
* exception) if the underlying computation did so |
535 |
|
* @return the computed result |
536 |
|
*/ |
537 |
|
public final V invoke() { |
542 |
|
} |
543 |
|
|
544 |
|
/** |
545 |
< |
* Forks the given tasks, returning when {@code isDone} holds |
546 |
< |
* for each task or an exception is encountered. |
545 |
> |
* Forks the given tasks, returning when {@code isDone} holds for |
546 |
> |
* each task or an (unchecked) exception is encountered, in which |
547 |
> |
* case the exception is rethrown. If more than one task |
548 |
> |
* encounters an exception, then this method throws any one of |
549 |
> |
* these exceptions. The individual status of each task may be |
550 |
> |
* checked using {@link #getException()} and related methods. |
551 |
|
* |
552 |
|
* <p>This method may be invoked only from within {@code |
553 |
|
* ForkJoinTask} computations (as may be determined using method |
558 |
|
* @param t1 the first task |
559 |
|
* @param t2 the second task |
560 |
|
* @throws NullPointerException if any task is null |
558 |
– |
* @throws RuntimeException or Error if a task did so |
561 |
|
*/ |
562 |
|
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) { |
563 |
|
t2.fork(); |
567 |
|
|
568 |
|
/** |
569 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
570 |
< |
* each task or an exception is encountered. If any task |
571 |
< |
* encounters an exception, others may be, but are not guaranteed |
572 |
< |
* to be, cancelled. |
570 |
> |
* each task or an (unchecked) exception is encountered, in which |
571 |
> |
* case the exception is rethrown. If any task encounters an |
572 |
> |
* exception, others may be, but are not guaranteed to be, |
573 |
> |
* cancelled. If more than one task encounters an exception, then |
574 |
> |
* this method throws any one of these exceptions. The individual |
575 |
> |
* status of each task may be checked using {@link #getException()} |
576 |
> |
* and related methods. |
577 |
|
* |
578 |
|
* <p>This method may be invoked only from within {@code |
579 |
|
* ForkJoinTask} computations (as may be determined using method |
582 |
|
* ClassCastException}. |
583 |
|
* |
584 |
|
* @param tasks the tasks |
585 |
< |
* @throws NullPointerException if tasks or any element are null |
580 |
< |
* @throws RuntimeException or Error if any task did so |
585 |
> |
* @throws NullPointerException if any task is null |
586 |
|
*/ |
587 |
|
public static void invokeAll(ForkJoinTask<?>... tasks) { |
588 |
|
Throwable ex = null; |
619 |
|
|
620 |
|
/** |
621 |
|
* Forks all tasks in the specified collection, returning when |
622 |
< |
* {@code isDone} holds for each task or an exception is |
623 |
< |
* encountered. If any task encounters an exception, others may |
624 |
< |
* be, but are not guaranteed to be, cancelled. The behavior of |
625 |
< |
* this operation is undefined if the specified collection is |
626 |
< |
* modified while the operation is in progress. |
622 |
> |
* {@code isDone} holds for each task or an (unchecked) exception |
623 |
> |
* is encountered. If any task encounters an exception, others |
624 |
> |
* may be, but are not guaranteed to be, cancelled. If more than |
625 |
> |
* one task encounters an exception, then this method throws any |
626 |
> |
* one of these exceptions. The individual status of each task |
627 |
> |
* may be checked using {@link #getException()} and related |
628 |
> |
* methods. The behavior of this operation is undefined if the |
629 |
> |
* specified collection is modified while the operation is in |
630 |
> |
* progress. |
631 |
|
* |
632 |
|
* <p>This method may be invoked only from within {@code |
633 |
|
* ForkJoinTask} computations (as may be determined using method |
638 |
|
* @param tasks the collection of tasks |
639 |
|
* @return the tasks argument, to simplify usage |
640 |
|
* @throws NullPointerException if tasks or any element are null |
632 |
– |
* @throws RuntimeException or Error if any task did so |
641 |
|
*/ |
642 |
|
public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) { |
643 |
|
if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) { |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
< |
* Returns {@code true} if the computation performed by this task |
685 |
< |
* has completed (or has been cancelled). |
686 |
< |
* |
687 |
< |
* @return {@code true} if this computation has completed |
688 |
< |
*/ |
689 |
< |
public final boolean isDone() { |
690 |
< |
return status < 0; |
683 |
< |
} |
684 |
< |
|
685 |
< |
/** |
686 |
< |
* Returns {@code true} if this task was cancelled. |
687 |
< |
* |
688 |
< |
* @return {@code true} if this task was cancelled |
689 |
< |
*/ |
690 |
< |
public final boolean isCancelled() { |
691 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
692 |
< |
} |
693 |
< |
|
694 |
< |
/** |
695 |
< |
* Asserts that the results of this task's computation will not be |
696 |
< |
* used. If a cancellation occurs before attempting to execute this |
697 |
< |
* task, execution will be suppressed, {@link #isCancelled} |
698 |
< |
* will report true, and {@link #join} will result in a |
699 |
< |
* {@code CancellationException} being thrown. Otherwise, when |
700 |
< |
* cancellation races with completion, there are no guarantees |
701 |
< |
* about whether {@code isCancelled} will report {@code true}, |
702 |
< |
* whether {@code join} will return normally or via an exception, |
703 |
< |
* or whether these behaviors will remain consistent upon repeated |
704 |
< |
* invocation. |
684 |
> |
* Attempts to cancel execution of this task. This attempt will |
685 |
> |
* fail if the task has already completed, has already been |
686 |
> |
* cancelled, or could not be cancelled for some other reason. If |
687 |
> |
* successful, and this task has not started when cancel is |
688 |
> |
* called, execution of this task is suppressed, {@link |
689 |
> |
* #isCancelled} will report true, and {@link #join} will result |
690 |
> |
* in a {@code CancellationException} being thrown. |
691 |
|
* |
692 |
|
* <p>This method may be overridden in subclasses, but if so, must |
693 |
|
* still ensure that these minimal properties hold. In particular, |
699 |
|
* invoke {@link #completeExceptionally}. |
700 |
|
* |
701 |
|
* @param mayInterruptIfRunning this value is ignored in the |
702 |
< |
* default implementation because tasks are not in general |
702 |
> |
* default implementation because tasks are not |
703 |
|
* cancelled via interruption |
704 |
|
* |
705 |
|
* @return {@code true} if this task is now cancelled |
710 |
|
} |
711 |
|
|
712 |
|
/** |
713 |
+ |
* Returns {@code true} if the computation performed by this task |
714 |
+ |
* has completed (or has been cancelled). |
715 |
+ |
* |
716 |
+ |
* @return {@code true} if this computation has completed |
717 |
+ |
*/ |
718 |
+ |
public final boolean isDone() { |
719 |
+ |
return status < 0; |
720 |
+ |
} |
721 |
+ |
|
722 |
+ |
/** |
723 |
+ |
* Returns {@code true} if this task was cancelled. |
724 |
+ |
* |
725 |
+ |
* @return {@code true} if this task was cancelled |
726 |
+ |
*/ |
727 |
+ |
public final boolean isCancelled() { |
728 |
+ |
return (status & COMPLETION_MASK) == CANCELLED; |
729 |
+ |
} |
730 |
+ |
|
731 |
+ |
/** |
732 |
|
* Returns {@code true} if this task threw an exception or was cancelled. |
733 |
|
* |
734 |
|
* @return {@code true} if this task threw an exception or was cancelled |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
+ |
* Returns {@code true} if this task completed without throwing an |
742 |
+ |
* exception and was not cancelled. |
743 |
+ |
* |
744 |
+ |
* @return {@code true} if this task completed without throwing an |
745 |
+ |
* exception and was not cancelled |
746 |
+ |
*/ |
747 |
+ |
public final boolean isCompletedNormally() { |
748 |
+ |
return (status & COMPLETION_MASK) == NORMAL; |
749 |
+ |
} |
750 |
+ |
|
751 |
+ |
/** |
752 |
+ |
* Returns {@code true} if this task threw an exception. |
753 |
+ |
* |
754 |
+ |
* @return {@code true} if this task threw an exception |
755 |
+ |
*/ |
756 |
+ |
public final boolean isCompletedExceptionally() { |
757 |
+ |
return (status & COMPLETION_MASK) == EXCEPTIONAL; |
758 |
+ |
} |
759 |
+ |
|
760 |
+ |
/** |
761 |
|
* Returns the exception thrown by the base computation, or a |
762 |
|
* {@code CancellationException} if cancelled, or {@code null} if |
763 |
|
* none or if the method has not yet completed. |
903 |
|
|
904 |
|
/** |
905 |
|
* Possibly executes tasks until the pool hosting the current task |
906 |
< |
* {@link ForkJoinPool#isQuiescent}. This method may be of use in |
907 |
< |
* designs in which many tasks are forked, but none are explicitly |
908 |
< |
* joined, instead executing them until all are processed. |
906 |
> |
* {@link ForkJoinPool#isQuiescent is quiescent}. This method may |
907 |
> |
* be of use in designs in which many tasks are forked, but none |
908 |
> |
* are explicitly joined, instead executing them until all are |
909 |
> |
* processed. |
910 |
|
* |
911 |
|
* <p>This method may be invoked only from within {@code |
912 |
|
* ForkJoinTask} computations (as may be determined using method |
1050 |
|
* called otherwise. The return value controls whether this task |
1051 |
|
* is considered to be done normally. It may return false in |
1052 |
|
* asynchronous actions that require explicit invocations of |
1053 |
< |
* {@link #complete} to become joinable. It may throw exceptions |
1054 |
< |
* to indicate abnormal exit. |
1053 |
> |
* {@link #complete} to become joinable. It may also throw an |
1054 |
> |
* (unchecked) exception to indicate abnormal exit. |
1055 |
|
* |
1056 |
|
* @return {@code true} if completed normally |
1031 |
– |
* @throws Error or RuntimeException if encountered during computation |
1057 |
|
*/ |
1058 |
|
protected abstract boolean exec(); |
1059 |
|
|