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

Comparing jsr166/src/jsr166e/ForkJoinTask.java (file contents):
Revision 1.2 by dl, Sun Oct 28 22:35:45 2012 UTC vs.
Revision 1.19 by jsr166, Thu Jun 30 14:17:04 2016 UTC

# Line 33 | Line 33 | import java.lang.reflect.Constructor;
33   * <p>A "main" {@code ForkJoinTask} begins execution when it is
34   * explicitly submitted to a {@link ForkJoinPool}, or, if not already
35   * engaged in a ForkJoin computation, commenced in the {@link
36 < * ForkJoinPool#commonPool} via {@link #fork}, {@link #invoke}, or
36 > * ForkJoinPool#commonPool()} via {@link #fork}, {@link #invoke}, or
37   * related methods.  Once started, it will usually in turn start other
38   * subtasks.  As indicated by the name of this class, many programs
39   * using {@code ForkJoinTask} employ only methods {@link #fork} and
# Line 55 | Line 55 | import java.lang.reflect.Constructor;
55   * minimize other blocking synchronization apart from joining other
56   * tasks or using synchronizers such as Phasers that are advertised to
57   * cooperate with fork/join scheduling. Subdividable tasks should also
58 < * not perform blocking IO, and should ideally access variables that
58 > * not perform blocking I/O, and should ideally access variables that
59   * are completely independent of those accessed by other running
60   * tasks. These guidelines are loosely enforced by not permitting
61   * checked exceptions such as {@code IOExceptions} to be
# Line 71 | Line 71 | import java.lang.reflect.Constructor;
71   * encountering the exception; minimally only the latter.
72   *
73   * <p>It is possible to define and use ForkJoinTasks that may block,
74 < * but doing do requires three further considerations: (1) Completion
74 > * but doing so requires three further considerations: (1) Completion
75   * of few if any <em>other</em> tasks should be dependent on a task
76 < * that blocks on external synchronization or IO. Event-style async
76 > * that blocks on external synchronization or I/O. Event-style async
77   * tasks that are never joined (for example, those subclassing {@link
78   * CountedCompleter}) often fall into this category.  (2) To minimize
79   * resource impact, tasks should be small; ideally performing only the
# Line 134 | Line 134 | import java.lang.reflect.Constructor;
134   * (DAG). Otherwise, executions may encounter a form of deadlock as
135   * tasks cyclically wait for each other.  However, this framework
136   * supports other methods and techniques (for example the use of
137 < * {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that
137 > * {@link java.util.concurrent.Phaser Phaser}, {@link #helpQuiesce}, and {@link #complete}) that
138   * may be of use in constructing custom subclasses for problems that
139 < * are not statically structured as DAGs. To support such usages a
139 > * are not statically structured as DAGs. To support such usages, a
140   * ForkJoinTask may be atomically <em>tagged</em> with a {@code short}
141   * value using {@link #setForkJoinTaskTag} or {@link
142   * #compareAndSetForkJoinTaskTag} and checked using {@link
# Line 285 | Line 285 | public abstract class ForkJoinTask<V> im
285       */
286      private int externalAwaitDone() {
287          int s;
288 <        boolean interrupted = false;
289 <        if ((s = status) >= 0 && ForkJoinPool.tryUnsubmitFromCommonPool(this))
290 <            s = doExec();
291 <        while (s >= 0) {
292 <            if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
293 <                synchronized (this) {
294 <                    if (status >= 0) {
295 <                        try {
296 <                            wait();
297 <                        } catch (InterruptedException ie) {
298 <                            interrupted = true;
288 >        ForkJoinPool cp = ForkJoinPool.common;
289 >        if ((s = status) >= 0) {
290 >            if (cp != null) {
291 >                if (this instanceof CountedCompleter)
292 >                    s = cp.externalHelpComplete((CountedCompleter<?>)this);
293 >                else if (cp.tryExternalUnpush(this))
294 >                    s = doExec();
295 >            }
296 >            if (s >= 0 && (s = status) >= 0) {
297 >                boolean interrupted = false;
298 >                do {
299 >                    if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
300 >                        synchronized (this) {
301 >                            if (status >= 0) {
302 >                                try {
303 >                                    wait();
304 >                                } catch (InterruptedException ie) {
305 >                                    interrupted = true;
306 >                                }
307 >                            }
308 >                            else
309 >                                notifyAll();
310                          }
311                      }
312 <                    else
313 <                        notifyAll();
314 <                }
312 >                } while ((s = status) >= 0);
313 >                if (interrupted)
314 >                    Thread.currentThread().interrupt();
315              }
305            s = status;
316          }
307        if (interrupted)
308            Thread.currentThread().interrupt();
317          return s;
318      }
319  
# Line 313 | Line 321 | public abstract class ForkJoinTask<V> im
321       * Blocks a non-worker-thread until completion or interruption.
322       */
323      private int externalInterruptibleAwaitDone() throws InterruptedException {
324 +        int s;
325 +        ForkJoinPool cp = ForkJoinPool.common;
326          if (Thread.interrupted())
327              throw new InterruptedException();
328 <        int s;
329 <        if ((s = status) >= 0 && ForkJoinPool.tryUnsubmitFromCommonPool(this))
330 <            s = doExec();
331 <        while (s >= 0) {
328 >        if ((s = status) >= 0 && cp != null) {
329 >            if (this instanceof CountedCompleter)
330 >                cp.externalHelpComplete((CountedCompleter<?>)this);
331 >            else if (cp.tryExternalUnpush(this))
332 >                doExec();
333 >        }
334 >        while ((s = status) >= 0) {
335              if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
336                  synchronized (this) {
337                      if (status >= 0)
# Line 327 | Line 340 | public abstract class ForkJoinTask<V> im
340                          notifyAll();
341                  }
342              }
330            s = status;
343          }
344          return s;
345      }
346  
347 +
348      /**
349       * Implementation for join, get, quietlyJoin. Directly handles
350       * only cases of already-completed, external wait, and
# Line 398 | Line 411 | public abstract class ForkJoinTask<V> im
411          final Throwable ex;
412          ExceptionNode next;
413          final long thrower;  // use id not ref to avoid weak cycles
414 +        final int hashCode;  // store task hashCode before weak ref disappears
415          ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next) {
416              super(task, exceptionTableRefQueue);
417              this.ex = ex;
418              this.next = next;
419              this.thrower = Thread.currentThread().getId();
420 +            this.hashCode = System.identityHashCode(task);
421          }
422      }
423  
# Line 438 | Line 453 | public abstract class ForkJoinTask<V> im
453      }
454  
455      /**
456 <     * Records exception and possibly propagates
456 >     * Records exception and possibly propagates.
457       *
458       * @return status on exit
459       */
# Line 471 | Line 486 | public abstract class ForkJoinTask<V> im
486      }
487  
488      /**
489 <     * Removes exception node and clears status
489 >     * Removes exception node and clears status.
490       */
491      private void clearExceptionalCompletion() {
492          int h = System.identityHashCode(this);
# Line 561 | Line 576 | public abstract class ForkJoinTask<V> im
576      /**
577       * Poll stale refs and remove them. Call only while holding lock.
578       */
579 +    /**
580 +     * Poll stale refs and remove them. Call only while holding lock.
581 +     */
582      private static void expungeStaleExceptions() {
583          for (Object x; (x = exceptionTableRefQueue.poll()) != null;) {
584              if (x instanceof ExceptionNode) {
585 <                ForkJoinTask<?> key = ((ExceptionNode)x).get();
585 >                int hashCode = ((ExceptionNode)x).hashCode;
586                  ExceptionNode[] t = exceptionTable;
587 <                int i = System.identityHashCode(key) & (t.length - 1);
587 >                int i = hashCode & (t.length - 1);
588                  ExceptionNode e = t[i];
589                  ExceptionNode pred = null;
590                  while (e != null) {
# Line 601 | Line 619 | public abstract class ForkJoinTask<V> im
619      }
620  
621      /**
622 +     * A version of "sneaky throw" to relay exceptions
623 +     */
624 +    static void rethrow(Throwable ex) {
625 +        if (ex != null)
626 +            ForkJoinTask.<RuntimeException>uncheckedThrow(ex);
627 +    }
628 +
629 +    /**
630 +     * The sneaky part of sneaky throw, relying on generics
631 +     * limitations to evade compiler complaints about rethrowing
632 +     * unchecked exceptions
633 +     */
634 +    @SuppressWarnings("unchecked") static <T extends Throwable>
635 +        void uncheckedThrow(Throwable t) throws T {
636 +        throw (T)t; // rely on vacuous cast
637 +    }
638 +
639 +    /**
640       * Throws exception, if any, associated with the given status.
641       */
642      private void reportException(int s) {
643 <        Throwable ex = ((s == CANCELLED) ?  new CancellationException() :
644 <                        (s == EXCEPTIONAL) ? getThrowableException() :
645 <                        null);
646 <        if (ex != null)
611 <            U.throwException(ex);
643 >        if (s == CANCELLED)
644 >            throw new CancellationException();
645 >        if (s == EXCEPTIONAL)
646 >            rethrow(getThrowableException());
647      }
648  
649      // public methods
# Line 616 | Line 651 | public abstract class ForkJoinTask<V> im
651      /**
652       * Arranges to asynchronously execute this task in the pool the
653       * current task is running in, if applicable, or using the {@link
654 <     * ForkJoinPool#commonPool} if not {@link #inForkJoinPool}.  While
654 >     * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}.  While
655       * it is not necessarily enforced, it is a usage error to fork a
656       * task more than once unless it has completed and been
657       * reinitialized.  Subsequent modifications to the state of this
# Line 633 | Line 668 | public abstract class ForkJoinTask<V> im
668          if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
669              ((ForkJoinWorkerThread)t).workQueue.push(this);
670          else
671 <            ForkJoinPool.submitToCommonPool(this);
671 >            ForkJoinPool.common.externalPush(this);
672          return this;
673      }
674  
# Line 735 | Line 770 | public abstract class ForkJoinTask<V> im
770              }
771          }
772          if (ex != null)
773 <            U.throwException(ex);
773 >            rethrow(ex);
774      }
775  
776      /**
# Line 752 | Line 787 | public abstract class ForkJoinTask<V> im
787       * unprocessed.
788       *
789       * @param tasks the collection of tasks
790 +     * @param <T> the type of the values returned from the tasks
791       * @return the tasks argument, to simplify usage
792       * @throws NullPointerException if tasks or any element are null
793       */
# Line 786 | Line 822 | public abstract class ForkJoinTask<V> im
822              }
823          }
824          if (ex != null)
825 <            U.throwException(ex);
825 >            rethrow(ex);
826          return tasks;
827      }
828  
# Line 809 | Line 845 | public abstract class ForkJoinTask<V> im
845       * <p>This method is designed to be invoked by <em>other</em>
846       * tasks. To terminate the current task, you can just return or
847       * throw an unchecked exception from its computation method, or
848 <     * invoke {@link #completeExceptionally}.
848 >     * invoke {@link #completeExceptionally(Throwable)}.
849       *
850       * @param mayInterruptIfRunning this value has no effect in the
851       * default implementation because interrupts are not used to
# Line 959 | Line 995 | public abstract class ForkJoinTask<V> im
995          if (Thread.interrupted())
996              throw new InterruptedException();
997          // Messy in part because we measure in nanosecs, but wait in millisecs
998 <        int s; long ns, ms;
999 <        if ((s = status) >= 0 && (ns = unit.toNanos(timeout)) > 0L) {
998 >        int s; long ms;
999 >        long ns = unit.toNanos(timeout);
1000 >        ForkJoinPool cp;
1001 >        if ((s = status) >= 0 && ns > 0L) {
1002              long deadline = System.nanoTime() + ns;
1003              ForkJoinPool p = null;
1004              ForkJoinPool.WorkQueue w = null;
# Line 969 | Line 1007 | public abstract class ForkJoinTask<V> im
1007                  ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
1008                  p = wt.pool;
1009                  w = wt.workQueue;
1010 <                s = p.helpJoinOnce(w, this); // no retries on failure
1010 >                p.helpJoinOnce(w, this); // no retries on failure
1011 >            }
1012 >            else if ((cp = ForkJoinPool.common) != null) {
1013 >                if (this instanceof CountedCompleter)
1014 >                    cp.externalHelpComplete((CountedCompleter<?>)this);
1015 >                else if (cp.tryExternalUnpush(this))
1016 >                    doExec();
1017              }
1018              boolean canBlock = false;
1019              boolean interrupted = false;
1020              try {
1021                  while ((s = status) >= 0) {
1022 <                    if (w != null && w.runState < 0)
1022 >                    if (w != null && w.qlock < 0)
1023                          cancelIgnoringExceptions(this);
1024                      else if (!canBlock) {
1025 <                        if (p == null || p.tryCompensate(this, null))
1025 >                        if (p == null || p.tryCompensate(p.ctl))
1026                              canBlock = true;
1027                      }
1028                      else {
# Line 1042 | Line 1086 | public abstract class ForkJoinTask<V> im
1086  
1087      /**
1088       * Possibly executes tasks until the pool hosting the current task
1089 <     * {@link ForkJoinPool#isQuiescent is quiescent}. This method may
1090 <     * be of use in designs in which many tasks are forked, but none
1091 <     * are explicitly joined, instead executing them until all are
1092 <     * processed.
1089 >     * {@linkplain ForkJoinPool#isQuiescent is quiescent}.  This
1090 >     * method may be of use in designs in which many tasks are forked,
1091 >     * but none are explicitly joined, instead executing them until
1092 >     * all are processed.
1093       */
1094      public static void helpQuiesce() {
1095          Thread t;
# Line 1054 | Line 1098 | public abstract class ForkJoinTask<V> im
1098              wt.pool.helpQuiescePool(wt.workQueue);
1099          }
1100          else
1101 <            ForkJoinPool.externalHelpQuiescePool();
1101 >            ForkJoinPool.quiesceCommonPool();
1102      }
1103  
1104      /**
# Line 1117 | Line 1161 | public abstract class ForkJoinTask<V> im
1161       */
1162      public boolean tryUnfork() {
1163          Thread t;
1164 <        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1165 <            ((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) :
1166 <            ForkJoinPool.tryUnsubmitFromCommonPool(this);
1164 >        return (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1165 >                ((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) :
1166 >                ForkJoinPool.common.tryExternalUnpush(this));
1167      }
1168  
1169      /**
# Line 1131 | Line 1175 | public abstract class ForkJoinTask<V> im
1175       * @return the number of tasks
1176       */
1177      public static int getQueuedTaskCount() {
1178 <        Thread t;
1179 <        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1180 <            ((ForkJoinWorkerThread)t).workQueue.queueSize() :
1181 <            ForkJoinPool.getEstimatedSubmitterQueueLength();
1178 >        Thread t; ForkJoinPool.WorkQueue q;
1179 >        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
1180 >            q = ((ForkJoinWorkerThread)t).workQueue;
1181 >        else
1182 >            q = ForkJoinPool.commonSubmitterQueue();
1183 >        return (q == null) ? 0 : q.queueSize();
1184      }
1185  
1186      /**
# Line 1151 | Line 1197 | public abstract class ForkJoinTask<V> im
1197       * @return the surplus number of tasks, which may be negative
1198       */
1199      public static int getSurplusQueuedTaskCount() {
1200 <        /*
1155 <         * The aim of this method is to return a cheap heuristic guide
1156 <         * for task partitioning when programmers, frameworks, tools,
1157 <         * or languages have little or no idea about task granularity.
1158 <         * In essence by offering this method, we ask users only about
1159 <         * tradeoffs in overhead vs expected throughput and its
1160 <         * variance, rather than how finely to partition tasks.
1161 <         *
1162 <         * In a steady state strict (tree-structured) computation,
1163 <         * each thread makes available for stealing enough tasks for
1164 <         * other threads to remain active. Inductively, if all threads
1165 <         * play by the same rules, each thread should make available
1166 <         * only a constant number of tasks.
1167 <         *
1168 <         * The minimum useful constant is just 1. But using a value of
1169 <         * 1 would require immediate replenishment upon each steal to
1170 <         * maintain enough tasks, which is infeasible.  Further,
1171 <         * partitionings/granularities of offered tasks should
1172 <         * minimize steal rates, which in general means that threads
1173 <         * nearer the top of computation tree should generate more
1174 <         * than those nearer the bottom. In perfect steady state, each
1175 <         * thread is at approximately the same level of computation
1176 <         * tree. However, producing extra tasks amortizes the
1177 <         * uncertainty of progress and diffusion assumptions.
1178 <         *
1179 <         * So, users will want to use values larger, but not much
1180 <         * larger than 1 to both smooth over transient shortages and
1181 <         * hedge against uneven progress; as traded off against the
1182 <         * cost of extra task overhead. We leave the user to pick a
1183 <         * threshold value to compare with the results of this call to
1184 <         * guide decisions, but recommend values such as 3.
1185 <         *
1186 <         * When all threads are active, it is on average OK to
1187 <         * estimate surplus strictly locally. In steady-state, if one
1188 <         * thread is maintaining say 2 surplus tasks, then so are
1189 <         * others. So we can just use estimated queue length.
1190 <         * However, this strategy alone leads to serious mis-estimates
1191 <         * in some non-steady-state conditions (ramp-up, ramp-down,
1192 <         * other stalls). We can detect many of these by further
1193 <         * considering the number of "idle" threads, that are known to
1194 <         * have zero queued tasks, so compensate by a factor of
1195 <         * (#idle/#active) threads.
1196 <         */
1197 <        Thread t; ForkJoinWorkerThread wt;
1198 <        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1199 <            (wt = (ForkJoinWorkerThread)t).workQueue.queueSize() - wt.pool.idlePerActive() :
1200 <            0;
1200 >        return ForkJoinPool.getSurplusQueuedTaskCount();
1201      }
1202  
1203      // Extension methods
# Line 1241 | Line 1241 | public abstract class ForkJoinTask<V> im
1241      /**
1242       * Returns, but does not unschedule or execute, a task queued by
1243       * the current thread but not yet executed, if one is immediately
1244 <     * available and the current thread is operating in a
1245 <     * ForkJoinPool. There is no guarantee that this task will
1246 <     * actually be polled or executed next. Conversely, this method
1247 <     * may return null even if a task exists but cannot be accessed
1248 <     * without contention with other threads.  This method is designed
1244 >     * available. There is no guarantee that this task will actually
1245 >     * be polled or executed next. Conversely, this method may return
1246 >     * null even if a task exists but cannot be accessed without
1247 >     * contention with other threads.  This method is designed
1248       * primarily to support extensions, and is unlikely to be useful
1249       * otherwise.
1250       *
1251       * @return the next task, or {@code null} if none are available
1252       */
1253      protected static ForkJoinTask<?> peekNextLocalTask() {
1254 <        Thread t;
1255 <        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1256 <            ((ForkJoinWorkerThread)t).workQueue.peek() :
1257 <            null;
1254 >        Thread t; ForkJoinPool.WorkQueue q;
1255 >        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
1256 >            q = ((ForkJoinWorkerThread)t).workQueue;
1257 >        else
1258 >            q = ForkJoinPool.commonSubmitterQueue();
1259 >        return (q == null) ? null : q.peek();
1260      }
1261  
1262      /**
# Line 1331 | Line 1332 | public abstract class ForkJoinTask<V> im
1332       *
1333       * @param e the expected tag value
1334       * @param tag the new tag value
1335 <     * @return true if successful; i.e., the current value was
1335 >     * @return {@code true} if successful; i.e., the current value was
1336       * equal to e and is now tag.
1337       * @since 1.8
1338       */
# Line 1346 | Line 1347 | public abstract class ForkJoinTask<V> im
1347      }
1348  
1349      /**
1350 <     * Adaptor for Runnables. This implements RunnableFuture
1350 >     * Adapter for Runnables. This implements RunnableFuture
1351       * to be compliant with AbstractExecutorService constraints
1352       * when used in ForkJoinPool.
1353       */
# Line 1367 | Line 1368 | public abstract class ForkJoinTask<V> im
1368      }
1369  
1370      /**
1371 <     * Adaptor for Runnables without results
1371 >     * Adapter for Runnables without results
1372       */
1373      static final class AdaptedRunnableAction extends ForkJoinTask<Void>
1374          implements RunnableFuture<Void> {
# Line 1384 | Line 1385 | public abstract class ForkJoinTask<V> im
1385      }
1386  
1387      /**
1388 <     * Adaptor for Callables
1388 >     * Adapter for Runnables in which failure forces worker exception
1389 >     */
1390 >    static final class RunnableExecuteAction extends ForkJoinTask<Void> {
1391 >        final Runnable runnable;
1392 >        RunnableExecuteAction(Runnable runnable) {
1393 >            if (runnable == null) throw new NullPointerException();
1394 >            this.runnable = runnable;
1395 >        }
1396 >        public final Void getRawResult() { return null; }
1397 >        public final void setRawResult(Void v) { }
1398 >        public final boolean exec() { runnable.run(); return true; }
1399 >        void internalPropagateException(Throwable ex) {
1400 >            rethrow(ex); // rethrow outside exec() catches.
1401 >        }
1402 >        private static final long serialVersionUID = 5232453952276885070L;
1403 >    }
1404 >
1405 >    /**
1406 >     * Adapter for Callables
1407       */
1408      static final class AdaptedCallable<T> extends ForkJoinTask<T>
1409          implements RunnableFuture<T> {
# Line 1431 | Line 1450 | public abstract class ForkJoinTask<V> im
1450       *
1451       * @param runnable the runnable action
1452       * @param result the result upon completion
1453 +     * @param <T> the type of the result
1454       * @return the task
1455       */
1456      public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
# Line 1444 | Line 1464 | public abstract class ForkJoinTask<V> im
1464       * encountered into {@code RuntimeException}.
1465       *
1466       * @param callable the callable action
1467 +     * @param <T> the type of the callable's result
1468       * @return the task
1469       */
1470      public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) {
# Line 1457 | Line 1478 | public abstract class ForkJoinTask<V> im
1478      /**
1479       * Saves this task to a stream (that is, serializes it).
1480       *
1481 +     * @param s the stream
1482 +     * @throws java.io.IOException if an I/O error occurs
1483       * @serialData the current run status and the exception thrown
1484       * during execution, or {@code null} if none
1485       */
# Line 1468 | Line 1491 | public abstract class ForkJoinTask<V> im
1491  
1492      /**
1493       * Reconstitutes this task from a stream (that is, deserializes it).
1494 +     * @param s the stream
1495 +     * @throws ClassNotFoundException if the class of a serialized object
1496 +     *         could not be found
1497 +     * @throws java.io.IOException if an I/O error occurs
1498       */
1499      private void readObject(java.io.ObjectInputStream s)
1500          throws java.io.IOException, ClassNotFoundException {
# Line 1480 | Line 1507 | public abstract class ForkJoinTask<V> im
1507      // Unsafe mechanics
1508      private static final sun.misc.Unsafe U;
1509      private static final long STATUS;
1510 +
1511      static {
1512          exceptionTableLock = new ReentrantLock();
1513          exceptionTableRefQueue = new ReferenceQueue<Object>();
1514          exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY];
1515          try {
1516              U = getUnsafe();
1517 +            Class<?> k = ForkJoinTask.class;
1518              STATUS = U.objectFieldOffset
1519 <                (ForkJoinTask.class.getDeclaredField("status"));
1519 >                (k.getDeclaredField("status"));
1520          } catch (Exception e) {
1521              throw new Error(e);
1522          }
# Line 1503 | Line 1532 | public abstract class ForkJoinTask<V> im
1532      private static sun.misc.Unsafe getUnsafe() {
1533          try {
1534              return sun.misc.Unsafe.getUnsafe();
1535 <        } catch (SecurityException se) {
1536 <            try {
1537 <                return java.security.AccessController.doPrivileged
1538 <                    (new java.security
1539 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1540 <                        public sun.misc.Unsafe run() throws Exception {
1541 <                            java.lang.reflect.Field f = sun.misc
1542 <                                .Unsafe.class.getDeclaredField("theUnsafe");
1543 <                            f.setAccessible(true);
1544 <                            return (sun.misc.Unsafe) f.get(null);
1545 <                        }});
1546 <            } catch (java.security.PrivilegedActionException e) {
1547 <                throw new RuntimeException("Could not initialize intrinsics",
1548 <                                           e.getCause());
1549 <            }
1535 >        } catch (SecurityException tryReflectionInstead) {}
1536 >        try {
1537 >            return java.security.AccessController.doPrivileged
1538 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1539 >                public sun.misc.Unsafe run() throws Exception {
1540 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
1541 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
1542 >                        f.setAccessible(true);
1543 >                        Object x = f.get(null);
1544 >                        if (k.isInstance(x))
1545 >                            return k.cast(x);
1546 >                    }
1547 >                    throw new NoSuchFieldError("the Unsafe");
1548 >                }});
1549 >        } catch (java.security.PrivilegedActionException e) {
1550 >            throw new RuntimeException("Could not initialize intrinsics",
1551 >                                       e.getCause());
1552          }
1553      }
1554   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines