jdk/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java
changeset 43522 f9c6f543c4db
parent 42318 35806bea1e90
child 45288 58be10a068c2
--- a/jdk/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java	Fri Feb 03 13:24:59 2017 -0800
+++ b/jdk/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java	Fri Feb 03 13:24:59 2017 -0800
@@ -44,6 +44,7 @@
 import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.ThreadPoolExecutor;
+import java.util.function.Supplier;
 import jdk.testlibrary.RandomFactory;
 
 public class ConfigChanges {
@@ -124,6 +125,22 @@
         }
     }
 
+    /**
+     * Waits for condition to become true, first spin-polling, then sleep-polling.
+     */
+    static void spinAwait(Supplier<Boolean> waitingForGodot) {
+        for (int spins = 0; !waitingForGodot.get(); ) {
+            if ((spins = (spins + 1) & 3) > 0) {
+                Thread.yield();
+            } else {
+                try { Thread.sleep(4); }
+                catch (InterruptedException unexpected) {
+                    throw new AssertionError(unexpected);
+                }
+            }
+        }
+    }
+
     private static void realMain(String[] args) throws Throwable {
         if (rnd.nextBoolean())
             System.setSecurityManager(new PermissiveSecurityManger());
@@ -210,10 +227,7 @@
         pumpedUp2.await();
         pumpedUp.await();
 
-        while (tg.activeCount() != 2*n &&
-               tg.activeCount() != 2*n)
-            Thread.yield();
-        equal(2*n, tg.activeCount());
+        spinAwait(() -> tg.activeCount() == 2*n);
         equal(2*n, tpe.getMaximumPoolSize());
         equal(4*n, tpe.getLargestPoolSize());
 
@@ -232,10 +246,7 @@
         long t0 = System.nanoTime();
         tpe.setKeepAliveTime(7L, MILLISECONDS);
         equal(7L, tpe.getKeepAliveTime(MILLISECONDS));
-        while (tg.activeCount() > n &&
-               tg.activeCount() > n)
-            Thread.sleep(4);
-        equal(n, tg.activeCount());
+        spinAwait(() -> tg.activeCount() == n);
         check(System.nanoTime() - t0 >= tpe.getKeepAliveTime(NANOSECONDS));
 
         //report("idle", tpe);
@@ -243,10 +254,7 @@
         t0 = System.nanoTime();
         tpe.allowCoreThreadTimeOut(true);
         check(tpe.allowsCoreThreadTimeOut());
-        while (tg.activeCount() > 0 &&
-               tg.activeCount() > 0)
-            Thread.sleep(4);
-        equal(tg.activeCount(), 0);
+        spinAwait(() -> tg.activeCount() == 0);
 
         // The following assertion is almost always true, but may
         // exceptionally not be during a transition from core count