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.24 by jsr166, Mon Jul 27 21:41:53 2009 UTC vs.
Revision 1.31 by jsr166, Sun Aug 2 22:58:50 2009 UTC

# Line 16 | Line 16 | import java.util.Map;
16   import java.util.WeakHashMap;
17  
18   /**
19 < * Abstract base class for tasks that run within a {@link
20 < * ForkJoinPool}.  A ForkJoinTask is a thread-like entity that is much
19 > * Abstract base class for tasks that run within a {@link ForkJoinPool}.
20 > * A {@code ForkJoinTask} is a thread-like entity that is much
21   * lighter weight than a normal thread.  Huge numbers of tasks and
22   * subtasks may be hosted by a small number of actual threads in a
23   * ForkJoinPool, at the price of some usage limitations.
24   *
25 < * <p> A "main" ForkJoinTask begins execution when submitted to a
26 < * {@link ForkJoinPool}. Once started, it will usually in turn start
27 < * other subtasks.  As indicated by the name of this class, many
28 < * programs using ForkJoinTasks employ only methods {@code fork} and
29 < * {@code join}, or derivatives such as {@code invokeAll}.  However,
30 < * this class also provides a number of other methods that can come
31 < * into play in advanced usages, as well as extension mechanics that
32 < * allow support of new forms of fork/join processing.
25 > * <p>A "main" {@code ForkJoinTask} begins execution when submitted
26 > * to a {@link ForkJoinPool}.  Once started, it will usually in turn
27 > * start other subtasks.  As indicated by the name of this class,
28 > * many programs using {@code ForkJoinTask} employ only methods
29 > * {@link #fork} and {@link #join}, or derivatives such as {@link
30 > * #invokeAll}.  However, this class also provides a number of other
31 > * methods that can come into play in advanced usages, as well as
32 > * extension mechanics that allow support of new forms of fork/join
33 > * processing.
34   *
35 < * <p>A ForkJoinTask is a lightweight form of {@link Future}.  The
36 < * efficiency of ForkJoinTasks stems from a set of restrictions (that
37 < * are only partially statically enforceable) reflecting their
38 < * intended use as computational tasks calculating pure functions or
39 < * operating on purely isolated objects.  The primary coordination
40 < * mechanisms are {@link #fork}, that arranges asynchronous execution,
41 < * and {@link #join}, that doesn't proceed until the task's result has
42 < * been computed.  Computations should avoid {@code synchronized}
43 < * methods or blocks, and should minimize other blocking
44 < * synchronization apart from joining other tasks or using
45 < * synchronizers such as Phasers that are advertised to cooperate with
46 < * fork/join scheduling. Tasks should also not perform blocking IO,
47 < * and should ideally access variables that are completely independent
48 < * of those accessed by other running tasks. Minor breaches of these
49 < * restrictions, for example using shared output streams, may be
50 < * tolerable in practice, but frequent use may result in poor
51 < * performance, and the potential to indefinitely stall if the number
52 < * of threads not waiting for IO or other external synchronization
53 < * becomes exhausted. This usage restriction is in part enforced by
54 < * not permitting checked exceptions such as {@code IOExceptions}
55 < * to be thrown. However, computations may still encounter unchecked
56 < * exceptions, that are rethrown to callers attempting join
57 < * them. These exceptions may additionally include
58 < * RejectedExecutionExceptions stemming from internal resource
59 < * exhaustion such as failure to allocate internal task queues.
35 > * <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}.
36 > * The efficiency of {@code ForkJoinTask}s stems from a set of
37 > * restrictions (that are only partially statically enforceable)
38 > * reflecting their intended use as computational tasks calculating
39 > * pure functions or operating on purely isolated objects.  The
40 > * primary coordination mechanisms are {@link #fork}, that arranges
41 > * asynchronous execution, and {@link #join}, that doesn't proceed
42 > * until the task's result has been computed.  Computations should
43 > * avoid {@code synchronized} methods or blocks, and should minimize
44 > * other blocking synchronization apart from joining other tasks or
45 > * using synchronizers such as Phasers that are advertised to
46 > * cooperate with fork/join scheduling. Tasks should also not perform
47 > * blocking IO, and should ideally access variables that are
48 > * completely independent of those accessed by other running
49 > * tasks. Minor breaches of these restrictions, for example using
50 > * shared output streams, may be tolerable in practice, but frequent
51 > * use may result in poor performance, and the potential to
52 > * indefinitely stall if the number of threads not waiting for IO or
53 > * other external synchronization becomes exhausted. This usage
54 > * restriction is in part enforced by not permitting checked
55 > * exceptions such as {@code IOExceptions} to be thrown. However,
56 > * computations may still encounter unchecked exceptions, that are
57 > * rethrown to callers attempting join them. These exceptions may
58 > * additionally include RejectedExecutionExceptions stemming from
59 > * internal resource exhaustion such as failure to allocate internal
60 > * task queues.
61   *
62   * <p>The primary method for awaiting completion and extracting
63   * results of a task is {@link #join}, but there are several variants:
# Line 74 | Line 76 | import java.util.WeakHashMap;
76   * performs the most common form of parallel invocation: forking a set
77   * of tasks and joining them all.
78   *
79 < * <p> The ForkJoinTask class is not usually directly subclassed.
79 > * <p>The ForkJoinTask class is not usually directly subclassed.
80   * Instead, you subclass one of the abstract classes that support a
81 < * particular style of fork/join processing.  Normally, a concrete
81 > * particular style of fork/join processing, typically {@link
82 > * RecursiveAction} for computations that do not return results, or
83 > * {@link RecursiveTask} for those that do.  Normally, a concrete
84   * ForkJoinTask subclass declares fields comprising its parameters,
85   * established in a constructor, and then defines a {@code compute}
86   * method that somehow uses the control methods supplied by this base
# Line 106 | Line 110 | import java.util.WeakHashMap;
110   * parallelism cannot improve throughput. If too small, then memory
111   * and internal task maintenance overhead may overwhelm processing.
112   *
113 + * <p>This class provides {@code adapt} methods for {@link
114 + * java.lang.Runnable} and {@link java.util.concurrent.Callable}, that
115 + * may be of use when mixing execution of ForkJoinTasks with other
116 + * kinds of tasks. When all tasks are of this form, consider using a
117 + * pool in {@link ForkJoinPool#setAsyncMode}.
118 + *
119   * <p>ForkJoinTasks are {@code Serializable}, which enables them
120   * to be used in extensions such as remote execution frameworks. It is
121   * in general sensible to serialize tasks only before or after, but
# Line 247 | Line 257 | public abstract class ForkJoinTask<V> im
257          synchronized (this) {
258              try {
259                  while (status >= 0) {
260 <                    long nt = nanos - System.nanoTime() - startTime;
260 >                    long nt = nanos - (System.nanoTime() - startTime);
261                      if (nt <= 0)
262                          break;
263                      wait(nt / 1000000, (int) (nt % 1000000));
# Line 486 | 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 >     * <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.
507 >     * @return {@code this}, to simplify usage
508       */
509      public final ForkJoinTask<V> fork() {
510          ((ForkJoinWorkerThread) Thread.currentThread())
# Line 502 | 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 531 | Line 543 | public abstract class ForkJoinTask<V> im
543      }
544  
545      /**
546 <     * Forks both tasks, returning when {@code isDone} holds for
547 <     * both of them 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.
552 <     *
553 <     * @param t1 one task
554 <     * @param t2 the other task
555 <     * @throws NullPointerException if t1 or t2 are null
556 <     * @throws RuntimeException or Error if either task did so
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();
564      }
565  
566      /**
567 <     * Forks the given tasks, returning when {@code isDone} holds
568 <     * for all of them. If any task encounters an exception, others
569 <     * may be cancelled.  This method may be invoked only from within
570 <     * ForkJoinTask computations (as may be determined using method
571 <     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
572 <     * result in exceptions or errors, possibly including
573 <     * ClassCastException.
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.
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 >     * <p>Overloadings of this method exist for the special cases
579 >     * of one to four arguments.
580       *
581 <     * @param tasks the array of tasks
581 >     * @param tasks the tasks
582       * @throws NullPointerException if tasks or any element are null
583       * @throws RuntimeException or Error if any task did so
584       */
# Line 596 | Line 616 | public abstract class ForkJoinTask<V> im
616      }
617  
618      /**
619 <     * Forks all tasks in the collection, returning when
620 <     * {@code isDone} holds for all of them. If any task
621 <     * encounters an exception, others may be cancelled.  This method
622 <     * may be invoked only from within ForkJoinTask computations (as
623 <     * may be determined using method {@link
624 <     * #inForkJoinPool}). Attempts to invoke in other contexts result
625 <     * in exceptions or errors, possibly including ClassCastException.
619 >     * Forks all tasks in the collection, returning when {@code
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
# Line 683 | Line 706 | public abstract class ForkJoinTask<V> im
706       *
707       * <p>This method may be overridden in subclasses, but if so, must
708       * still ensure that these minimal properties hold. In particular,
709 <     * the cancel method itself must not throw exceptions.
709 >     * the {@code cancel} method itself must not throw exceptions.
710       *
711 <     * <p> This method is designed to be invoked by <em>other</em>
711 >     * <p>This method is designed to be invoked by <em>other</em>
712       * tasks. To terminate the current task, you can just return or
713       * throw an unchecked exception from its computation method, or
714       * invoke {@link #completeExceptionally}.
# Line 712 | Line 735 | public abstract class ForkJoinTask<V> im
735  
736      /**
737       * Returns the exception thrown by the base computation, or a
738 <     * CancellationException if cancelled, or null if none or if the
739 <     * method has not yet completed.
738 >     * {@code CancellationException} if cancelled, or {@code null} if
739 >     * none or if the method has not yet completed.
740       *
741       * @return the exception, or {@code null} if none
742       */
# Line 732 | Line 755 | public abstract class ForkJoinTask<V> im
755       * {@code join} and related operations. This method may be used
756       * to induce exceptions in asynchronous tasks, or to force
757       * completion of tasks that would not otherwise complete.  Its use
758 <     * in other situations is likely to be wrong.  This method is
758 >     * in other situations is discouraged.  This method is
759       * overridable, but overridden versions must invoke {@code super}
760       * implementation to maintain guarantees.
761       *
# Line 752 | Line 775 | public abstract class ForkJoinTask<V> im
775       * operations. This method may be used to provide results for
776       * asynchronous tasks, or to provide alternative handling for
777       * tasks that would not otherwise complete normally. Its use in
778 <     * other situations is likely to be wrong. This method is
778 >     * other situations is discouraged. This method is
779       * overridable, but overridden versions must invoke {@code super}
780       * implementation to maintain guarantees.
781       *
# Line 777 | Line 800 | public abstract class ForkJoinTask<V> im
800  
801      public final V get(long timeout, TimeUnit unit)
802          throws InterruptedException, ExecutionException, TimeoutException {
803 +        long nanos = unit.toNanos(timeout);
804          ForkJoinWorkerThread w = getWorker();
805          if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke())
806 <            awaitDone(w, unit.toNanos(timeout));
806 >            awaitDone(w, nanos);
807          return reportTimedFutureResult();
808      }
809  
# Line 790 | Line 814 | public abstract class ForkJoinTask<V> im
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       */
# Line 806 | Line 832 | public abstract class ForkJoinTask<V> im
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) {
# Line 852 | Line 880 | public abstract class ForkJoinTask<V> im
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.
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())
# Line 864 | Line 898 | public abstract class ForkJoinTask<V> im
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 <     * almost surely wrong. 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() {
# Line 878 | Line 912 | public abstract class ForkJoinTask<V> im
912       * Returns the pool hosting the current task execution, or null
913       * if this task is executing outside of any ForkJoinPool.
914       *
915 +     * @see #inForkJoinPool
916       * @return the pool, or {@code null} if none
917       */
918      public static ForkJoinPool getPool() {
# Line 903 | Line 938 | public abstract class ForkJoinTask<V> im
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       */
# Line 922 | Line 959 | public abstract class ForkJoinTask<V> im
959       * value may be useful for heuristic decisions about whether to
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() {
# Line 939 | Line 982 | public abstract class ForkJoinTask<V> im
982       * tasks, and to process computations locally if this threshold is
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() {
# Line 983 | Line 1032 | public abstract class ForkJoinTask<V> im
1032      protected abstract boolean exec();
1033  
1034      /**
1035 <     * Returns, but does not unschedule or execute, the task queued by
1036 <     * the current thread but not yet executed, if one is
1035 >     * Returns, but does not unschedule or execute, a task queued by
1036 >     * the current thread but not yet executed, if one is immediately
1037       * available. There is no guarantee that this task will actually
1038 <     * be polled or executed next.  This method is designed primarily
1039 <     * to support extensions, and is unlikely to be useful otherwise.
1040 <     * This method may be invoked only from within ForkJoinTask
1041 <     * computations (as may be determined using method {@link
1042 <     * #inForkJoinPool}). Attempts to invoke in other contexts result
1043 <     * in exceptions or errors, possibly including ClassCastException.
1038 >     * be polled or executed next. Conversely, this method may return
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.
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       */
# Line 1004 | Line 1058 | public abstract class ForkJoinTask<V> im
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       */
# Line 1025 | Line 1081 | public abstract class ForkJoinTask<V> im
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       */
# Line 1038 | Line 1096 | public abstract class ForkJoinTask<V> im
1096              .pollTask();
1097      }
1098  
1099 <    // adaptors
1099 >    /**
1100 >     * Adaptor for Runnables. This implements RunnableFuture
1101 >     * to be compliant with AbstractExecutorService constraints
1102 >     * when used in ForkJoinPool.
1103 >     */
1104 >    static final class AdaptedRunnable<T> extends ForkJoinTask<T>
1105 >        implements RunnableFuture<T> {
1106 >        final Runnable runnable;
1107 >        final T resultOnCompletion;
1108 >        T result;
1109 >        AdaptedRunnable(Runnable runnable, T result) {
1110 >            if (runnable == null) throw new NullPointerException();
1111 >            this.runnable = runnable;
1112 >            this.resultOnCompletion = result;
1113 >        }
1114 >        public T getRawResult() { return result; }
1115 >        public void setRawResult(T v) { result = v; }
1116 >        public boolean exec() {
1117 >            runnable.run();
1118 >            result = resultOnCompletion;
1119 >            return true;
1120 >        }
1121 >        public void run() { invoke(); }
1122 >        private static final long serialVersionUID = 5232453952276885070L;
1123 >    }
1124 >
1125 >    /**
1126 >     * Adaptor for Callables
1127 >     */
1128 >    static final class AdaptedCallable<T> extends ForkJoinTask<T>
1129 >        implements RunnableFuture<T> {
1130 >        final Callable<? extends T> callable;
1131 >        T result;
1132 >        AdaptedCallable(Callable<? extends T> callable) {
1133 >            if (callable == null) throw new NullPointerException();
1134 >            this.callable = callable;
1135 >        }
1136 >        public T getRawResult() { return result; }
1137 >        public void setRawResult(T v) { result = v; }
1138 >        public boolean exec() {
1139 >            try {
1140 >                result = callable.call();
1141 >                return true;
1142 >            } catch (Error err) {
1143 >                throw err;
1144 >            } catch (RuntimeException rex) {
1145 >                throw rex;
1146 >            } catch (Exception ex) {
1147 >                throw new RuntimeException(ex);
1148 >            }
1149 >        }
1150 >        public void run() { invoke(); }
1151 >        private static final long serialVersionUID = 2838392045355241008L;
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
1161       */
1162 <    public static ForkJoinTask<Void> adapt(Runnable runnable) {
1163 <        return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null);
1162 >    public static ForkJoinTask<?> adapt(Runnable runnable) {
1163 >        return new AdaptedRunnable<Void>(runnable, null);
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
1173       * @return the task
1174       */
1175      public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
1176 <        return new ForkJoinPool.AdaptedRunnable<T>(runnable, result);
1176 >        return new AdaptedRunnable<T>(runnable, result);
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
1187       */
1188 <    public static <T> ForkJoinTask<T> adapt(Callable<T> callable) {
1189 <        return new ForkJoinPool.AdaptedCallable<T>(callable);
1188 >    public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) {
1189 >        return new AdaptedCallable<T>(callable);
1190      }
1191  
1192      // Serialization support

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines