Print this page


Split Close
Expand all
Collapse all
          --- old/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
          +++ new/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
↓ open down ↓ 23 lines elided ↑ open up ↑
  24   24  /*
  25   25   * @test
  26   26   * @bug 6233235 6268386
  27   27   * @summary Test allowsCoreThreadTimeOut
  28   28   * @author Martin Buchholz
  29   29   */
  30   30  
  31   31  import java.util.concurrent.*;
  32   32  
  33   33  public class CoreThreadTimeOut {
  34      -    static volatile int passed = 0, failed = 0;
  35      -    static void pass() { passed++; }
  36      -    static void fail() { failed++; Thread.dumpStack(); }
  37      -    static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
  38      -    static void check(boolean cond) { if (cond) pass(); else fail(); }
  39      -    static void equal(Object x, Object y) {
  40      -        if (x == null ? y == null : x.equals(y)) pass();
  41      -        else {System.out.println(x + " not equal to " + y); fail(); }}
  42   34  
  43      -    static int countExecutorThreads() {
       35 +    static class IdentifiableThreadFactory implements ThreadFactory {
       36 +        static ThreadFactory defaultThreadFactory
       37 +            = Executors.defaultThreadFactory();
       38 +
       39 +        public Thread newThread(Runnable r) {
       40 +            Thread t = defaultThreadFactory.newThread(r);
       41 +            t.setName("CoreThreadTimeOut-" + t.getName());
       42 +            return t;
       43 +        }
       44 +    }
       45 +
       46 +    int countExecutorThreads() {
  44   47          Thread[] threads = new Thread[Thread.activeCount()+100];
  45   48          Thread.enumerate(threads);
  46   49          int count = 0;
  47   50          for (Thread t : threads)
  48      -            if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
       51 +            if (t != null &&
       52 +                t.getName().matches
       53 +                ("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+"))
  49   54                  count++;
  50   55          return count;
  51   56      }
  52   57  
  53      -    public static void main(String[] args) throws Throwable {
       58 +    long millisElapsedSince(long t0) {
       59 +        return (System.nanoTime() - t0) / (1000L * 1000L);
       60 +    }
       61 +
       62 +    void test(String[] args) throws Throwable {
  54   63          final int threadCount = 10;
       64 +        final int timeoutMillis = 30;
  55   65          BlockingQueue<Runnable> q
  56   66              = new ArrayBlockingQueue<Runnable>(2*threadCount);
  57   67          ThreadPoolExecutor tpe
  58   68              = new ThreadPoolExecutor(threadCount, threadCount,
  59      -                                     30, TimeUnit.MILLISECONDS,
  60      -                                     q);
       69 +                                     timeoutMillis, TimeUnit.MILLISECONDS,
       70 +                                     q, new IdentifiableThreadFactory());
  61   71          equal(tpe.getCorePoolSize(), threadCount);
  62   72          check(! tpe.allowsCoreThreadTimeOut());
  63   73          tpe.allowCoreThreadTimeOut(true);
  64   74          check(tpe.allowsCoreThreadTimeOut());
  65   75          equal(countExecutorThreads(), 0);
       76 +        long t0 = System.nanoTime();
  66   77          for (int i = 0; i < threadCount; i++)
  67   78              tpe.submit(new Runnable() { public void run() {}});
  68      -        equal(countExecutorThreads(), threadCount);
  69      -        Thread.sleep(500);
       79 +        int count = countExecutorThreads();
       80 +        if (millisElapsedSince(t0) < timeoutMillis)
       81 +            equal(count, threadCount);
       82 +        while (countExecutorThreads() > 0 &&
       83 +               millisElapsedSince(t0) < 10 * 1000);
  70   84          equal(countExecutorThreads(), 0);
  71   85          tpe.shutdown();
  72   86          check(tpe.allowsCoreThreadTimeOut());
       87 +        check(tpe.awaitTermination(10, TimeUnit.SECONDS));
  73   88  
  74   89          System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  75   90          if (failed > 0) throw new Exception("Some tests failed");
  76   91      }
       92 +
       93 +    //--------------------- Infrastructure ---------------------------
       94 +    volatile int passed = 0, failed = 0;
       95 +    void pass() {passed++;}
       96 +    void fail() {failed++; Thread.dumpStack();}
       97 +    void fail(String msg) {System.err.println(msg); fail();}
       98 +    void unexpected(Throwable t) {failed++; t.printStackTrace();}
       99 +    void check(boolean cond) {if (cond) pass(); else fail();}
      100 +    void equal(Object x, Object y) {
      101 +        if (x == null ? y == null : x.equals(y)) pass();
      102 +        else fail(x + " not equal to " + y);}
      103 +    public static void main(String[] args) throws Throwable {
      104 +        new CoreThreadTimeOut().instanceMain(args);}
      105 +    public void instanceMain(String[] args) throws Throwable {
      106 +        try {test(args);} catch (Throwable t) {unexpected(t);}
      107 +        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
      108 +        if (failed > 0) throw new AssertionError("Some tests failed");}
  77  109  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX