ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinTask.java
(Generate patch)

Comparing jsr166/src/jsr166y/ForkJoinTask.java (file contents):
Revision 1.28 by jsr166, Sun Aug 2 17:02:06 2009 UTC vs.
Revision 1.33 by dl, Tue Aug 4 00:36:45 2009 UTC

# Line 12 | Line 12 | import java.io.Serializable;
12   import java.util.Collection;
13   import java.util.Collections;
14   import java.util.List;
15 + import java.util.RandomAccess;
16   import java.util.Map;
17   import java.util.WeakHashMap;
18  
# Line 54 | Line 55 | import java.util.WeakHashMap;
55   * restriction is in part enforced by not permitting checked
56   * exceptions such as {@code IOExceptions} to be thrown. However,
57   * computations may still encounter unchecked exceptions, that are
58 < * rethrown to callers attempting join them. These exceptions may
58 > * rethrown to callers attempting to join them. These exceptions may
59   * additionally include RejectedExecutionExceptions stemming from
60   * internal resource exhaustion such as failure to allocate internal
61   * task queues.
# Line 85 | Line 86 | import java.util.WeakHashMap;
86   * established in a constructor, and then defines a {@code compute}
87   * method that somehow uses the control methods supplied by this base
88   * class. While these methods have {@code public} access (to allow
89 < * instances of different task subclasses to call each others
89 > * instances of different task subclasses to call each other's
90   * methods), some of them may only be called from within other
91   * ForkJoinTasks (as may be determined using method {@link
92   * #inForkJoinPool}).  Attempts to invoke them in other contexts
93   * result in exceptions or errors, possibly including
94   * ClassCastException.
95   *
96 < * <p>Most base support methods are {@code final} because their
97 < * implementations are intrinsically tied to the underlying
98 < * lightweight task scheduling framework, and so cannot be overridden.
99 < * Developers creating new basic styles of fork/join processing should
100 < * minimally implement {@code protected} methods
101 < * {@link #exec}, {@link #setRawResult}, and
102 < * {@link #getRawResult}, while also introducing an abstract
103 < * computational method that can be implemented in its subclasses,
104 < * possibly relying on other {@code protected} methods provided
104 < * by this class.
96 > * <p>Most base support methods are {@code final}, to prevent
97 > * overriding of implementations that are intrinsically tied to the
98 > * underlying lightweight task scheduling framework.  Developers
99 > * creating new basic styles of fork/join processing should minimally
100 > * implement {@code protected} methods {@link #exec}, {@link
101 > * #setRawResult}, and {@link #getRawResult}, while also introducing
102 > * an abstract computational method that can be implemented in its
103 > * subclasses, possibly relying on other {@code protected} methods
104 > * provided by this class.
105   *
106   * <p>ForkJoinTasks should perform relatively small amounts of
107 < * computations, otherwise splitting into smaller tasks. As a very
108 < * rough rule of thumb, a task should perform more than 100 and less
109 < * than 10000 basic computational steps. If tasks are too big, then
110 < * parallelism cannot improve throughput. If too small, then memory
111 < * and internal task maintenance overhead may overwhelm processing.
107 > * computation. Large tasks should be split into smaller subtasks,
108 > * usually via recursive decomposition. As a very rough rule of thumb,
109 > * a task should perform more than 100 and less than 10000 basic
110 > * computational steps. If tasks are too big, then parallelism cannot
111 > * improve throughput. If too small, then memory and internal task
112 > * maintenance overhead may overwhelm processing.
113   *
114   * <p>This class provides {@code adapt} methods for {@link
115   * java.lang.Runnable} and {@link java.util.concurrent.Callable}, that
# Line 116 | Line 117 | import java.util.WeakHashMap;
117   * kinds of tasks. When all tasks are of this form, consider using a
118   * pool in {@link ForkJoinPool#setAsyncMode}.
119   *
120 < * <p>ForkJoinTasks are {@code Serializable}, which enables them
121 < * to be used in extensions such as remote execution frameworks. It is
122 < * in general sensible to serialize tasks only before or after, but
123 < * not during execution. Serialization is not relied on during
123 < * execution itself.
120 > * <p>ForkJoinTasks are {@code Serializable}, which enables them to be
121 > * used in extensions such as remote execution frameworks. It is
122 > * sensible to serialize tasks only before or after, but not during,
123 > * execution. Serialization is not relied on during execution itself.
124   *
125   * @since 1.7
126   * @author Doug Lea
# Line 496 | Line 496 | public abstract class ForkJoinTask<V> im
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())
# Line 512 | Line 514 | public abstract class ForkJoinTask<V> im
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       */
# Line 541 | Line 543 | public abstract class ForkJoinTask<V> im
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();
# Line 563 | Line 567 | public abstract class ForkJoinTask<V> im
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
567 <     * ForkJoinTask computations (as may be determined using method
568 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
569 <     * result in exceptions or errors, possibly including
570 <     * ClassCastException.
570 >     * to be, cancelled.
571       *
572 <     * Overloadings of this method exist for the special cases
573 <     * of one to four arguments.
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       * @param tasks the tasks
579       * @throws NullPointerException if tasks or any element are null
# Line 610 | Line 613 | public abstract class ForkJoinTask<V> im
613      }
614  
615      /**
616 <     * Forks all tasks in the collection, returning when {@code
617 <     * isDone} holds for each task or an exception is encountered. If
618 <     * any task encounters an exception, others may be, but are not
619 <     * guaranteed to be, cancelled.  This method may be invoked only
620 <     * from within ForkJoinTask computations (as may be determined
621 <     * using method {@link #inForkJoinPool}). Attempts to invoke in
622 <     * other contexts result in exceptions or errors, possibly
623 <     * including ClassCastException.
616 >     * Forks all tasks in the specified collection, returning when
617 >     * {@code isDone} holds for each task or an exception is
618 >     * encountered.  If any task encounters an exception, others may
619 >     * be, but are not guaranteed to be, cancelled. The behavior of
620 >     * this operation is undefined if the specified collection is
621 >     * modified while the operation is in progress.
622 >     *
623 >     * <p>This method may be invoked only from within {@code
624 >     * ForkJoinTask} computations (as may be determined using method
625 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
626 >     * result in exceptions or errors, possibly including {@code
627 >     * ClassCastException}.
628       *
629       * @param tasks the collection of tasks
630       * @return the tasks argument, to simplify usage
# Line 625 | Line 632 | public abstract class ForkJoinTask<V> im
632       * @throws RuntimeException or Error if any task did so
633       */
634      public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) {
635 <        if (!(tasks instanceof List<?>)) {
635 >        if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) {
636              invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()]));
637              return tasks;
638          }
# Line 685 | Line 692 | public abstract class ForkJoinTask<V> im
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},
695 <     * whether {@code join} will return normally or via an exception,
696 <     * or whether these behaviors will remain consistent upon repeated
697 <     * invocation.
695 >     * Attempts to cancel execution of this task. This attempt will
696 >     * fail if the task has already completed, has already been
697 >     * cancelled, or could not be cancelled for some other reason. If
698 >     * successful, and this task has not started when cancel is
699 >     * called, execution of this task is suppressed, {@link
700 >     * #isCancelled} will report true, and {@link #join} will result
701 >     * in a {@code CancellationException} being thrown.
702       *
703       * <p>This method may be overridden in subclasses, but if so, must
704       * still ensure that these minimal properties hold. In particular,
705 <     * the cancel method itself must not throw exceptions.
705 >     * the {@code cancel} method itself must not throw exceptions.
706       *
707       * <p>This method is designed to be invoked by <em>other</em>
708       * tasks. To terminate the current task, you can just return or
# Line 706 | Line 710 | public abstract class ForkJoinTask<V> im
710       * invoke {@link #completeExceptionally}.
711       *
712       * @param mayInterruptIfRunning this value is ignored in the
713 <     * default implementation because tasks are not in general
713 >     * default implementation because tasks are not
714       * cancelled via interruption
715       *
716       * @return {@code true} if this task is now cancelled
# Line 727 | Line 731 | public abstract class ForkJoinTask<V> im
731  
732      /**
733       * Returns the exception thrown by the base computation, or a
734 <     * CancellationException if cancelled, or null if none or if the
735 <     * method has not yet completed.
734 >     * {@code CancellationException} if cancelled, or {@code null} if
735 >     * none or if the method has not yet completed.
736       *
737       * @return the exception, or {@code null} if none
738       */
# Line 806 | Line 810 | public abstract class ForkJoinTask<V> im
810       * there are no potential dependencies between continuation of the
811       * current task and that of any other task that might be executed
812       * while helping. (This usually holds for pure divide-and-conquer
813 <     * tasks). This method may be invoked only from within
814 <     * ForkJoinTask computations (as may be determined using method
815 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
816 <     * result in exceptions or errors, possibly including
817 <     * ClassCastException.
813 >     * tasks).
814 >     *
815 >     * <p>This method may be invoked only from within {@code
816 >     * ForkJoinTask} computations (as may be determined using method
817 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
818 >     * result in exceptions or errors, possibly including {@code
819 >     * ClassCastException}.
820       *
821       * @return the computed result
822       */
# Line 823 | Line 829 | public abstract class ForkJoinTask<V> im
829  
830      /**
831       * Possibly executes other tasks until this task is ready.  This
832 <     * method may be invoked only from within ForkJoinTask
833 <     * computations (as may be determined using method {@link
834 <     * #inForkJoinPool}). Attempts to invoke in other contexts result
835 <     * in exceptions or errors, possibly including ClassCastException.
832 >     * method may be useful when processing collections of tasks when
833 >     * some have been cancelled or otherwise known to have aborted.
834 >     *
835 >     * <p>This method may be invoked only from within {@code
836 >     * ForkJoinTask} computations (as may be determined using method
837 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
838 >     * result in exceptions or errors, possibly including {@code
839 >     * ClassCastException}.
840       */
841      public final void quietlyHelpJoin() {
842          if (status >= 0) {
# Line 865 | Line 875 | public abstract class ForkJoinTask<V> im
875  
876      /**
877       * Possibly executes tasks until the pool hosting the current task
878 <     * {@link ForkJoinPool#isQuiescent}. This method may be of use in
879 <     * designs in which many tasks are forked, but none are explicitly
880 <     * joined, instead executing them until all are processed.  This
881 <     * method may be invoked only from within ForkJoinTask
882 <     * computations (as may be determined using method {@link
883 <     * #inForkJoinPool}). Attempts to invoke in other contexts result
884 <     * in exceptions or errors, possibly including ClassCastException.
878 >     * {@link ForkJoinPool#isQuiescent is quiescent}. This method may
879 >     * be of use in designs in which many tasks are forked, but none
880 >     * are explicitly joined, instead executing them until all are
881 >     * processed.
882 >     *
883 >     * <p>This method may be invoked only from within {@code
884 >     * ForkJoinTask} computations (as may be determined using method
885 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
886 >     * result in exceptions or errors, possibly including {@code
887 >     * ClassCastException}.
888       */
889      public static void helpQuiesce() {
890          ((ForkJoinWorkerThread) Thread.currentThread())
# Line 884 | Line 897 | public abstract class ForkJoinTask<V> im
897       * this task, but only if reuse occurs when this task has either
898       * never been forked, or has been forked, then completed and all
899       * outstanding joins of this task have also completed. Effects
900 <     * under any other usage conditions are not guaranteed, and are
901 <     * discouraged. This method may be useful when executing
900 >     * under any other usage conditions are not guaranteed.
901 >     * This method may be useful when executing
902       * pre-constructed trees of subtasks in loops.
903       */
904      public void reinitialize() {
# Line 924 | Line 937 | public abstract class ForkJoinTask<V> im
937       * by the current thread, and has not commenced executing in
938       * another thread.  This method may be useful when arranging
939       * alternative local processing of tasks that could have been, but
940 <     * were not, stolen. This method may be invoked only from within
941 <     * ForkJoinTask computations (as may be determined using method
942 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
943 <     * result in exceptions or errors, possibly including
944 <     * ClassCastException.
940 >     * were not, stolen.
941 >     *
942 >     * <p>This method may be invoked only from within {@code
943 >     * ForkJoinTask} computations (as may be determined using method
944 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
945 >     * result in exceptions or errors, possibly including {@code
946 >     * ClassCastException}.
947       *
948       * @return {@code true} if unforked
949       */
# Line 941 | Line 956 | public abstract class ForkJoinTask<V> im
956       * Returns an estimate of the number of tasks that have been
957       * forked by the current worker thread but not yet executed. This
958       * value may be useful for heuristic decisions about whether to
959 <     * fork other tasks.  This method may be invoked only from within
960 <     * ForkJoinTask computations (as may be determined using method
961 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
962 <     * result in exceptions or errors, possibly including
963 <     * ClassCastException.
959 >     * fork other tasks.
960 >     *
961 >     * <p>This method may be invoked only from within {@code
962 >     * ForkJoinTask} computations (as may be determined using method
963 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
964 >     * result in exceptions or errors, possibly including {@code
965 >     * ClassCastException}.
966 >     *
967       * @return the number of tasks
968       */
969      public static int getQueuedTaskCount() {
# Line 961 | Line 979 | public abstract class ForkJoinTask<V> im
979       * usages of ForkJoinTasks, at steady state, each worker should
980       * aim to maintain a small constant surplus (for example, 3) of
981       * tasks, and to process computations locally if this threshold is
982 <     * exceeded.  This method may be invoked only from within
983 <     * ForkJoinTask computations (as may be determined using method
984 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
985 <     * result in exceptions or errors, possibly including
986 <     * ClassCastException.  *
982 >     * exceeded.
983 >     *
984 >     * <p>This method may be invoked only from within {@code
985 >     * ForkJoinTask} computations (as may be determined using method
986 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
987 >     * result in exceptions or errors, possibly including {@code
988 >     * ClassCastException}.
989 >     *
990       * @return the surplus number of tasks, which may be negative
991       */
992      public static int getSurplusQueuedTaskCount() {
# Line 1017 | Line 1038 | public abstract class ForkJoinTask<V> im
1038       * null even if a task exists but cannot be accessed without
1039       * contention with other threads.  This method is designed
1040       * primarily to support extensions, and is unlikely to be useful
1041 <     * otherwise.  This method may be invoked only from within
1042 <     * ForkJoinTask computations (as may be determined using method
1043 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
1044 <     * result in exceptions or errors, possibly including
1045 <     * ClassCastException.
1041 >     * otherwise.
1042 >     *
1043 >     * <p>This method may be invoked only from within {@code
1044 >     * ForkJoinTask} computations (as may be determined using method
1045 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
1046 >     * result in exceptions or errors, possibly including {@code
1047 >     * ClassCastException}.
1048       *
1049       * @return the next task, or {@code null} if none are available
1050       */
# Line 1034 | Line 1057 | public abstract class ForkJoinTask<V> im
1057       * Unschedules and returns, without executing, the next task
1058       * queued by the current thread but not yet executed.  This method
1059       * is designed primarily to support extensions, and is unlikely to
1060 <     * be useful otherwise.  This method may be invoked only from
1061 <     * within ForkJoinTask computations (as may be determined using
1062 <     * method {@link #inForkJoinPool}). Attempts to invoke in other
1063 <     * contexts result in exceptions or errors, possibly including
1064 <     * ClassCastException.
1060 >     * be useful otherwise.
1061 >     *
1062 >     * <p>This method may be invoked only from within {@code
1063 >     * ForkJoinTask} computations (as may be determined using method
1064 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
1065 >     * result in exceptions or errors, possibly including {@code
1066 >     * ClassCastException}.
1067       *
1068       * @return the next task, or {@code null} if none are available
1069       */
# Line 1055 | Line 1080 | public abstract class ForkJoinTask<V> im
1080       * {@code null} result does not necessarily imply quiescence
1081       * of the pool this task is operating in.  This method is designed
1082       * primarily to support extensions, and is unlikely to be useful
1083 <     * otherwise.  This method may be invoked only from within
1084 <     * ForkJoinTask computations (as may be determined using method
1085 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
1086 <     * result in exceptions or errors, possibly including
1087 <     * ClassCastException.
1083 >     * otherwise.
1084 >     *
1085 >     * <p>This method may be invoked only from within {@code
1086 >     * ForkJoinTask} computations (as may be determined using method
1087 >     * {@link #inForkJoinPool}).  Attempts to invoke in other contexts
1088 >     * result in exceptions or errors, possibly including {@code
1089 >     * ClassCastException}.
1090       *
1091       * @return a task, or {@code null} if none are available
1092       */
# Line 1124 | Line 1151 | public abstract class ForkJoinTask<V> im
1151      }
1152  
1153      /**
1154 <     * Returns a new ForkJoinTask that performs the {@code run}
1155 <     * method of the given Runnable as its action, and returns a null
1156 <     * result upon {@code join}.
1154 >     * Returns a new {@code ForkJoinTask} that performs the {@code run}
1155 >     * method of the given {@code Runnable} as its action, and returns
1156 >     * a null result upon {@link #join}.
1157       *
1158       * @param runnable the runnable action
1159       * @return the task
# Line 1136 | Line 1163 | public abstract class ForkJoinTask<V> im
1163      }
1164  
1165      /**
1166 <     * Returns a new ForkJoinTask that performs the {@code run}
1167 <     * method of the given Runnable as its action, and returns the
1168 <     * given result upon {@code join}.
1166 >     * Returns a new {@code ForkJoinTask} that performs the {@code run}
1167 >     * method of the given {@code Runnable} as its action, and returns
1168 >     * the given result upon {@link #join}.
1169       *
1170       * @param runnable the runnable action
1171       * @param result the result upon completion
# Line 1149 | Line 1176 | public abstract class ForkJoinTask<V> im
1176      }
1177  
1178      /**
1179 <     * Returns a new ForkJoinTask that performs the {@code call}
1180 <     * method of the given Callable as its action, and returns its
1181 <     * result upon {@code join}, translating any checked
1182 <     * exceptions encountered into {@code RuntimeException}.
1179 >     * Returns a new {@code ForkJoinTask} that performs the {@code call}
1180 >     * method of the given {@code Callable} as its action, and returns
1181 >     * its result upon {@link #join}, translating any checked exceptions
1182 >     * encountered into {@code RuntimeException}.
1183       *
1184       * @param callable the callable action
1185       * @return the task

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines