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.10 by jsr166, Tue Jul 21 00:15:13 2009 UTC vs.
Revision 1.18 by dl, Sat Jul 25 15:50:57 2009 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 < import java.io.Serializable;
9 < import java.util.*;
8 >
9   import java.util.concurrent.*;
10 < import java.util.concurrent.atomic.*;
11 < import sun.misc.Unsafe;
12 < import java.lang.reflect.*;
10 >
11 > import java.io.Serializable;
12 > import java.util.Collection;
13 > import java.util.Collections;
14 > import java.util.List;
15 > import java.util.Map;
16 > import java.util.WeakHashMap;
17  
18   /**
19   * Abstract base class for tasks that run within a {@link
# Line 81 | Line 84 | import java.lang.reflect.*;
84   * class. While these methods have {@code public} access (to allow
85   * instances of different task subclasses to call each others
86   * methods), some of them may only be called from within other
87 < * ForkJoinTasks. Attempts to invoke them in other contexts result in
88 < * exceptions or errors possibly including ClassCastException.
87 > * ForkJoinTasks (as may be determined using method {@link
88 > * #inForkJoinPool}).  Attempts to invoke them in other contexts
89 > * result in exceptions or errors, possibly including
90 > * ClassCastException.
91   *
92   * <p>Most base support methods are {@code final} because their
93   * implementations are intrinsically tied to the underlying
# Line 107 | Line 112 | import java.lang.reflect.*;
112   * in general sensible to serialize tasks only before or after, but
113   * not during execution. Serialization is not relied on during
114   * execution itself.
115 + *
116 + * @since 1.7
117 + * @author Doug Lea
118   */
119   public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
120  
# Line 157 | Line 165 | public abstract class ForkJoinTask<V> im
165       */
166      static ForkJoinWorkerThread getWorker() {
167          Thread t = Thread.currentThread();
168 <        return ((t instanceof ForkJoinWorkerThread)?
169 <                (ForkJoinWorkerThread)t : null);
168 >        return ((t instanceof ForkJoinWorkerThread) ?
169 >                (ForkJoinWorkerThread) t : null);
170      }
171  
172      final boolean casStatus(int cmp, int val) {
173 <        return _unsafe.compareAndSwapInt(this, statusOffset, cmp, val);
173 >        return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val);
174      }
175  
176      /**
# Line 170 | Line 178 | public abstract class ForkJoinTask<V> im
178       */
179      static void rethrowException(Throwable ex) {
180          if (ex != null)
181 <            _unsafe.throwException(ex);
181 >            UNSAFE.throwException(ex);
182      }
183  
184      // Setting completion status
# Line 184 | Line 192 | public abstract class ForkJoinTask<V> im
192          ForkJoinPool pool = getPool();
193          if (pool != null) {
194              int s; // Clear signal bits while setting completion status
195 <            do;while ((s = status) >= 0 && !casStatus(s, completion));
195 >            do {} while ((s = status) >= 0 && !casStatus(s, completion));
196  
197              if ((s & SIGNAL_MASK) != 0) {
198                  if ((s &= INTERNAL_SIGNAL_MASK) != 0)
199                      pool.updateRunningCount(s);
200 <                synchronized(this) { notifyAll(); }
200 >                synchronized (this) { notifyAll(); }
201              }
202          }
203          else
# Line 202 | Line 210 | public abstract class ForkJoinTask<V> im
210       */
211      private void externallySetCompletion(int completion) {
212          int s;
213 <        do;while ((s = status) >= 0 &&
214 <                  !casStatus(s, (s & SIGNAL_MASK) | completion));
215 <        synchronized(this) { notifyAll(); }
213 >        do {} while ((s = status) >= 0 &&
214 >                     !casStatus(s, (s & SIGNAL_MASK) | completion));
215 >        synchronized (this) { notifyAll(); }
216      }
217  
218      /**
219 <     * Sets status to indicate normal completion
219 >     * Sets status to indicate normal completion.
220       */
221      final void setNormalCompletion() {
222          // Try typical fast case -- single CAS, no signal, not already done.
223          // Manually expand casStatus to improve chances of inlining it
224 <        if (!_unsafe.compareAndSwapInt(this, statusOffset, 0, NORMAL))
224 >        if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL))
225              setCompletion(NORMAL);
226      }
227  
228      // internal waiting and notification
229  
230      /**
231 <     * Performs the actual monitor wait for awaitDone
231 >     * Performs the actual monitor wait for awaitDone.
232       */
233      private void doAwaitDone() {
234          // Minimize lock bias and in/de-flation effects by maximizing
235          // chances of waiting inside sync
236          try {
237              while (status >= 0)
238 <                synchronized(this) { if (status >= 0) wait(); }
238 >                synchronized (this) { if (status >= 0) wait(); }
239          } catch (InterruptedException ie) {
240              onInterruptedWait();
241          }
242      }
243  
244      /**
245 <     * Performs the actual monitor wait for awaitDone
245 >     * Performs the actual timed monitor wait for awaitDone.
246       */
247      private void doAwaitDone(long startTime, long nanos) {
248 <        synchronized(this) {
248 >        synchronized (this) {
249              try {
250                  while (status >= 0) {
251                      long nt = nanos - System.nanoTime() - startTime;
252                      if (nt <= 0)
253                          break;
254 <                    wait(nt / 1000000, (int)(nt % 1000000));
254 >                    wait(nt / 1000000, (int) (nt % 1000000));
255                  }
256              } catch (InterruptedException ie) {
257                  onInterruptedWait();
# Line 259 | Line 267 | public abstract class ForkJoinTask<V> im
267       *
268       * @return status upon exit
269       */
270 <    private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) {
271 <        ForkJoinPool pool = w == null? null : w.pool;
270 >    private int awaitDone(ForkJoinWorkerThread w,
271 >                          boolean maintainParallelism) {
272 >        ForkJoinPool pool = (w == null) ? null : w.pool;
273          int s;
274          while ((s = status) >= 0) {
275 <            if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) {
275 >            if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) {
276                  if (pool == null || !pool.preJoin(this, maintainParallelism))
277                      doAwaitDone();
278                  if (((s = status) & INTERNAL_SIGNAL_MASK) != 0)
# Line 276 | Line 285 | public abstract class ForkJoinTask<V> im
285  
286      /**
287       * Timed version of awaitDone
288 +     *
289       * @return status upon exit
290       */
291      private int awaitDone(ForkJoinWorkerThread w, long nanos) {
292 <        ForkJoinPool pool = w == null? null : w.pool;
292 >        ForkJoinPool pool = (w == null) ? null : w.pool;
293          int s;
294          while ((s = status) >= 0) {
295 <            if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) {
295 >            if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) {
296                  long startTime = System.nanoTime();
297                  if (pool == null || !pool.preJoin(this, false))
298                      doAwaitDone(startTime, nanos);
# Line 304 | Line 314 | public abstract class ForkJoinTask<V> im
314       */
315      private void adjustPoolCountsOnUnblock(ForkJoinPool pool) {
316          int s;
317 <        do;while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK));
317 >        do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK));
318          if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0)
319              pool.updateRunningCount(s);
320      }
# Line 359 | Line 369 | public abstract class ForkJoinTask<V> im
369  
370      /**
371       * Returns result or throws exception using j.u.c.Future conventions.
372 <     * Only call when isDone known to be true.
372 >     * Only call when {@code isDone} known to be true.
373       */
374      private V reportFutureResult()
375          throws ExecutionException, InterruptedException {
# Line 425 | Line 435 | public abstract class ForkJoinTask<V> im
435              try {
436                  if (!exec())
437                      return;
438 <            } catch(Throwable rex) {
438 >            } catch (Throwable rex) {
439                  setDoneExceptionally(rex);
440                  return;
441              }
# Line 457 | Line 467 | public abstract class ForkJoinTask<V> im
467      final void cancelIgnoringExceptions() {
468          try {
469              cancel(false);
470 <        } catch(Throwable ignore) {
470 >        } catch (Throwable ignore) {
471          }
472      }
473  
# Line 469 | Line 479 | public abstract class ForkJoinTask<V> im
479          ForkJoinTask<?> t;
480          while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null)
481              t.quietlyExec();
482 <        return (s >= 0)? awaitDone(w, false) : s; // block if no work
482 >        return (s >= 0) ? awaitDone(w, false) : s; // block if no work
483      }
484  
485      // public methods
# Line 479 | Line 489 | public abstract class ForkJoinTask<V> im
489       * necessarily enforced, it is a usage error to fork a task more
490       * than once unless it has completed and been reinitialized.  This
491       * method may be invoked only from within ForkJoinTask
492 <     * computations. Attempts to invoke in other contexts result in
493 <     * exceptions or errors possibly including ClassCastException.
494 <     */
495 <    public final void fork() {
496 <        ((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this);
492 >     * computations (as may be determined using method {@link
493 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
494 >     * in exceptions or errors, possibly including ClassCastException.
495 >     *
496 >     * @return <code>this</code>, to simplify usage.
497 >     */
498 >    public final ForkJoinTask<V> fork() {
499 >        ((ForkJoinWorkerThread) Thread.currentThread())
500 >            .pushTask(this);
501 >        return this;
502      }
503  
504      /**
# Line 519 | Line 534 | public abstract class ForkJoinTask<V> im
534      /**
535       * Forks both tasks, returning when {@code isDone} holds for
536       * both of them or an exception is encountered. This method may be
537 <     * invoked only from within ForkJoinTask computations. Attempts to
538 <     * invoke in other contexts result in exceptions or errors
537 >     * invoked only from within ForkJoinTask computations (as may be
538 >     * determined using method {@link #inForkJoinPool}). Attempts to
539 >     * invoke in other contexts result in exceptions or errors,
540       * possibly including ClassCastException.
541       *
542       * @param t1 one task
# Line 538 | Line 554 | public abstract class ForkJoinTask<V> im
554       * Forks the given tasks, returning when {@code isDone} holds
555       * for all of them. If any task encounters an exception, others
556       * may be cancelled.  This method may be invoked only from within
557 <     * ForkJoinTask computations. Attempts to invoke in other contexts
558 <     * result in exceptions or errors possibly including ClassCastException.
557 >     * ForkJoinTask computations (as may be determined using method
558 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
559 >     * result in exceptions or errors, possibly including
560 >     * ClassCastException.
561       *
562       * @param tasks the array of tasks
563       * @throws NullPointerException if tasks or any element are null
# Line 582 | Line 600 | public abstract class ForkJoinTask<V> im
600       * Forks all tasks in the collection, returning when
601       * {@code isDone} holds for all of them. If any task
602       * encounters an exception, others may be cancelled.  This method
603 <     * may be invoked only from within ForkJoinTask
604 <     * computations. Attempts to invoke in other contexts result in
605 <     * exceptions or errors possibly including ClassCastException.
603 >     * may be invoked only from within ForkJoinTask computations (as
604 >     * may be determined using method {@link
605 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
606 >     * in exceptions or errors, possibly including ClassCastException.
607       *
608       * @param tasks the collection of tasks
609       * @throws NullPointerException if tasks or any element are null
610       * @throws RuntimeException or Error if any task did so
611       */
612      public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) {
613 <        if (!(tasks instanceof List)) {
614 <            invokeAll(tasks.toArray(new ForkJoinTask[tasks.size()]));
613 >        if (!(tasks instanceof List<?>)) {
614 >            invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()]));
615              return;
616          }
617 +        @SuppressWarnings("unchecked")
618          List<? extends ForkJoinTask<?>> ts =
619 <            (List<? extends ForkJoinTask<?>>)tasks;
619 >            (List<? extends ForkJoinTask<?>>) tasks;
620          Throwable ex = null;
621          int last = ts.size() - 1;
622          for (int i = last; i >= 0; --i) {
# Line 671 | Line 691 | public abstract class ForkJoinTask<V> im
691       *
692       * @param mayInterruptIfRunning this value is ignored in the
693       * default implementation because tasks are not in general
694 <     * cancelled via interruption.
694 >     * cancelled via interruption
695       *
696       * @return true if this task is now cancelled
697       */
# Line 721 | Line 741 | public abstract class ForkJoinTask<V> im
741       */
742      public void completeExceptionally(Throwable ex) {
743          setDoneExceptionally((ex instanceof RuntimeException) ||
744 <                             (ex instanceof Error)? ex :
744 >                             (ex instanceof Error) ? ex :
745                               new RuntimeException(ex));
746      }
747  
# Line 740 | Line 760 | public abstract class ForkJoinTask<V> im
760      public void complete(V value) {
761          try {
762              setRawResult(value);
763 <        } catch(Throwable rex) {
763 >        } catch (Throwable rex) {
764              setDoneExceptionally(rex);
765              return;
766          }
# Line 770 | Line 790 | public abstract class ForkJoinTask<V> im
790       * current task and that of any other task that might be executed
791       * while helping. (This usually holds for pure divide-and-conquer
792       * tasks). This method may be invoked only from within
793 <     * ForkJoinTask computations. Attempts to invoke in other contexts
794 <     * result in exceptions or errors possibly including ClassCastException.
793 >     * ForkJoinTask computations (as may be determined using method
794 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
795 >     * result in exceptions or errors, possibly including
796 >     * ClassCastException.
797       *
798       * @return the computed result
799       */
800      public final V helpJoin() {
801 <        ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread());
801 >        ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread();
802          if (status < 0 || !w.unpushTask(this) || !tryExec())
803              reportException(busyJoin(w));
804          return getRawResult();
# Line 785 | Line 807 | public abstract class ForkJoinTask<V> im
807      /**
808       * Possibly executes other tasks until this task is ready.  This
809       * method may be invoked only from within ForkJoinTask
810 <     * computations. Attempts to invoke in other contexts result in
811 <     * exceptions or errors possibly including ClassCastException.
810 >     * computations (as may be determined using method {@link
811 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
812 >     * in exceptions or errors, possibly including ClassCastException.
813       */
814      public final void quietlyHelpJoin() {
815          if (status >= 0) {
816              ForkJoinWorkerThread w =
817 <                (ForkJoinWorkerThread)(Thread.currentThread());
817 >                (ForkJoinWorkerThread) Thread.currentThread();
818              if (!w.unpushTask(this) || !tryQuietlyInvoke())
819                  busyJoin(w);
820          }
# Line 830 | Line 853 | public abstract class ForkJoinTask<V> im
853       * joined, instead executing them until all are processed.
854       */
855      public static void helpQuiesce() {
856 <        ((ForkJoinWorkerThread)(Thread.currentThread())).
857 <            helpQuiescePool();
856 >        ((ForkJoinWorkerThread) Thread.currentThread())
857 >            .helpQuiescePool();
858      }
859  
860      /**
# Line 852 | Line 875 | public abstract class ForkJoinTask<V> im
875  
876      /**
877       * Returns the pool hosting the current task execution, or null
878 <     * if this task is executing outside of any pool.
878 >     * if this task is executing outside of any ForkJoinPool.
879       *
880       * @return the pool, or null if none
881       */
882      public static ForkJoinPool getPool() {
883          Thread t = Thread.currentThread();
884 <        return ((t instanceof ForkJoinWorkerThread)?
885 <                ((ForkJoinWorkerThread)t).pool : null);
884 >        return (t instanceof ForkJoinWorkerThread) ?
885 >            ((ForkJoinWorkerThread) t).pool : null;
886 >    }
887 >
888 >    /**
889 >     * Returns {@code true} if the current thread is executing as a
890 >     * ForkJoinPool computation.
891 >     *
892 >     * @return {@code true} if the current thread is executing as a
893 >     * ForkJoinPool computation, or false otherwise
894 >     */
895 >    public static boolean inForkJoinPool() {
896 >        return Thread.currentThread() instanceof ForkJoinWorkerThread;
897      }
898  
899      /**
# Line 869 | Line 903 | public abstract class ForkJoinTask<V> im
903       * another thread.  This method may be useful when arranging
904       * alternative local processing of tasks that could have been, but
905       * were not, stolen. This method may be invoked only from within
906 <     * ForkJoinTask computations. Attempts to invoke in other contexts
907 <     * result in exceptions or errors possibly including ClassCastException.
906 >     * ForkJoinTask computations (as may be determined using method
907 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
908 >     * result in exceptions or errors, possibly including
909 >     * ClassCastException.
910       *
911       * @return true if unforked
912       */
913      public boolean tryUnfork() {
914 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).unpushTask(this);
914 >        return ((ForkJoinWorkerThread) Thread.currentThread())
915 >            .unpushTask(this);
916      }
917  
918      /**
# Line 887 | Line 924 | public abstract class ForkJoinTask<V> im
924       * @return the number of tasks
925       */
926      public static int getQueuedTaskCount() {
927 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
928 <            getQueueSize();
927 >        return ((ForkJoinWorkerThread) Thread.currentThread())
928 >            .getQueueSize();
929      }
930  
931      /**
# Line 904 | Line 941 | public abstract class ForkJoinTask<V> im
941       * @return the surplus number of tasks, which may be negative
942       */
943      public static int getSurplusQueuedTaskCount() {
944 <        return ((ForkJoinWorkerThread)(Thread.currentThread()))
944 >        return ((ForkJoinWorkerThread) Thread.currentThread())
945              .getEstimatedSurplusTaskCount();
946      }
947  
# Line 951 | Line 988 | public abstract class ForkJoinTask<V> im
988       * be polled or executed next.  This method is designed primarily
989       * to support extensions, and is unlikely to be useful otherwise.
990       * This method may be invoked only from within ForkJoinTask
991 <     * computations. Attempts to invoke in other contexts result in
992 <     * exceptions or errors possibly including ClassCastException.
991 >     * computations (as may be determined using method {@link
992 >     * #inForkJoinPool}). Attempts to invoke in other contexts result
993 >     * in exceptions or errors, possibly including ClassCastException.
994       *
995       * @return the next task, or null if none are available
996       */
997      protected static ForkJoinTask<?> peekNextLocalTask() {
998 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).peekTask();
998 >        return ((ForkJoinWorkerThread) Thread.currentThread())
999 >            .peekTask();
1000      }
1001  
1002      /**
# Line 965 | Line 1004 | public abstract class ForkJoinTask<V> im
1004       * queued by the current thread but not yet executed.  This method
1005       * is designed primarily to support extensions, and is unlikely to
1006       * be useful otherwise.  This method may be invoked only from
1007 <     * within ForkJoinTask computations. Attempts to invoke in other
1008 <     * contexts result in exceptions or errors possibly including
1007 >     * within ForkJoinTask computations (as may be determined using
1008 >     * method {@link #inForkJoinPool}). Attempts to invoke in other
1009 >     * contexts result in exceptions or errors, possibly including
1010       * ClassCastException.
1011       *
1012       * @return the next task, or null if none are available
1013       */
1014      protected static ForkJoinTask<?> pollNextLocalTask() {
1015 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask();
1015 >        return ((ForkJoinWorkerThread) Thread.currentThread())
1016 >            .pollLocalTask();
1017      }
1018  
1019      /**
# Line 984 | Line 1025 | public abstract class ForkJoinTask<V> im
1025       * of the pool this task is operating in.  This method is designed
1026       * primarily to support extensions, and is unlikely to be useful
1027       * otherwise.  This method may be invoked only from within
1028 <     * ForkJoinTask computations. Attempts to invoke in other contexts
1029 <     * result in exceptions or errors possibly including
1028 >     * ForkJoinTask computations (as may be determined using method
1029 >     * {@link #inForkJoinPool}). Attempts to invoke in other contexts
1030 >     * result in exceptions or errors, possibly including
1031       * ClassCastException.
1032       *
1033       * @return a task, or null if none are available
1034       */
1035      protected static ForkJoinTask<?> pollTask() {
1036 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
1037 <            pollTask();
1036 >        return ((ForkJoinWorkerThread) Thread.currentThread())
1037 >            .pollTask();
1038 >    }
1039 >
1040 >    // adaptors
1041 >
1042 >    /**
1043 >     * Returns a new ForkJoinTask that performs the <code>run</code>
1044 >     * method of the given Runnable as its action, and returns a null
1045 >     * result upon <code>join</code>.
1046 >     *
1047 >     * @param runnable the runnable action
1048 >     * @return the task
1049 >     */
1050 >    public static ForkJoinTask<Void> adapt(Runnable runnable) {
1051 >        return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null);
1052 >    }
1053 >
1054 >    /**
1055 >     * Returns a new ForkJoinTask that performs the <code>run</code>
1056 >     * method of the given Runnable as its action, and returns the
1057 >     * given result upon <code>join</code>.
1058 >     *
1059 >     * @param runnable the runnable action
1060 >     * @param result the result upon completion
1061 >     * @return the task
1062 >     */
1063 >    public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
1064 >        return new ForkJoinPool.AdaptedRunnable<T>(runnable, result);
1065 >    }
1066 >
1067 >    /**
1068 >     * Returns a new ForkJoinTask that performs the <code>call</code>
1069 >     * method of the given Callable as its action, and returns its
1070 >     * result upon <code>join</code>, translating any checked
1071 >     * exceptions encountered into <code>RuntimeException<code>.
1072 >     *
1073 >     * @param callable the callable action
1074 >     * @return the task
1075 >     */
1076 >    public static <T> ForkJoinTask<T> adapt(Callable<T> callable) {
1077 >        return new ForkJoinPool.AdaptedCallable<T>(callable);
1078      }
1079  
1080      // Serialization support
# Line 1024 | Line 1106 | public abstract class ForkJoinTask<V> im
1106          status |= EXTERNAL_SIGNAL; // conservatively set external signal
1107          Object ex = s.readObject();
1108          if (ex != null)
1109 <            setDoneExceptionally((Throwable)ex);
1109 >            setDoneExceptionally((Throwable) ex);
1110      }
1111  
1112 <    // Temporary Unsafe mechanics for preliminary release
1113 <    private static Unsafe getUnsafe() throws Throwable {
1112 >    // Unsafe mechanics for jsr166y 3rd party package.
1113 >    private static sun.misc.Unsafe getUnsafe() {
1114          try {
1115 <            return Unsafe.getUnsafe();
1115 >            return sun.misc.Unsafe.getUnsafe();
1116          } catch (SecurityException se) {
1117              try {
1118                  return java.security.AccessController.doPrivileged
1119 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
1120 <                        public Unsafe run() throws Exception {
1121 <                            return getUnsafePrivileged();
1119 >                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1120 >                        public sun.misc.Unsafe run() throws Exception {
1121 >                            return getUnsafeByReflection();
1122                          }});
1123              } catch (java.security.PrivilegedActionException e) {
1124 <                throw e.getCause();
1124 >                throw new RuntimeException("Could not initialize intrinsics",
1125 >                                           e.getCause());
1126              }
1127          }
1128      }
1129  
1130 <    private static Unsafe getUnsafePrivileged()
1130 >    private static sun.misc.Unsafe getUnsafeByReflection()
1131              throws NoSuchFieldException, IllegalAccessException {
1132 <        Field f = Unsafe.class.getDeclaredField("theUnsafe");
1132 >        java.lang.reflect.Field f =
1133 >            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1134          f.setAccessible(true);
1135 <        return (Unsafe) f.get(null);
1135 >        return (sun.misc.Unsafe) f.get(null);
1136      }
1137  
1138 <    private static long fieldOffset(String fieldName)
1055 <            throws NoSuchFieldException {
1056 <        return _unsafe.objectFieldOffset
1057 <            (ForkJoinTask.class.getDeclaredField(fieldName));
1058 <    }
1059 <
1060 <    static final Unsafe _unsafe;
1061 <    static final long statusOffset;
1062 <
1063 <    static {
1138 >    private static long fieldOffset(String fieldName, Class<?> klazz) {
1139          try {
1140 <            _unsafe = getUnsafe();
1141 <            statusOffset = fieldOffset("status");
1142 <        } catch (Throwable e) {
1143 <            throw new RuntimeException("Could not initialize intrinsics", e);
1140 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
1141 >        } catch (NoSuchFieldException e) {
1142 >            // Convert Exception to Error
1143 >            NoSuchFieldError error = new NoSuchFieldError(fieldName);
1144 >            error.initCause(e);
1145 >            throw error;
1146          }
1147      }
1148  
1149 +    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1150 +    static final long statusOffset =
1151 +        fieldOffset("status", ForkJoinTask.class);
1152 +
1153   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines