jdk/test/java/util/concurrent/ThreadPoolExecutor/Custom.java
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
equal deleted inserted replaced
36232:7a020ad42ac0 36233:f85ed703cf7e
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 6277663
    26  * @bug 6277663
    27  * @summary Test TPE extensibility framework
    27  * @summary Test TPE extensibility framework
       
    28  * @library /lib/testlibrary/
    28  * @author Martin Buchholz
    29  * @author Martin Buchholz
    29  */
    30  */
       
    31 
       
    32 import static java.util.concurrent.TimeUnit.MILLISECONDS;
    30 
    33 
    31 import java.util.concurrent.ArrayBlockingQueue;
    34 import java.util.concurrent.ArrayBlockingQueue;
    32 import java.util.concurrent.Callable;
    35 import java.util.concurrent.Callable;
    33 import java.util.concurrent.FutureTask;
    36 import java.util.concurrent.FutureTask;
    34 import java.util.concurrent.RunnableFuture;
    37 import java.util.concurrent.RunnableFuture;
    35 import java.util.concurrent.RunnableScheduledFuture;
    38 import java.util.concurrent.RunnableScheduledFuture;
    36 import java.util.concurrent.ScheduledThreadPoolExecutor;
    39 import java.util.concurrent.ScheduledThreadPoolExecutor;
    37 import java.util.concurrent.ThreadPoolExecutor;
    40 import java.util.concurrent.ThreadPoolExecutor;
    38 import java.util.concurrent.TimeUnit;
    41 import java.util.concurrent.TimeUnit;
    39 import java.util.concurrent.atomic.AtomicInteger;
    42 import java.util.concurrent.atomic.AtomicInteger;
       
    43 import java.util.function.BooleanSupplier;
       
    44 import jdk.testlibrary.Utils;
    40 
    45 
    41 public class Custom {
    46 public class Custom {
       
    47     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
    42     static volatile int passed = 0, failed = 0;
    48     static volatile int passed = 0, failed = 0;
    43     static void pass() { passed++; }
    49     static void pass() { passed++; }
    44     static void fail() { failed++; Thread.dumpStack(); }
    50     static void fail() { failed++; Thread.dumpStack(); }
    45     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
    51     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
    46     static void check(boolean cond) { if (cond) pass(); else fail(); }
    52     static void check(boolean cond) { if (cond) pass(); else fail(); }
    95         return count;
   101         return count;
    96     }
   102     }
    97 
   103 
    98     private static final int threadCount = 10;
   104     private static final int threadCount = 10;
    99 
   105 
       
   106     static long millisElapsedSince(long startTime) {
       
   107         return (System.nanoTime() - startTime) / (1000L * 1000L);
       
   108     }
       
   109 
       
   110     static void spinWaitUntil(BooleanSupplier predicate, long timeoutMillis) {
       
   111         long startTime = -1L;
       
   112         while (!predicate.getAsBoolean()) {
       
   113             if (startTime == -1L)
       
   114                 startTime = System.nanoTime();
       
   115             else if (millisElapsedSince(startTime) > timeoutMillis)
       
   116                 throw new AssertionError(
       
   117                     String.format("timed out after %s ms", timeoutMillis));
       
   118             Thread.yield();
       
   119         }
       
   120     }
       
   121 
   100     public static void main(String[] args) throws Throwable {
   122     public static void main(String[] args) throws Throwable {
   101         CustomTPE tpe = new CustomTPE();
   123         CustomTPE tpe = new CustomTPE();
   102         equal(tpe.getCorePoolSize(), threadCount);
   124         equal(tpe.getCorePoolSize(), threadCount);
   103         equal(countExecutorThreads(), 0);
   125         equal(countExecutorThreads(), 0);
   104         for (int i = 0; i < threadCount; i++)
   126         for (int i = 0; i < threadCount; i++)
   105             tpe.submit(new Runnable() { public void run() {}});
   127             tpe.submit(new Runnable() { public void run() {}});
   106         equal(countExecutorThreads(), threadCount);
   128         equal(countExecutorThreads(), threadCount);
   107         equal(CustomTask.births.get(), threadCount);
   129         equal(CustomTask.births.get(), threadCount);
   108         tpe.shutdown();
   130         tpe.shutdown();
   109         tpe.awaitTermination(120L, TimeUnit.SECONDS);
   131         tpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS);
   110         Thread.sleep(1000);
   132         spinWaitUntil(() -> countExecutorThreads() == 0, LONG_DELAY_MS);
   111         equal(countExecutorThreads(), 0);
       
   112 
   133 
   113         CustomSTPE stpe = new CustomSTPE();
   134         CustomSTPE stpe = new CustomSTPE();
   114         for (int i = 0; i < threadCount; i++)
   135         for (int i = 0; i < threadCount; i++)
   115             stpe.submit(new Runnable() { public void run() {}});
   136             stpe.submit(new Runnable() { public void run() {}});
   116         equal(CustomSTPE.decorations.get(), threadCount);
   137         equal(CustomSTPE.decorations.get(), threadCount);
   117         equal(countExecutorThreads(), threadCount);
   138         equal(countExecutorThreads(), threadCount);
   118         stpe.shutdown();
   139         stpe.shutdown();
   119         stpe.awaitTermination(120L, TimeUnit.SECONDS);
   140         stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS);
   120         Thread.sleep(1000);
   141         spinWaitUntil(() -> countExecutorThreads() == 0, LONG_DELAY_MS);
   121         equal(countExecutorThreads(), 0);
       
   122 
   142 
   123         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
   143         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
   124         if (failed > 0) throw new Exception("Some tests failed");
   144         if (failed > 0) throw new Exception("Some tests failed");
   125     }
   145     }
   126 }
   146 }