--- jsr166/src/jsr166y/ForkJoinPool.java 2011/03/23 11:27:43 1.99 +++ jsr166/src/jsr166y/ForkJoinPool.java 2011/04/14 01:17:58 1.102 @@ -762,18 +762,19 @@ public class ForkJoinPool extends Abstra /** * Tries to enqueue worker w in wait queue and await change in - * worker's eventCount. If the pool is quiescent, possibly - * terminates worker upon exit. Otherwise, before blocking, - * rescans queues to avoid missed signals. Upon finding work, - * releases at least one worker (which may be the current - * worker). Rescans restart upon detected staleness or failure to - * release due to contention. Note the unusual conventions about - * Thread.interrupt here and elsewhere: Because interrupts are - * used solely to alert threads to check termination, which is - * checked here anyway, we clear status (using Thread.interrupted) - * before any call to park, so that park does not immediately - * return due to status being set via some other unrelated call to - * interrupt in user code. + * worker's eventCount. If the pool is quiescent and there is + * more than one worker, possibly terminates worker upon exit. + * Otherwise, before blocking, rescans queues to avoid missed + * signals. Upon finding work, releases at least one worker + * (which may be the current worker). Rescans restart upon + * detected staleness or failure to release due to + * contention. Note the unusual conventions about Thread.interrupt + * here and elsewhere: Because interrupts are used solely to alert + * threads to check termination, which is checked here anyway, we + * clear status (using Thread.interrupted) before any call to + * park, so that park does not immediately return due to status + * being set via some other unrelated call to interrupt in user + * code. * * @param w the calling worker * @param c the ctl value on entry @@ -794,7 +795,8 @@ public class ForkJoinPool extends Abstra else if (w.eventCount != v) return true; // update next time } - if (parallelism + (int)(nc >> AC_SHIFT) == 0 && + if ((!shutdown || !tryTerminate(false)) && + (int)c != 0 && parallelism + (int)(nc >> AC_SHIFT) == 0 && blockedCount == 0 && quiescerCount == 0) idleAwaitWork(w, nc, c, v); // quiescent for (boolean rescanned = false;;) { @@ -864,7 +866,7 @@ public class ForkJoinPool extends Abstra w.parked = false; if (w.eventCount != v) break; - else if (System.nanoTime() - startTime < + else if (System.nanoTime() - startTime < SHRINK_RATE - (SHRINK_RATE / 10)) // timing slop Thread.interrupted(); // spurious wakeup else if (UNSAFE.compareAndSwapLong(this, ctlOffset, @@ -994,8 +996,8 @@ public class ForkJoinPool extends Abstra do {} while (!UNSAFE.compareAndSwapLong(this, ctlOffset, // no mask c = ctl, c + AC_UNIT)); int b; - do {} while(!UNSAFE.compareAndSwapInt(this, blockedCountOffset, - b = blockedCount, b - 1)); + do {} while (!UNSAFE.compareAndSwapInt(this, blockedCountOffset, + b = blockedCount, b - 1)); } /** @@ -1147,7 +1149,7 @@ public class ForkJoinPool extends Abstra ws[k] = w; nextWorkerIndex = k + 1; int m = g & SMASK; - g = k > m? ((m << 1) + 1) & SMASK : g + (SG_UNIT<<1); + g = (k > m) ? ((m << 1) + 1) & SMASK : g + (SG_UNIT<<1); } } finally { scanGuard = g; @@ -1330,8 +1332,8 @@ public class ForkJoinPool extends Abstra */ final void addQuiescerCount(int delta) { int c; - do {} while(!UNSAFE.compareAndSwapInt(this, quiescerCountOffset, - c = quiescerCount, c + delta)); + do {} while (!UNSAFE.compareAndSwapInt(this, quiescerCountOffset, + c = quiescerCount, c + delta)); } /** @@ -1356,12 +1358,12 @@ public class ForkJoinPool extends Abstra final int idlePerActive() { // Approximate at powers of two for small values, saturate past 4 int p = parallelism; - int a = p + (int)(ctl >> AC_SHIFT); - return (a > (p >>>= 1) ? 0 : - a > (p >>>= 1) ? 1 : - a > (p >>>= 1) ? 2 : - a > (p >>>= 1) ? 4 : - 8); + int a = p + (int)(ctl >> AC_SHIFT); + return (a > (p >>>= 1) ? 0 : + a > (p >>>= 1) ? 1 : + a > (p >>>= 1) ? 2 : + a > (p >>>= 1) ? 4 : + 8); } // Exported methods @@ -1684,7 +1686,7 @@ public class ForkJoinPool extends Abstra */ public int getRunningThreadCount() { int r = parallelism + (int)(ctl >> AC_SHIFT); - return r <= 0? 0 : r; // suppress momentarily negative values + return (r <= 0) ? 0 : r; // suppress momentarily negative values } /** @@ -1696,7 +1698,7 @@ public class ForkJoinPool extends Abstra */ public int getActiveThreadCount() { int r = parallelism + (int)(ctl >> AC_SHIFT) + blockedCount; - return r <= 0? 0 : r; // suppress momentarily negative values + return (r <= 0) ? 0 : r; // suppress momentarily negative values } /** @@ -1851,9 +1853,9 @@ public class ForkJoinPool extends Abstra int ac = rc + blockedCount; String level; if ((c & STOP_BIT) != 0) - level = (tc == 0)? "Terminated" : "Terminating"; + level = (tc == 0) ? "Terminated" : "Terminating"; else - level = shutdown? "Shutting down" : "Running"; + level = shutdown ? "Shutting down" : "Running"; return super.toString() + "[" + level + ", parallelism = " + pc +