496 |
|
/** |
497 |
|
* Arranges to asynchronously execute this task. While it is not |
498 |
|
* necessarily enforced, it is a usage error to fork a task more |
499 |
< |
* than once unless it has completed and been reinitialized. This |
500 |
< |
* method may be invoked only from within ForkJoinTask |
501 |
< |
* computations (as may be determined using method {@link |
502 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
503 |
< |
* in exceptions or errors, possibly including ClassCastException. |
499 |
> |
* than once unless it has completed and been reinitialized. |
500 |
|
* |
501 |
< |
* @return {@code this}, to simplify usage. |
501 |
> |
* <p>This method may be invoked only from within {@code |
502 |
> |
* ForkJoinTask} computations (as may be determined using method |
503 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
504 |
> |
* result in exceptions or errors, possibly including {@code |
505 |
> |
* ClassCastException}. |
506 |
> |
* |
507 |
> |
* @return {@code this}, to simplify usage |
508 |
|
*/ |
509 |
|
public final ForkJoinTask<V> fork() { |
510 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
514 |
|
|
515 |
|
/** |
516 |
|
* Returns the result of the computation when it is ready. |
517 |
< |
* This method differs from {@code get} in that abnormal |
518 |
< |
* completion results in RuntimeExceptions or Errors, not |
519 |
< |
* ExecutionExceptions. |
517 |
> |
* This method differs from {@link #get()} in that |
518 |
> |
* abnormal completion results in {@code RuntimeException} or |
519 |
> |
* {@code Error}, not {@code ExecutionException}. |
520 |
|
* |
521 |
|
* @return the computed result |
522 |
|
*/ |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
< |
* Forks the given tasks, returning when {@code isDone} holds for |
547 |
< |
* each task or an exception is encountered. This method may be |
548 |
< |
* invoked only from within ForkJoinTask computations (as may be |
549 |
< |
* determined using method {@link #inForkJoinPool}). Attempts to |
550 |
< |
* invoke in other contexts result in exceptions or errors, |
551 |
< |
* possibly including ClassCastException. |
546 |
> |
* Forks the given tasks, returning when {@code isDone} holds |
547 |
> |
* for each task or an exception is encountered. |
548 |
> |
* |
549 |
> |
* <p>This method may be invoked only from within {@code |
550 |
> |
* ForkJoinTask} computations (as may be determined using method |
551 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
552 |
> |
* result in exceptions or errors, possibly including {@code |
553 |
> |
* ClassCastException}. |
554 |
|
* |
555 |
|
* @param t1 the first task |
556 |
|
* @param t2 the second task |
557 |
|
* @throws NullPointerException if any task is null |
558 |
|
* @throws RuntimeException or Error if a task did so |
559 |
|
*/ |
560 |
< |
public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) { |
560 |
> |
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) { |
561 |
|
t2.fork(); |
562 |
|
t1.invoke(); |
563 |
|
t2.join(); |
567 |
|
* Forks the given tasks, returning when {@code isDone} holds for |
568 |
|
* each task or an exception is encountered. If any task |
569 |
|
* encounters an exception, others may be, but are not guaranteed |
570 |
< |
* to be, cancelled. This method may be invoked only from within |
571 |
< |
* ForkJoinTask computations (as may be determined using method |
572 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
573 |
< |
* result in exceptions or errors, possibly including |
574 |
< |
* ClassCastException. |
570 |
> |
* to be, cancelled. |
571 |
> |
* |
572 |
> |
* <p>This method may be invoked only from within {@code |
573 |
> |
* ForkJoinTask} computations (as may be determined using method |
574 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
575 |
> |
* result in exceptions or errors, possibly including {@code |
576 |
> |
* ClassCastException}. |
577 |
|
* |
578 |
< |
* Overloadings of this method exist for the special cases |
578 |
> |
* <p>Overloadings of this method exist for the special cases |
579 |
|
* of one to four arguments. |
580 |
|
* |
581 |
|
* @param tasks the tasks |
617 |
|
|
618 |
|
/** |
619 |
|
* Forks all tasks in the collection, returning when {@code |
620 |
< |
* isDone} holds for each task or an exception is encountered. If |
621 |
< |
* any task encounters an exception, others may be, but are not |
622 |
< |
* guaranteed to be, cancelled. This method may be invoked only |
623 |
< |
* from within ForkJoinTask computations (as may be determined |
624 |
< |
* using method {@link #inForkJoinPool}). Attempts to invoke in |
625 |
< |
* other contexts result in exceptions or errors, possibly |
626 |
< |
* including ClassCastException. |
620 |
> |
* isDone} holds for each task or an exception is encountered. |
621 |
> |
* If any task encounters an exception, others may be, but are |
622 |
> |
* not guaranteed to be, cancelled. |
623 |
> |
* |
624 |
> |
* <p>This method may be invoked only from within {@code |
625 |
> |
* ForkJoinTask} computations (as may be determined using method |
626 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
627 |
> |
* result in exceptions or errors, possibly including {@code |
628 |
> |
* ClassCastException}. |
629 |
|
* |
630 |
|
* @param tasks the collection of tasks |
631 |
|
* @return the tasks argument, to simplify usage |
814 |
|
* there are no potential dependencies between continuation of the |
815 |
|
* current task and that of any other task that might be executed |
816 |
|
* while helping. (This usually holds for pure divide-and-conquer |
817 |
< |
* tasks). This method may be invoked only from within |
818 |
< |
* ForkJoinTask computations (as may be determined using method |
819 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
820 |
< |
* result in exceptions or errors, possibly including |
821 |
< |
* ClassCastException. |
817 |
> |
* tasks). |
818 |
> |
* |
819 |
> |
* <p>This method may be invoked only from within {@code |
820 |
> |
* ForkJoinTask} computations (as may be determined using method |
821 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
822 |
> |
* result in exceptions or errors, possibly including {@code |
823 |
> |
* ClassCastException}. |
824 |
|
* |
825 |
|
* @return the computed result |
826 |
|
*/ |
832 |
|
} |
833 |
|
|
834 |
|
/** |
835 |
< |
* Possibly executes other tasks until this task is ready. This |
836 |
< |
* method may be invoked only from within ForkJoinTask |
837 |
< |
* computations (as may be determined using method {@link |
838 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
839 |
< |
* in exceptions or errors, possibly including ClassCastException. |
835 |
> |
* Possibly executes other tasks until this task is ready. |
836 |
> |
* |
837 |
> |
* <p>This method may be invoked only from within {@code |
838 |
> |
* ForkJoinTask} computations (as may be determined using method |
839 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
840 |
> |
* result in exceptions or errors, possibly including {@code |
841 |
> |
* ClassCastException}. |
842 |
|
*/ |
843 |
|
public final void quietlyHelpJoin() { |
844 |
|
if (status >= 0) { |
879 |
|
* Possibly executes tasks until the pool hosting the current task |
880 |
|
* {@link ForkJoinPool#isQuiescent}. This method may be of use in |
881 |
|
* designs in which many tasks are forked, but none are explicitly |
882 |
< |
* joined, instead executing them until all are processed. This |
883 |
< |
* method may be invoked only from within ForkJoinTask |
884 |
< |
* computations (as may be determined using method {@link |
885 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
886 |
< |
* in exceptions or errors, possibly including ClassCastException. |
882 |
> |
* joined, instead executing them until all are processed. |
883 |
> |
* |
884 |
> |
* <p>This method may be invoked only from within {@code |
885 |
> |
* ForkJoinTask} computations (as may be determined using method |
886 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
887 |
> |
* result in exceptions or errors, possibly including {@code |
888 |
> |
* ClassCastException}. |
889 |
|
*/ |
890 |
|
public static void helpQuiesce() { |
891 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
898 |
|
* this task, but only if reuse occurs when this task has either |
899 |
|
* never been forked, or has been forked, then completed and all |
900 |
|
* outstanding joins of this task have also completed. Effects |
901 |
< |
* under any other usage conditions are not guaranteed, and are |
902 |
< |
* discouraged. This method may be useful when executing |
901 |
> |
* under any other usage conditions are not guaranteed. |
902 |
> |
* This method may be useful when executing |
903 |
|
* pre-constructed trees of subtasks in loops. |
904 |
|
*/ |
905 |
|
public void reinitialize() { |
938 |
|
* by the current thread, and has not commenced executing in |
939 |
|
* another thread. This method may be useful when arranging |
940 |
|
* alternative local processing of tasks that could have been, but |
941 |
< |
* were not, stolen. This method may be invoked only from within |
942 |
< |
* ForkJoinTask computations (as may be determined using method |
943 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
944 |
< |
* result in exceptions or errors, possibly including |
945 |
< |
* ClassCastException. |
941 |
> |
* were not, stolen. |
942 |
> |
* |
943 |
> |
* <p>This method may be invoked only from within {@code |
944 |
> |
* ForkJoinTask} computations (as may be determined using method |
945 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
946 |
> |
* result in exceptions or errors, possibly including {@code |
947 |
> |
* ClassCastException}. |
948 |
|
* |
949 |
|
* @return {@code true} if unforked |
950 |
|
*/ |
957 |
|
* Returns an estimate of the number of tasks that have been |
958 |
|
* forked by the current worker thread but not yet executed. This |
959 |
|
* value may be useful for heuristic decisions about whether to |
960 |
< |
* fork other tasks. This method may be invoked only from within |
961 |
< |
* ForkJoinTask computations (as may be determined using method |
962 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
963 |
< |
* result in exceptions or errors, possibly including |
964 |
< |
* ClassCastException. |
960 |
> |
* fork other tasks. |
961 |
> |
* |
962 |
> |
* <p>This method may be invoked only from within {@code |
963 |
> |
* ForkJoinTask} computations (as may be determined using method |
964 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
965 |
> |
* result in exceptions or errors, possibly including {@code |
966 |
> |
* ClassCastException}. |
967 |
> |
* |
968 |
|
* @return the number of tasks |
969 |
|
*/ |
970 |
|
public static int getQueuedTaskCount() { |
980 |
|
* usages of ForkJoinTasks, at steady state, each worker should |
981 |
|
* aim to maintain a small constant surplus (for example, 3) of |
982 |
|
* tasks, and to process computations locally if this threshold is |
983 |
< |
* exceeded. This method may be invoked only from within |
984 |
< |
* ForkJoinTask computations (as may be determined using method |
985 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
986 |
< |
* result in exceptions or errors, possibly including |
987 |
< |
* ClassCastException. * |
983 |
> |
* exceeded. |
984 |
> |
* |
985 |
> |
* <p>This method may be invoked only from within {@code |
986 |
> |
* ForkJoinTask} computations (as may be determined using method |
987 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
988 |
> |
* result in exceptions or errors, possibly including {@code |
989 |
> |
* ClassCastException}. |
990 |
> |
* |
991 |
|
* @return the surplus number of tasks, which may be negative |
992 |
|
*/ |
993 |
|
public static int getSurplusQueuedTaskCount() { |
1039 |
|
* null even if a task exists but cannot be accessed without |
1040 |
|
* contention with other threads. This method is designed |
1041 |
|
* primarily to support extensions, and is unlikely to be useful |
1042 |
< |
* otherwise. This method may be invoked only from within |
1043 |
< |
* ForkJoinTask computations (as may be determined using method |
1044 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1045 |
< |
* result in exceptions or errors, possibly including |
1046 |
< |
* ClassCastException. |
1042 |
> |
* otherwise. |
1043 |
> |
* |
1044 |
> |
* <p>This method may be invoked only from within {@code |
1045 |
> |
* ForkJoinTask} computations (as may be determined using method |
1046 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1047 |
> |
* result in exceptions or errors, possibly including {@code |
1048 |
> |
* ClassCastException}. |
1049 |
|
* |
1050 |
|
* @return the next task, or {@code null} if none are available |
1051 |
|
*/ |
1058 |
|
* Unschedules and returns, without executing, the next task |
1059 |
|
* queued by the current thread but not yet executed. This method |
1060 |
|
* is designed primarily to support extensions, and is unlikely to |
1061 |
< |
* be useful otherwise. This method may be invoked only from |
1062 |
< |
* within ForkJoinTask computations (as may be determined using |
1063 |
< |
* method {@link #inForkJoinPool}). Attempts to invoke in other |
1064 |
< |
* contexts result in exceptions or errors, possibly including |
1065 |
< |
* ClassCastException. |
1061 |
> |
* be useful otherwise. |
1062 |
> |
* |
1063 |
> |
* <p>This method may be invoked only from within {@code |
1064 |
> |
* ForkJoinTask} computations (as may be determined using method |
1065 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1066 |
> |
* result in exceptions or errors, possibly including {@code |
1067 |
> |
* ClassCastException}. |
1068 |
|
* |
1069 |
|
* @return the next task, or {@code null} if none are available |
1070 |
|
*/ |
1081 |
|
* {@code null} result does not necessarily imply quiescence |
1082 |
|
* of the pool this task is operating in. This method is designed |
1083 |
|
* primarily to support extensions, and is unlikely to be useful |
1084 |
< |
* otherwise. This method may be invoked only from within |
1085 |
< |
* ForkJoinTask computations (as may be determined using method |
1086 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1087 |
< |
* result in exceptions or errors, possibly including |
1088 |
< |
* ClassCastException. |
1084 |
> |
* otherwise. |
1085 |
> |
* |
1086 |
> |
* <p>This method may be invoked only from within {@code |
1087 |
> |
* ForkJoinTask} computations (as may be determined using method |
1088 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1089 |
> |
* result in exceptions or errors, possibly including {@code |
1090 |
> |
* ClassCastException}. |
1091 |
|
* |
1092 |
|
* @return a task, or {@code null} if none are available |
1093 |
|
*/ |
1152 |
|
} |
1153 |
|
|
1154 |
|
/** |
1155 |
< |
* Returns a new ForkJoinTask that performs the {@code run} |
1156 |
< |
* method of the given Runnable as its action, and returns a null |
1157 |
< |
* result upon {@code join}. |
1155 |
> |
* Returns a new {@code ForkJoinTask} that performs the {@code run} |
1156 |
> |
* method of the given {@code Runnable} as its action, and returns |
1157 |
> |
* a null result upon {@link #join}. |
1158 |
|
* |
1159 |
|
* @param runnable the runnable action |
1160 |
|
* @return the task |
1164 |
|
} |
1165 |
|
|
1166 |
|
/** |
1167 |
< |
* Returns a new ForkJoinTask that performs the {@code run} |
1168 |
< |
* method of the given Runnable as its action, and returns the |
1169 |
< |
* given result upon {@code join}. |
1167 |
> |
* Returns a new {@code ForkJoinTask} that performs the {@code run} |
1168 |
> |
* method of the given {@code Runnable} as its action, and returns |
1169 |
> |
* the given result upon {@link #join}. |
1170 |
|
* |
1171 |
|
* @param runnable the runnable action |
1172 |
|
* @param result the result upon completion |
1177 |
|
} |
1178 |
|
|
1179 |
|
/** |
1180 |
< |
* Returns a new ForkJoinTask that performs the {@code call} |
1181 |
< |
* method of the given Callable as its action, and returns its |
1182 |
< |
* result upon {@code join}, translating any checked |
1183 |
< |
* exceptions encountered into {@code RuntimeException}. |
1180 |
> |
* Returns a new {@code ForkJoinTask} that performs the {@code call} |
1181 |
> |
* method of the given {@code Callable} as its action, and returns |
1182 |
> |
* its result upon {@link #join}, translating any checked exceptions |
1183 |
> |
* encountered into {@code RuntimeException}. |
1184 |
|
* |
1185 |
|
* @param callable the callable action |
1186 |
|
* @return the task |