diff --git a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java index ec46f61291d27..5c74e653431b6 100644 --- a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java +++ b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java @@ -1278,8 +1278,7 @@ final int queueSize() { * @param internal if caller owns this queue * @throws RejectedExecutionException if array could not be resized */ - final void push(ForkJoinTask task, ForkJoinPool pool, - boolean internal) { + final void push(ForkJoinTask task, ForkJoinPool pool, boolean internal) { int s = top, b = base, m, cap, room; ForkJoinTask[] a; if ((a = array) != null && (cap = a.length) > 0 && // else disabled task != null) { @@ -1383,8 +1382,7 @@ final boolean tryUnpush(ForkJoinTask task, boolean internal) { if (a != null && (cap = a.length) > 0 && U.getReference(a, k = slotOffset((cap - 1) & s)) == task && (internal || tryLockPhase())) { - if (top == p && - U.compareAndSetReference(a, k, task, null)) { + if (top == p && U.compareAndSetReference(a, k, task, null)) { taken = true; updateTop(s); } @@ -2061,8 +2059,9 @@ private int deactivate(WorkQueue w, int phase) { ((e & SHUTDOWN) != 0L && ac == 0 && quiescent() > 0) || (qs = queues) == null || (n = qs.length) <= 0) return IDLE; // terminating - int k = Math.max(n << 2, SPIN_WAITS << 1); - for (int prechecks = k / n;;) { // reactivation threshold + + for (int prechecks = Math.min(ac, 2), // reactivation threshold + k = Math.max(n + (n << 1), SPIN_WAITS << 1);;) { WorkQueue q; int cap; ForkJoinTask[] a; long c; if (w.phase == activePhase) return activePhase; @@ -2071,7 +2070,7 @@ private int deactivate(WorkQueue w, int phase) { if ((q = qs[k & (n - 1)]) == null) Thread.onSpinWait(); else if ((a = q.array) != null && (cap = a.length) > 0 && - a[q.base & (cap - 1)] != null && --prechecks <= 0 && + a[q.base & (cap - 1)] != null && --prechecks < 0 && (int)(c = ctl) == activePhase && compareAndSetCtl(c, (sp & LMASK) | ((c + RC_UNIT) & UMASK))) return w.phase = activePhase; // reactivate diff --git a/test/jdk/java/util/concurrent/tck/ForkJoinPool20Test.java b/test/jdk/java/util/concurrent/tck/ForkJoinPool20Test.java index 41dd82300e210..9b60bbae484d8 100644 --- a/test/jdk/java/util/concurrent/tck/ForkJoinPool20Test.java +++ b/test/jdk/java/util/concurrent/tck/ForkJoinPool20Test.java @@ -528,6 +528,7 @@ public void testScheduleWithFixedDelay_overflow() throws Exception { final CountDownLatch delayedDone = new CountDownLatch(1); final CountDownLatch immediateDone = new CountDownLatch(1); final ForkJoinPool p = new ForkJoinPool(2); + p.cancelDelayedTasksOnShutdown(); try (PoolCleaner cleaner = cleaner(p)) { final Runnable delayed = () -> { delayedDone.countDown(); @@ -568,8 +569,8 @@ public void testSubmitWithTimeoutCancels() throws InterruptedException { public Boolean call() throws Exception { Thread.sleep(LONGER_DELAY_MS); return Boolean.TRUE; }}; ForkJoinTask task = p.submitWithTimeout(c, 1, NANOSECONDS, null); - Thread.sleep(timeoutMillis()); - assertTrue(task.isCancelled()); + while(!task.isCancelled()) + Thread.sleep(timeoutMillis()); } static final class SubmitWithTimeoutException extends RuntimeException {} @@ -586,7 +587,6 @@ public Item call() throws Exception { c, 1, NANOSECONDS, (ForkJoinTask t) -> t.complete(two)); - Thread.sleep(timeoutMillis()); assertEquals(task.join(), two); } @@ -602,7 +602,6 @@ public Boolean call() throws Exception { c, 1, NANOSECONDS, (ForkJoinTask t) -> t.completeExceptionally(new SubmitWithTimeoutException())); - Thread.sleep(timeoutMillis()); try { task.join(); shouldThrow();