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

Comparing jsr166/src/jsr166y/ForkJoinPool.java (file contents):
Revision 1.61 by dl, Wed Aug 11 18:45:12 2010 UTC vs.
Revision 1.71 by jsr166, Mon Sep 6 21:36:43 2010 UTC

# Line 7 | Line 7
7   package jsr166y;
8  
9   import java.util.concurrent.*;
10
10   import java.util.ArrayList;
11   import java.util.Arrays;
12   import java.util.Collection;
# Line 69 | Line 68 | import java.util.concurrent.CountDownLat
68   *    <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td>
69   *  </tr>
70   *  <tr>
71 < *    <td> <b>Arange async execution</td>
71 > *    <td> <b>Arrange async execution</td>
72   *    <td> {@link #execute(ForkJoinTask)}</td>
73   *    <td> {@link ForkJoinTask#fork}</td>
74   *  </tr>
# Line 110 | Line 109 | import java.util.concurrent.CountDownLat
109   *
110   * <p>This implementation rejects submitted tasks (that is, by throwing
111   * {@link RejectedExecutionException}) only when the pool is shut down
112 < * or internal resources have been exhuasted.
112 > * or internal resources have been exhausted.
113   *
114   * @since 1.7
115   * @author Doug Lea
# Line 140 | Line 139 | public class ForkJoinPool extends Abstra
139       * Beyond work-stealing support and essential bookkeeping, the
140       * main responsibility of this framework is to take actions when
141       * one worker is waiting to join a task stolen (or always held by)
142 <     * another.  Becauae we are multiplexing many tasks on to a pool
142 >     * another.  Because we are multiplexing many tasks on to a pool
143       * of workers, we can't just let them block (as in Thread.join).
144       * We also cannot just reassign the joiner's run-time stack with
145       * another and replace it later, which would be a form of
# Line 157 | Line 156 | public class ForkJoinPool extends Abstra
156       *      links to try to find such a task.
157       *
158       *   Compensating: Unless there are already enough live threads,
159 <     *      method helpMaintainParallelism() may create or or
159 >     *      method helpMaintainParallelism() may create or
160       *      re-activate a spare thread to compensate for blocked
161       *      joiners until they unblock.
162       *
163 <     * Because the determining existence of conservatively safe
164 <     * helping targets, the availability of already-created spares,
165 <     * and the apparent need to create new spares are all racy and
166 <     * require heuristic guidance, we rely on multiple retries of
167 <     * each. Further, because it is impossible to keep exactly the
168 <     * target (parallelism) number of threads running at any given
169 <     * time, we allow compensation during joins to fail, and enlist
170 <     * all other threads to help out whenever they are not otherwise
171 <     * occupied (i.e., mainly in method preStep).
163 >     * It is impossible to keep exactly the target (parallelism)
164 >     * number of threads running at any given time.  Determining
165 >     * existence of conservatively safe helping targets, the
166 >     * availability of already-created spares, and the apparent need
167 >     * to create new spares are all racy and require heuristic
168 >     * guidance, so we rely on multiple retries of each.  Compensation
169 >     * occurs in slow-motion. It is triggered only upon timeouts of
170 >     * Object.wait used for joins. This reduces poor decisions that
171 >     * would otherwise be made when threads are waiting for others
172 >     * that are stalled because of unrelated activities such as
173 >     * garbage collection.
174       *
175       * The ManagedBlocker extension API can't use helping so relies
176       * only on compensation in method awaitBlocker.
# Line 224 | Line 225 | public class ForkJoinPool extends Abstra
225       * ManagedBlocker), we may create or resume others to take their
226       * place until they unblock (see below). Implementing this
227       * requires counts of the number of "running" threads (i.e., those
228 <     * that are neither blocked nor artifically suspended) as well as
228 >     * that are neither blocked nor artificially suspended) as well as
229       * the total number.  These two values are packed into one field,
230       * "workerCounts" because we need accurate snapshots when deciding
231       * to create, resume or suspend.  Note however that the
232 <     * correspondance of these counts to reality is not guaranteed. In
232 >     * correspondence of these counts to reality is not guaranteed. In
233       * particular updates for unblocked threads may lag until they
234       * actually wake up.
235       *
# Line 259 | Line 260 | public class ForkJoinPool extends Abstra
260       * workers that previously could not find a task to now find one:
261       * Submission of a new task to the pool, or another worker pushing
262       * a task onto a previously empty queue.  (We also use this
263 <     * mechanism for termination actions that require wakeups of idle
264 <     * workers).  Each worker maintains its last known event count,
265 <     * and blocks when a scan for work did not find a task AND its
266 <     * lastEventCount matches the current eventCount. Waiting idle
267 <     * workers are recorded in a variant of Treiber stack headed by
268 <     * field eventWaiters which, when nonzero, encodes the thread
269 <     * index and count awaited for by the worker thread most recently
270 <     * calling eventSync. This thread in turn has a record (field
271 <     * nextEventWaiter) for the next waiting worker.  In addition to
272 <     * allowing simpler decisions about need for wakeup, the event
273 <     * count bits in eventWaiters serve the role of tags to avoid ABA
274 <     * errors in Treiber stacks.  To reduce delays in task diffusion,
275 <     * workers not otherwise occupied may invoke method
276 <     * releaseEventWaiters, that removes and signals (unparks) workers
277 <     * not waiting on current count. To reduce stalls, To minimize
278 <     * task production stalls associate with signalling, any worker
279 <     * pushing a task on an empty queue invokes the weaker method
279 <     * signalWork, that only releases idle workers until it detects
280 <     * interference by other threads trying to release, and lets them
281 <     * take over.  The net effect is a tree-like diffusion of signals,
282 <     * where released threads (and possibly others) help with unparks.
283 <     * To further reduce contention effects a bit, failed CASes to
284 <     * increment field eventCount are tolerated without retries.
263 >     * mechanism for configuration and termination actions that
264 >     * require wakeups of idle workers).  Each worker maintains its
265 >     * last known event count, and blocks when a scan for work did not
266 >     * find a task AND its lastEventCount matches the current
267 >     * eventCount. Waiting idle workers are recorded in a variant of
268 >     * Treiber stack headed by field eventWaiters which, when nonzero,
269 >     * encodes the thread index and count awaited for by the worker
270 >     * thread most recently calling eventSync. This thread in turn has
271 >     * a record (field nextEventWaiter) for the next waiting worker.
272 >     * In addition to allowing simpler decisions about need for
273 >     * wakeup, the event count bits in eventWaiters serve the role of
274 >     * tags to avoid ABA errors in Treiber stacks. Upon any wakeup,
275 >     * released threads also try to release at most two others.  The
276 >     * net effect is a tree-like diffusion of signals, where released
277 >     * threads (and possibly others) help with unparks.  To further
278 >     * reduce contention effects a bit, failed CASes to increment
279 >     * field eventCount are tolerated without retries in signalWork.
280       * Conceptually they are merged into the same event, which is OK
281       * when their only purpose is to enable workers to scan for work.
282       *
283 <     * 5. Managing suspension of extra workers. When a worker is about
284 <     * to block waiting for a join (or via ManagedBlockers), we may
285 <     * create a new thread to maintain parallelism level, or at least
286 <     * avoid starvation. Usually, extra threads are needed for only
287 <     * very short periods, yet join dependencies are such that we
288 <     * sometimes need them in bursts. Rather than create new threads
289 <     * each time this happens, we suspend no-longer-needed extra ones
290 <     * as "spares". For most purposes, we don't distinguish "extra"
291 <     * spare threads from normal "core" threads: On each call to
292 <     * preStep (the only point at which we can do this) a worker
293 <     * checks to see if there are now too many running workers, and if
294 <     * so, suspends itself.  Method helpMaintainParallelism looks for
295 <     * suspended threads to resume before considering creating a new
296 <     * replacement. The spares themselves are encoded on another
297 <     * variant of a Treiber Stack, headed at field "spareWaiters".
298 <     * Note that the use of spares is intrinsically racy.  One thread
299 <     * may become a spare at about the same time as another is
300 <     * needlessly being created. We counteract this and related slop
301 <     * in part by requiring resumed spares to immediately recheck (in
302 <     * preStep) to see whether they they should re-suspend.  To avoid
303 <     * long-term build-up of spares, the oldest spare (see
304 <     * ForkJoinWorkerThread.suspendAsSpare) occasionally wakes up if
305 <     * not signalled and calls tryTrimSpare, which uses two different
306 <     * thresholds: Always killing if the number of spares is greater
307 <     * that 25% of total, and killing others only at a slower rate
308 <     * (UNUSED_SPARE_TRIM_RATE_NANOS).
283 >     * 5. Managing suspension of extra workers. When a worker notices
284 >     * (usually upon timeout of a wait()) that there are too few
285 >     * running threads, we may create a new thread to maintain
286 >     * parallelism level, or at least avoid starvation. Usually, extra
287 >     * threads are needed for only very short periods, yet join
288 >     * dependencies are such that we sometimes need them in
289 >     * bursts. Rather than create new threads each time this happens,
290 >     * we suspend no-longer-needed extra ones as "spares". For most
291 >     * purposes, we don't distinguish "extra" spare threads from
292 >     * normal "core" threads: On each call to preStep (the only point
293 >     * at which we can do this) a worker checks to see if there are
294 >     * now too many running workers, and if so, suspends itself.
295 >     * Method helpMaintainParallelism looks for suspended threads to
296 >     * resume before considering creating a new replacement. The
297 >     * spares themselves are encoded on another variant of a Treiber
298 >     * Stack, headed at field "spareWaiters".  Note that the use of
299 >     * spares is intrinsically racy.  One thread may become a spare at
300 >     * about the same time as another is needlessly being created. We
301 >     * counteract this and related slop in part by requiring resumed
302 >     * spares to immediately recheck (in preStep) to see whether they
303 >     * they should re-suspend.
304 >     *
305 >     * 6. Killing off unneeded workers. A timeout mechanism is used to
306 >     * shed unused workers: The oldest (first) event queue waiter uses
307 >     * a timed rather than hard wait. When this wait times out without
308 >     * a normal wakeup, it tries to shutdown any one (for convenience
309 >     * the newest) other spare or event waiter via
310 >     * tryShutdownUnusedWorker. This eventually reduces the number of
311 >     * worker threads to a minimum of one after a long enough period
312 >     * without use.
313       *
314 <     * 6. Deciding when to create new workers. The main dynamic
314 >     * 7. Deciding when to create new workers. The main dynamic
315       * control in this class is deciding when to create extra threads
316       * in method helpMaintainParallelism. We would like to keep
317 <     * exactly #parallelism threads running, which is an impossble
317 >     * exactly #parallelism threads running, which is an impossible
318       * task. We always need to create one when the number of running
319       * threads would become zero and all workers are busy. Beyond
320 <     * this, we must rely on heuristics that work well in the the
321 <     * presence of transients phenomena such as GC stalls, dynamic
320 >     * this, we must rely on heuristics that work well in the
321 >     * presence of transient phenomena such as GC stalls, dynamic
322       * compilation, and wake-up lags. These transients are extremely
323       * common -- we are normally trying to fully saturate the CPUs on
324       * a machine, so almost any activity other than running tasks
325 <     * impedes accuracy. Our main defense is to allow some slack in
326 <     * creation thresholds, using rules that reflect the fact that the
327 <     * more threads we have running, the more likely that we are
328 <     * underestimating the number running threads. The rules also
329 <     * better cope with the fact that some of the methods in this
330 <     * class tend to never become compiled (but are interpreted), so
331 <     * some components of the entire set of controls might execute 100
332 <     * times faster than others. And similarly for cases where the
333 <     * apparent lack of work is just due to GC stalls and other
335 <     * transient system activity.
325 >     * impedes accuracy. Our main defense is to allow parallelism to
326 >     * lapse for a while during joins, and use a timeout to see if,
327 >     * after the resulting settling, there is still a need for
328 >     * additional workers.  This also better copes with the fact that
329 >     * some of the methods in this class tend to never become compiled
330 >     * (but are interpreted), so some components of the entire set of
331 >     * controls might execute 100 times faster than others. And
332 >     * similarly for cases where the apparent lack of work is just due
333 >     * to GC stalls and other transient system activity.
334       *
335       * Beware that there is a lot of representation-level coupling
336       * among classes ForkJoinPool, ForkJoinWorkerThread, and
# Line 347 | Line 345 | public class ForkJoinPool extends Abstra
345       * "while ((local = field) != 0)") which are usually the simplest
346       * way to ensure the required read orderings (which are sometimes
347       * critical). Also several occurrences of the unusual "do {}
348 <     * while(!cas...)" which is the simplest way to force an update of
348 >     * while (!cas...)" which is the simplest way to force an update of
349       * a CAS'ed variable. There are also other coding oddities that
350       * help some methods perform reasonably even when interpreted (not
351       * compiled), at the expense of some messy constructions that
# Line 419 | Line 417 | public class ForkJoinPool extends Abstra
417          new AtomicInteger();
418  
419      /**
420 +     * The time to block in a join (see awaitJoin) before checking if
421 +     * a new worker should be (re)started to maintain parallelism
422 +     * level. The value should be short enough to maintain global
423 +     * responsiveness and progress but long enough to avoid
424 +     * counterproductive firings during GC stalls or unrelated system
425 +     * activity, and to not bog down systems with continual re-firings
426 +     * on GCs or legitimately long waits.
427 +     */
428 +    private static final long JOIN_TIMEOUT_MILLIS = 250L; // 4 per second
429 +
430 +    /**
431 +     * The wakeup interval (in nanoseconds) for the oldest worker
432 +     * worker waiting for an event invokes tryShutdownUnusedWorker to shrink
433 +     * the number of workers.  The exact value does not matter too
434 +     * much, but should be long enough to slowly release resources
435 +     * during long periods without use without disrupting normal use.
436 +     */
437 +    private static final long SHRINK_RATE_NANOS =
438 +        30L * 1000L * 1000L * 1000L; // 2 per minute
439 +
440 +    /**
441       * Absolute bound for parallelism level. Twice this number plus
442       * one (i.e., 0xfff) must fit into a 16bit field to enable
443       * word-packing for some counts and indices.
# Line 463 | Line 482 | public class ForkJoinPool extends Abstra
482      private volatile long stealCount;
483  
484      /**
485 <     * The last nanoTime that a spare thread was trimmed
467 <     */
468 <    private volatile long trimTime;
469 <
470 <    /**
471 <     * The rate at which to trim unused spares
472 <     */
473 <    static final long UNUSED_SPARE_TRIM_RATE_NANOS =
474 <        1000L * 1000L * 1000L; // 1 sec
475 <
476 <    /**
477 <     * Encoded record of top of treiber stack of threads waiting for
485 >     * Encoded record of top of Treiber stack of threads waiting for
486       * events. The top 32 bits contain the count being waited for. The
487       * bottom 16 bits contains one plus the pool index of waiting
488       * worker thread. (Bits 16-31 are unused.)
# Line 493 | Line 501 | public class ForkJoinPool extends Abstra
501      private volatile int eventCount;
502  
503      /**
504 <     * Encoded record of top of treiber stack of spare threads waiting
504 >     * Encoded record of top of Treiber stack of spare threads waiting
505       * for resumption. The top 16 bits contain an arbitrary count to
506       * avoid ABA effects. The bottom 16bits contains one plus the pool
507       * index of waiting worker thread.
# Line 514 | Line 522 | public class ForkJoinPool extends Abstra
522       * These are bundled together to ensure consistent read for
523       * termination checks (i.e., that runLevel is at least SHUTDOWN
524       * and active threads is zero).
525 +     *
526 +     * Notes: Most direct CASes are dependent on these bitfield
527 +     * positions.  Also, this field is non-private to enable direct
528 +     * performance-sensitive CASes in ForkJoinWorkerThread.
529       */
530 <    private volatile int runState;
530 >    volatile int runState;
531  
532      // Note: The order among run level values matters.
533      private static final int RUNLEVEL_SHIFT     = 16;
# Line 523 | Line 535 | public class ForkJoinPool extends Abstra
535      private static final int TERMINATING        = 1 << (RUNLEVEL_SHIFT + 1);
536      private static final int TERMINATED         = 1 << (RUNLEVEL_SHIFT + 2);
537      private static final int ACTIVE_COUNT_MASK  = (1 << RUNLEVEL_SHIFT) - 1;
526    private static final int ONE_ACTIVE         = 1; // active update delta
538  
539      /**
540       * Holds number of total (i.e., created and not yet terminated)
# Line 564 | Line 575 | public class ForkJoinPool extends Abstra
575       */
576      private final int poolNumber;
577  
578 <
579 <    // Utilities for CASing fields. Note that several of these
569 <    // are manually inlined by callers
578 >    // Utilities for CASing fields. Note that most of these
579 >    // are usually manually inlined by callers
580  
581      /**
582       * Increments running count part of workerCounts
# Line 599 | Line 609 | public class ForkJoinPool extends Abstra
609      private void decrementWorkerCounts(int dr, int dt) {
610          for (;;) {
611              int wc = workerCounts;
602            if (wc == 0 && (runState & TERMINATED) != 0)
603                return; // lagging termination on a backout
612              if ((wc & RUNNING_COUNT_MASK)  - dr < 0 ||
613 <                (wc >>> TOTAL_COUNT_SHIFT) - dt < 0)
613 >                (wc >>> TOTAL_COUNT_SHIFT) - dt < 0) {
614 >                if ((runState & TERMINATED) != 0)
615 >                    return; // lagging termination on a backout
616                  Thread.yield();
617 +            }
618              if (UNSAFE.compareAndSwapInt(this, workerCountsOffset,
619                                           wc, wc - (dr + dt)))
620                  return;
# Line 611 | Line 622 | public class ForkJoinPool extends Abstra
622      }
623  
624      /**
614     * Increments event count
615     */
616    private void advanceEventCount() {
617        int c;
618        do {} while(!UNSAFE.compareAndSwapInt(this, eventCountOffset,
619                                              c = eventCount, c+1));
620    }
621
622    /**
623     * Tries incrementing active count; fails on contention.
624     * Called by workers before executing tasks.
625     *
626     * @return true on success
627     */
628    final boolean tryIncrementActiveCount() {
629        int c;
630        return UNSAFE.compareAndSwapInt(this, runStateOffset,
631                                        c = runState, c + ONE_ACTIVE);
632    }
633
634    /**
625       * Tries decrementing active count; fails on contention.
626       * Called when workers cannot find tasks to run.
627       */
628      final boolean tryDecrementActiveCount() {
629          int c;
630          return UNSAFE.compareAndSwapInt(this, runStateOffset,
631 <                                        c = runState, c - ONE_ACTIVE);
631 >                                        c = runState, c - 1);
632      }
633  
634      /**
# Line 683 | Line 673 | public class ForkJoinPool extends Abstra
673      }
674  
675      /**
676 <     * Nulls out record of worker in workers array
676 >     * Nulls out record of worker in workers array.
677       */
678      private void forgetWorker(ForkJoinWorkerThread w) {
679          int idx = w.poolIndex;
680 <        // Locking helps method recordWorker avoid unecessary expansion
680 >        // Locking helps method recordWorker avoid unnecessary expansion
681          final ReentrantLock lock = this.workerLock;
682          lock.lock();
683          try {
# Line 699 | Line 689 | public class ForkJoinPool extends Abstra
689          }
690      }
691  
702    // adding and removing workers
703
704    /**
705     * Tries to create and add new worker. Assumes that worker counts
706     * are already updated to accommodate the worker, so adjusts on
707     * failure.
708     */
709    private void addWorker() {
710        ForkJoinWorkerThread w = null;
711        try {
712            w = factory.newThread(this);
713        } finally { // Adjust on either null or exceptional factory return
714            if (w == null) {
715                decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL);
716                tryTerminate(false); // in case of failure during shutdown
717            }
718        }
719        if (w != null)
720            w.start(recordWorker(w), ueh);
721    }
722
692      /**
693       * Final callback from terminating worker.  Removes record of
694       * worker from array, and adjusts counts. If pool is shutting
695 <     * down, tries to complete terminatation.
695 >     * down, tries to complete termination.
696       *
697       * @param w the worker
698       */
# Line 740 | Line 709 | public class ForkJoinPool extends Abstra
709      /**
710       * Releases workers blocked on a count not equal to current count.
711       * Normally called after precheck that eventWaiters isn't zero to
712 <     * avoid wasted array checks.
713 <     *
745 <     * @param signalling true if caller is a signalling worker so can
746 <     * exit upon (conservatively) detected contention by other threads
747 <     * who will continue to release
712 >     * avoid wasted array checks. Gives up upon a change in count or
713 >     * upon releasing two workers, letting others take over.
714       */
715 <    private void releaseEventWaiters(boolean signalling) {
715 >    private void releaseEventWaiters() {
716          ForkJoinWorkerThread[] ws = workers;
717          int n = ws.length;
718 <        long h; // head of stack
719 <        ForkJoinWorkerThread w; int id, ec;
720 <        while ((id = ((int)((h = eventWaiters) & WAITER_ID_MASK)) - 1) >= 0 &&
721 <               (int)(h >>> EVENT_COUNT_SHIFT) != (ec = eventCount) &&
718 >        long h = eventWaiters;
719 >        int ec = eventCount;
720 >        boolean releasedOne = false;
721 >        ForkJoinWorkerThread w; int id;
722 >        while ((id = ((int)(h & WAITER_ID_MASK)) - 1) >= 0 &&
723 >               (int)(h >>> EVENT_COUNT_SHIFT) != ec &&
724                 id < n && (w = ws[id]) != null) {
725              if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
726 <                                          h, h = w.nextWaiter))
726 >                                          h,  w.nextWaiter)) {
727                  LockSupport.unpark(w);
728 <            if (signalling && (eventCount != ec || eventWaiters != h))
728 >                if (releasedOne) // exit on second release
729 >                    break;
730 >                releasedOne = true;
731 >            }
732 >            if (eventCount != ec)
733                  break;
734 +            h = eventWaiters;
735          }
736      }
737  
# Line 770 | Line 743 | public class ForkJoinPool extends Abstra
743          int c; // try to increment event count -- CAS failure OK
744          UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
745          if (eventWaiters != 0L)
746 <            releaseEventWaiters(true);
746 >            releaseEventWaiters();
747      }
748  
749      /**
750 <     * Blocks worker until terminating or event count
751 <     * advances from last value held by worker
750 >     * Adds the given worker to event queue and blocks until
751 >     * terminating or event count advances from the given value
752       *
753       * @param w the calling worker thread
754 +     * @param ec the count
755       */
756 <    private void eventSync(ForkJoinWorkerThread w) {
757 <        int wec = w.lastEventCount;
784 <        long nh = (((long)wec) << EVENT_COUNT_SHIFT) | ((long)(w.poolIndex+1));
756 >    private void eventSync(ForkJoinWorkerThread w, int ec) {
757 >        long nh = (((long)ec) << EVENT_COUNT_SHIFT) | ((long)(w.poolIndex+1));
758          long h;
759          while ((runState < SHUTDOWN || !tryTerminate(false)) &&
760 <               ((h = eventWaiters) == 0L ||
761 <                (int)(h >>> EVENT_COUNT_SHIFT) == wec) &&
762 <               eventCount == wec) {
760 >               (((int)((h = eventWaiters) & WAITER_ID_MASK)) == 0 ||
761 >                (int)(h >>> EVENT_COUNT_SHIFT) == ec) &&
762 >               eventCount == ec) {
763              if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
764                                            w.nextWaiter = h, nh)) {
765 <                while (runState < TERMINATING && eventCount == wec) {
766 <                    if (!tryAccumulateStealCount(w))  // transfer while idle
767 <                        continue;
768 <                    Thread.interrupted();             // clear/ignore interrupt
769 <                    if (eventCount != wec)
770 <                        break;
765 >                awaitEvent(w, ec);
766 >                break;
767 >            }
768 >        }
769 >    }
770 >
771 >    /**
772 >     * Blocks the given worker (that has already been entered as an
773 >     * event waiter) until terminating or event count advances from
774 >     * the given value. The oldest (first) waiter uses a timed wait to
775 >     * occasionally one-by-one shrink the number of workers (to a
776 >     * minimum of one) if the pool has not been used for extended
777 >     * periods.
778 >     *
779 >     * @param w the calling worker thread
780 >     * @param ec the count
781 >     */
782 >    private void awaitEvent(ForkJoinWorkerThread w, int ec) {
783 >        while (eventCount == ec) {
784 >            if (tryAccumulateStealCount(w)) { // transfer while idle
785 >                boolean untimed = (w.nextWaiter != 0L ||
786 >                                   (workerCounts & RUNNING_COUNT_MASK) <= 1);
787 >                long startTime = untimed? 0 : System.nanoTime();
788 >                Thread.interrupted();         // clear/ignore interrupt
789 >                if (eventCount != ec || w.runState != 0 ||
790 >                    runState >= TERMINATING)  // recheck after clear
791 >                    break;
792 >                if (untimed)
793                      LockSupport.park(w);
794 +                else {
795 +                    LockSupport.parkNanos(w, SHRINK_RATE_NANOS);
796 +                    if (eventCount != ec || w.runState != 0 ||
797 +                        runState >= TERMINATING)
798 +                        break;
799 +                    if (System.nanoTime() - startTime >= SHRINK_RATE_NANOS)
800 +                        tryShutdownUnusedWorker(ec);
801                  }
800                break;
802              }
803          }
803        w.lastEventCount = eventCount;
804      }
805  
806 <    // Maintaining spares
806 >    // Maintaining parallelism
807  
808      /**
809       * Pushes worker onto the spare stack
810       */
811      final void pushSpare(ForkJoinWorkerThread w) {
812 <        int ns = (++w.spareCount << SPARE_COUNT_SHIFT) | (w.poolIndex+1);
812 >        int ns = (++w.spareCount << SPARE_COUNT_SHIFT) | (w.poolIndex + 1);
813          do {} while (!UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
814                                                 w.nextSpare = spareWaiters,ns));
815      }
816  
817      /**
818 <     * Tries (once) to resume a spare if running count is less than
819 <     * target parallelism. Fails on contention or stale workers.
818 >     * Tries (once) to resume a spare if the number of running
819 >     * threads is less than target.
820       */
821      private void tryResumeSpare() {
822          int sw, id;
823 +        ForkJoinWorkerThread[] ws = workers;
824 +        int n = ws.length;
825          ForkJoinWorkerThread w;
826 <        ForkJoinWorkerThread[] ws;
827 <        if ((id = ((sw = spareWaiters) & SPARE_ID_MASK) - 1) >= 0 &&
828 <            id < (ws = workers).length && (w = ws[id]) != null &&
826 >        if ((sw = spareWaiters) != 0 &&
827 >            (id = (sw & SPARE_ID_MASK) - 1) >= 0 &&
828 >            id < n && (w = ws[id]) != null &&
829              (workerCounts & RUNNING_COUNT_MASK) < parallelism &&
828            eventWaiters == 0L &&
830              spareWaiters == sw &&
831              UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
832 <                                     sw, w.nextSpare) &&
833 <            w.tryUnsuspend()) {
834 <            int c; // try increment; if contended, finish after unpark
835 <            boolean inc = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
836 <                                                   c = workerCounts,
837 <                                                   c + ONE_RUNNING);
838 <            LockSupport.unpark(w);
839 <            if (!inc) {
840 <                do {} while(!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
840 <                                                      c = workerCounts,
841 <                                                      c + ONE_RUNNING));
842 <            }
832 >                                     sw, w.nextSpare)) {
833 >            int c; // increment running count before resume
834 >            do {} while (!UNSAFE.compareAndSwapInt
835 >                         (this, workerCountsOffset,
836 >                          c = workerCounts, c + ONE_RUNNING));
837 >            if (w.tryUnsuspend())
838 >                LockSupport.unpark(w);
839 >            else   // back out if w was shutdown
840 >                decrementWorkerCounts(ONE_RUNNING, 0);
841          }
842      }
843  
844      /**
845 <     * Callback from oldest spare occasionally waking up.  Tries
846 <     * (once) to shutdown a spare if more than 25% spare overage, or
847 <     * if UNUSED_SPARE_TRIM_RATE_NANOS have elapsed and there are at
848 <     * least #parallelism running threads. Note that we don't need CAS
849 <     * or locks here because the method is called only from the oldest
850 <     * suspended spare occasionally waking (and even misfires are OK).
851 <     *
854 <     * @param now the wake up nanoTime of caller
855 <     */
856 <    final void tryTrimSpare(long now) {
857 <        long lastTrim = trimTime;
858 <        trimTime = now;
859 <        helpMaintainParallelism(); // first, help wake up any needed spares
860 <        int sw, id;
861 <        ForkJoinWorkerThread w;
862 <        ForkJoinWorkerThread[] ws;
845 >     * Tries to increase the number of running workers if below target
846 >     * parallelism: If a spare exists tries to resume it via
847 >     * tryResumeSpare.  Otherwise, if not enough total workers or all
848 >     * existing workers are busy, adds a new worker. In all cases also
849 >     * helps wake up releasable workers waiting for work.
850 >     */
851 >    private void helpMaintainParallelism() {
852          int pc = parallelism;
853 <        int wc = workerCounts;
854 <        if ((wc & RUNNING_COUNT_MASK) >= pc &&
855 <            (((wc >>> TOTAL_COUNT_SHIFT) - pc) > (pc >>> 2) + 1 ||// approx 25%
856 <             now - lastTrim >= UNUSED_SPARE_TRIM_RATE_NANOS) &&
857 <            (id = ((sw = spareWaiters) & SPARE_ID_MASK) - 1) >= 0 &&
858 <            id < (ws = workers).length && (w = ws[id]) != null &&
859 <            UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
860 <                                     sw, w.nextSpare))
861 <            w.shutdown(false);
853 >        int wc, rs, tc;
854 >        while (((wc = workerCounts) & RUNNING_COUNT_MASK) < pc &&
855 >               (rs = runState) < TERMINATING) {
856 >            if (spareWaiters != 0)
857 >                tryResumeSpare();
858 >            else if ((tc = wc >>> TOTAL_COUNT_SHIFT) >= MAX_WORKERS ||
859 >                     (tc >= pc && (rs & ACTIVE_COUNT_MASK) != tc))
860 >                break;   // enough total
861 >            else if (runState == rs && workerCounts == wc &&
862 >                     UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
863 >                                              wc + (ONE_RUNNING|ONE_TOTAL))) {
864 >                ForkJoinWorkerThread w = null;
865 >                try {
866 >                    w = factory.newThread(this);
867 >                } finally { // adjust on null or exceptional factory return
868 >                    if (w == null) {
869 >                        decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL);
870 >                        tryTerminate(false); // handle failure during shutdown
871 >                    }
872 >                }
873 >                if (w == null)
874 >                    break;
875 >                w.start(recordWorker(w), ueh);
876 >                if ((workerCounts >>> TOTAL_COUNT_SHIFT) >= pc) {
877 >                    int c; // advance event count
878 >                    UNSAFE.compareAndSwapInt(this, eventCountOffset,
879 >                                             c = eventCount, c+1);
880 >                    break; // add at most one unless total below target
881 >                }
882 >            }
883 >        }
884 >        if (eventWaiters != 0L)
885 >            releaseEventWaiters();
886      }
887  
888      /**
889 <     * Does at most one of:
890 <     *
891 <     * 1. Help wake up existing workers waiting for work via
892 <     *    releaseEventWaiters. (If any exist, then it probably doesn't
893 <     *    matter right now if under target parallelism level.)
889 >     * Callback from the oldest waiter in awaitEvent waking up after a
890 >     * period of non-use. If all workers are idle, tries (once) to
891 >     * shutdown an event waiter or a spare, if one exists. Note that
892 >     * we don't need CAS or locks here because the method is called
893 >     * only from one thread occasionally waking (and even misfires are
894 >     * OK). Note that until the shutdown worker fully terminates,
895 >     * workerCounts will overestimate total count, which is tolerable.
896       *
897 <     * 2. If below parallelism level and a spare exists, try (once)
898 <     *    to resume it via tryResumeSpare.
884 <     *
885 <     * 3. If neither of the above, tries (once) to add a new
886 <     *    worker if either there are not enough total, or if all
887 <     *    existing workers are busy, there are either no running
888 <     *    workers or the deficit is at least twice the surplus.
897 >     * @param ec the event count waited on by caller (to abort
898 >     * attempt if count has since changed).
899       */
900 <    private void helpMaintainParallelism() {
901 <        // uglified to work better when not compiled
902 <        int pc, wc, rc, tc, rs; long h;
903 <        if ((h = eventWaiters) != 0L) {
904 <            if ((int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
905 <                releaseEventWaiters(false); // avoid useless call
906 <        }
907 <        else if ((pc = parallelism) >
908 <                 (rc = ((wc = workerCounts) & RUNNING_COUNT_MASK))) {
909 <            if (spareWaiters != 0)
910 <                tryResumeSpare();
911 <            else if ((rs = runState) < TERMINATING &&
912 <                     ((tc = wc >>> TOTAL_COUNT_SHIFT) < pc ||
913 <                      (tc == (rs & ACTIVE_COUNT_MASK) && // all busy
914 <                       (rc == 0 ||                       // must add
915 <                        rc < pc - ((tc - pc) << 1)) &&   // within slack
916 <                       tc < MAX_WORKERS && runState == rs)) && // recheck busy
917 <                     workerCounts == wc &&
918 <                     UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
919 <                                              wc + (ONE_RUNNING|ONE_TOTAL)))
920 <                addWorker();
900 >    private void tryShutdownUnusedWorker(int ec) {
901 >        if (runState == 0 && eventCount == ec) { // only trigger if all idle
902 >            ForkJoinWorkerThread[] ws = workers;
903 >            int n = ws.length;
904 >            ForkJoinWorkerThread w = null;
905 >            boolean shutdown = false;
906 >            int sw;
907 >            long h;
908 >            if ((sw = spareWaiters) != 0) { // prefer killing spares
909 >                int id = (sw & SPARE_ID_MASK) - 1;
910 >                if (id >= 0 && id < n && (w = ws[id]) != null &&
911 >                    UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
912 >                                             sw, w.nextSpare))
913 >                    shutdown = true;
914 >            }
915 >            else if ((h = eventWaiters) != 0L) {
916 >                long nh;
917 >                int id = ((int)(h & WAITER_ID_MASK)) - 1;
918 >                if (id >= 0 && id < n && (w = ws[id]) != null &&
919 >                    (nh = w.nextWaiter) != 0L && // keep at least one worker
920 >                    UNSAFE.compareAndSwapLong(this, eventWaitersOffset, h, nh))
921 >                    shutdown = true;
922 >            }
923 >            if (w != null && shutdown) {
924 >                w.shutdown();
925 >                LockSupport.unpark(w);
926 >            }
927          }
928 +        releaseEventWaiters(); // in case of interference
929      }
930  
931      /**
932       * Callback from workers invoked upon each top-level action (i.e.,
933 <     * stealing a task or taking a submission and running
934 <     * it). Performs one or more of the following:
933 >     * stealing a task or taking a submission and running it).
934 >     * Performs one or more of the following:
935       *
936 <     * 1. If the worker cannot find work (misses > 0), updates its
937 <     *    active status to inactive and updates activeCount unless
938 <     *    this is the first miss and there is contention, in which
939 <     *    case it may try again (either in this or a subsequent
940 <     *    call).
941 <     *
942 <     * 2. If there are at least 2 misses, awaits the next task event
943 <     *    via eventSync
944 <     *
945 <     * 3. If there are too many running threads, suspends this worker
946 <     *    (first forcing inactivation if necessary).  If it is not
947 <     *    needed, it may be killed while suspended via
948 <     *    tryTrimSpare. Otherwise, upon resume it rechecks to make
949 <     *    sure that it is still needed.
950 <     *
951 <     * 4. Helps release and/or reactivate other workers via
952 <     *    helpMaintainParallelism
936 >     * 1. If the worker is active and either did not run a task
937 >     *    or there are too many workers, try to set its active status
938 >     *    to inactive and update activeCount. On contention, we may
939 >     *    try again in this or a subsequent call.
940 >     *
941 >     * 2. If not enough total workers, help create some.
942 >     *
943 >     * 3. If there are too many running workers, suspend this worker
944 >     *    (first forcing inactive if necessary).  If it is not needed,
945 >     *    it may be shutdown while suspended (via
946 >     *    tryShutdownUnusedWorker).  Otherwise, upon resume it
947 >     *    rechecks running thread count and need for event sync.
948 >     *
949 >     * 4. If worker did not run a task, await the next task event via
950 >     *    eventSync if necessary (first forcing inactivation), upon
951 >     *    which the worker may be shutdown via
952 >     *    tryShutdownUnusedWorker.  Otherwise, help release any
953 >     *    existing event waiters that are now releasable,
954       *
955       * @param w the worker
956 <     * @param misses the number of scans by caller failing to find work
939 <     * (saturating at 2 just to avoid wraparound)
956 >     * @param ran true if worker ran a task since last call to this method
957       */
958 <    final void preStep(ForkJoinWorkerThread w, int misses) {
958 >    final void preStep(ForkJoinWorkerThread w, boolean ran) {
959 >        int wec = w.lastEventCount;
960          boolean active = w.active;
961 +        boolean inactivate = false;
962          int pc = parallelism;
963 <        for (;;) {
963 >        int rs;
964 >        while (w.runState == 0 && (rs = runState) < TERMINATING) {
965 >            if ((inactivate || (active && (rs & ACTIVE_COUNT_MASK) >= pc)) &&
966 >                UNSAFE.compareAndSwapInt(this, runStateOffset, rs, rs - 1))
967 >                inactivate = active = w.active = false;
968              int wc = workerCounts;
969 <            int rc = wc & RUNNING_COUNT_MASK;
970 <            if (active && (misses > 0 || rc > pc)) {
971 <                int rs;                      // try inactivate
949 <                if (UNSAFE.compareAndSwapInt(this, runStateOffset,
950 <                                             rs = runState, rs - ONE_ACTIVE))
951 <                    active = w.active = false;
952 <                else if (misses > 1 || rc > pc ||
953 <                         (rs & ACTIVE_COUNT_MASK) >= pc)
954 <                    continue;                // force inactivate
955 <            }
956 <            if (misses > 1) {
957 <                misses = 0;                  // don't re-sync
958 <                eventSync(w);                // continue loop to recheck rc
959 <            }
960 <            else if (rc > pc) {
961 <                if (workerCounts == wc &&   // try to suspend as spare
969 >            if ((wc & RUNNING_COUNT_MASK) > pc) {
970 >                if (!(inactivate |= active) && // must inactivate to suspend
971 >                    workerCounts == wc &&      // try to suspend as spare
972                      UNSAFE.compareAndSwapInt(this, workerCountsOffset,
973 <                                             wc, wc - ONE_RUNNING) &&
974 <                    !w.suspendAsSpare())    // false if killed
973 >                                             wc, wc - ONE_RUNNING))
974 >                    w.suspendAsSpare();
975 >            }
976 >            else if ((wc >>> TOTAL_COUNT_SHIFT) < pc)
977 >                helpMaintainParallelism();     // not enough workers
978 >            else if (!ran) {
979 >                long h = eventWaiters;
980 >                int ec = eventCount;
981 >                if (h != 0L && (int)(h >>> EVENT_COUNT_SHIFT) != ec)
982 >                    releaseEventWaiters();     // release others before waiting
983 >                else if (ec != wec) {
984 >                    w.lastEventCount = ec;     // no need to wait
985                      break;
986 +                }
987 +                else if (!(inactivate |= active))
988 +                    eventSync(w, wec);         // must inactivate before sync
989              }
990 <            else {
968 <                if (rc < pc || eventWaiters != 0L)
969 <                    helpMaintainParallelism();
990 >            else
991                  break;
971            }
992          }
993      }
994  
995      /**
996       * Helps and/or blocks awaiting join of the given task.
997 <     * Alternates between helpJoinTask() and helpMaintainParallelism()
978 <     * as many times as there is a deficit in running count (or longer
979 <     * if running count would become zero), then blocks if task still
980 <     * not done.
997 >     * See above for explanation.
998       *
999       * @param joinMe the task to join
1000 +     * @param worker the current worker thread
1001       */
1002      final void awaitJoin(ForkJoinTask<?> joinMe, ForkJoinWorkerThread worker) {
1003 <        int threshold = parallelism;         // descend blocking thresholds
1003 >        int retries = 2 + (parallelism >> 2); // #helpJoins before blocking
1004          while (joinMe.status >= 0) {
1005 <            boolean block; int wc;
1005 >            int wc;
1006              worker.helpJoinTask(joinMe);
1007              if (joinMe.status < 0)
1008                  break;
1009 <            if (((wc = workerCounts) & RUNNING_COUNT_MASK) <= threshold) {
1010 <                if (threshold > 0)
1011 <                    --threshold;
1012 <                else
1013 <                    advanceEventCount(); // force release
1014 <                block = false;
1015 <            }
1016 <            else
1017 <                block = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1018 <                                                 wc, wc - ONE_RUNNING);
1019 <            helpMaintainParallelism();
1020 <            if (block) {
1021 <                int c;
1022 <                joinMe.internalAwaitDone();
1009 >            else if (retries > 0)
1010 >                --retries;
1011 >            else if (((wc = workerCounts) & RUNNING_COUNT_MASK) != 0 &&
1012 >                     UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1013 >                                              wc, wc - ONE_RUNNING)) {
1014 >                int stat, c; long h;
1015 >                while ((stat = joinMe.status) >= 0 &&
1016 >                       (h = eventWaiters) != 0L && // help release others
1017 >                       (int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
1018 >                    releaseEventWaiters();
1019 >                if (stat >= 0 &&
1020 >                    ((workerCounts & RUNNING_COUNT_MASK) == 0 ||
1021 >                     (stat =
1022 >                      joinMe.internalAwaitDone(JOIN_TIMEOUT_MILLIS)) >= 0))
1023 >                    helpMaintainParallelism(); // timeout or no running workers
1024                  do {} while (!UNSAFE.compareAndSwapInt
1025                               (this, workerCountsOffset,
1026                                c = workerCounts, c + ONE_RUNNING));
1027 <                break;
1027 >                if (stat < 0)
1028 >                    break;   // else restart
1029              }
1030          }
1031      }
1032  
1033      /**
1034 <     * Same idea as awaitJoin, but no helping
1034 >     * Same idea as awaitJoin, but no helping, retries, or timeouts.
1035       */
1036      final void awaitBlocker(ManagedBlocker blocker)
1037          throws InterruptedException {
1018        int threshold = parallelism;
1038          while (!blocker.isReleasable()) {
1039 <            boolean block; int wc;
1040 <            if (((wc = workerCounts) & RUNNING_COUNT_MASK) <= threshold) {
1041 <                if (threshold > 0)
1042 <                    --threshold;
1024 <                else
1025 <                    advanceEventCount();
1026 <                block = false;
1027 <            }
1028 <            else
1029 <                block = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1030 <                                                 wc, wc - ONE_RUNNING);
1031 <            helpMaintainParallelism();
1032 <            if (block) {
1039 >            int wc = workerCounts;
1040 >            if ((wc & RUNNING_COUNT_MASK) != 0 &&
1041 >                UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1042 >                                         wc, wc - ONE_RUNNING)) {
1043                  try {
1044 <                    do {} while (!blocker.isReleasable() && !blocker.block());
1044 >                    while (!blocker.isReleasable()) {
1045 >                        long h = eventWaiters;
1046 >                        if (h != 0L &&
1047 >                            (int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
1048 >                            releaseEventWaiters();
1049 >                        else if ((workerCounts & RUNNING_COUNT_MASK) == 0 &&
1050 >                                 runState < TERMINATING)
1051 >                            helpMaintainParallelism();
1052 >                        else if (blocker.block())
1053 >                            break;
1054 >                    }
1055                  } finally {
1056                      int c;
1057                      do {} while (!UNSAFE.compareAndSwapInt
# Line 1073 | Line 1093 | public class ForkJoinPool extends Abstra
1093       * Actions on transition to TERMINATING
1094       *
1095       * Runs up to four passes through workers: (0) shutting down each
1096 <     * quietly (without waking up if parked) to quickly spread
1097 <     * notifications without unnecessary bouncing around event queues
1098 <     * etc (1) wake up and help cancel tasks (2) interrupt (3) mop up
1099 <     * races with interrupted workers
1096 >     * (without waking up if parked) to quickly spread notifications
1097 >     * without unnecessary bouncing around event queues etc (1) wake
1098 >     * up and help cancel tasks (2) interrupt (3) mop up races with
1099 >     * interrupted workers
1100       */
1101      private void startTerminating() {
1102          cancelSubmissions();
1103          for (int passes = 0; passes < 4 && workerCounts != 0; ++passes) {
1104 <            advanceEventCount();
1104 >            int c; // advance event count
1105 >            UNSAFE.compareAndSwapInt(this, eventCountOffset,
1106 >                                     c = eventCount, c+1);
1107              eventWaiters = 0L; // clobber lists
1108              spareWaiters = 0;
1109 <            ForkJoinWorkerThread[] ws = workers;
1088 <            int n = ws.length;
1089 <            for (int i = 0; i < n; ++i) {
1090 <                ForkJoinWorkerThread w = ws[i];
1109 >            for (ForkJoinWorkerThread w : workers) {
1110                  if (w != null) {
1111 <                    w.shutdown(true);
1111 >                    w.shutdown();
1112                      if (passes > 0 && !w.isTerminated()) {
1113                          w.cancelTasks();
1114                          LockSupport.unpark(w);
# Line 1151 | Line 1170 | public class ForkJoinPool extends Abstra
1170       */
1171      final int idlePerActive() {
1172          int pc = parallelism; // use parallelism, not rc
1173 <        int ac = runState;    // no mask -- artifically boosts during shutdown
1173 >        int ac = runState;    // no mask -- artificially boosts during shutdown
1174          // Use exact results for small values, saturate past 4
1175          return pc <= ac? 0 : pc >>> 1 <= ac? 1 : pc >>> 2 <= ac? 3 : pc >>> 3;
1176      }
# Line 1237 | Line 1256 | public class ForkJoinPool extends Abstra
1256          this.workerLock = new ReentrantLock();
1257          this.termination = new Phaser(1);
1258          this.poolNumber = poolNumberGenerator.incrementAndGet();
1240        this.trimTime = System.nanoTime();
1259      }
1260  
1261      /**
# Line 1245 | Line 1263 | public class ForkJoinPool extends Abstra
1263       * @param pc the initial parallelism level
1264       */
1265      private static int initialArraySizeFor(int pc) {
1266 <        // See Hackers Delight, sec 3.2. We know MAX_WORKERS < (1 >>> 16)
1266 >        // If possible, initially allocate enough space for one spare
1267          int size = pc < MAX_WORKERS ? pc + 1 : MAX_WORKERS;
1268 +        // See Hackers Delight, sec 3.2. We know MAX_WORKERS < (1 >>> 16)
1269          size |= size >>> 1;
1270          size |= size >>> 2;
1271          size |= size >>> 4;
# Line 1265 | Line 1284 | public class ForkJoinPool extends Abstra
1284          if (runState >= SHUTDOWN)
1285              throw new RejectedExecutionException();
1286          submissionQueue.offer(task);
1287 <        advanceEventCount();
1288 <        helpMaintainParallelism();         // start or wake up workers
1287 >        int c; // try to increment event count -- CAS failure OK
1288 >        UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
1289 >        helpMaintainParallelism(); // create, start, or resume some workers
1290      }
1291  
1292      /**
1293       * Performs the given task, returning its result upon completion.
1274     * If the caller is already engaged in a fork/join computation in
1275     * the current pool, this method is equivalent in effect to
1276     * {@link ForkJoinTask#invoke}.
1294       *
1295       * @param task the task
1296       * @return the task's result
# Line 1288 | Line 1305 | public class ForkJoinPool extends Abstra
1305  
1306      /**
1307       * Arranges for (asynchronous) execution of the given task.
1291     * If the caller is already engaged in a fork/join computation in
1292     * the current pool, this method is equivalent in effect to
1293     * {@link ForkJoinTask#fork}.
1308       *
1309       * @param task the task
1310       * @throws NullPointerException if the task is null
# Line 1319 | Line 1333 | public class ForkJoinPool extends Abstra
1333  
1334      /**
1335       * Submits a ForkJoinTask for execution.
1322     * If the caller is already engaged in a fork/join computation in
1323     * the current pool, this method is equivalent in effect to
1324     * {@link ForkJoinTask#fork}.
1336       *
1337       * @param task the task to submit
1338       * @return the task
# Line 1512 | Line 1523 | public class ForkJoinPool extends Abstra
1523       */
1524      public long getQueuedTaskCount() {
1525          long count = 0;
1526 <        ForkJoinWorkerThread[] ws = workers;
1516 <        int n = ws.length;
1517 <        for (int i = 0; i < n; ++i) {
1518 <            ForkJoinWorkerThread w = ws[i];
1526 >        for (ForkJoinWorkerThread w : workers)
1527              if (w != null)
1528                  count += w.getQueueSize();
1521        }
1529          return count;
1530      }
1531  
# Line 1573 | Line 1580 | public class ForkJoinPool extends Abstra
1580       */
1581      protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
1582          int count = submissionQueue.drainTo(c);
1583 <        ForkJoinWorkerThread[] ws = workers;
1577 <        int n = ws.length;
1578 <        for (int i = 0; i < n; ++i) {
1579 <            ForkJoinWorkerThread w = ws[i];
1583 >        for (ForkJoinWorkerThread w : workers)
1584              if (w != null)
1585                  count += w.drainTasksTo(c);
1582        }
1586          return count;
1587      }
1588  
# Line 1706 | Line 1709 | public class ForkJoinPool extends Abstra
1709          throws InterruptedException {
1710          try {
1711              return termination.awaitAdvanceInterruptibly(0, timeout, unit) > 0;
1712 <        } catch(TimeoutException ex) {
1712 >        } catch (TimeoutException ex) {
1713              return false;
1714          }
1715      }
# Line 1753 | Line 1756 | public class ForkJoinPool extends Abstra
1756       *   QueueTaker(BlockingQueue<E> q) { this.queue = q; }
1757       *   public boolean block() throws InterruptedException {
1758       *     if (item == null)
1759 <     *       item = queue.take
1759 >     *       item = queue.take();
1760       *     return true;
1761       *   }
1762       *   public boolean isReleasable() {
1763 <     *     return item != null || (item = queue.poll) != null;
1763 >     *     return item != null || (item = queue.poll()) != null;
1764       *   }
1765       *   public E getItem() { // call after pool.managedBlock completes
1766       *     return item;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines