Print this page


Split Close
Expand all
Collapse all
          --- old/test/java/util/concurrent/ThreadPoolExecutor/Custom.java
          +++ new/test/java/util/concurrent/ThreadPoolExecutor/Custom.java
↓ open down ↓ 35 lines elided ↑ open up ↑
  36   36      static void pass() { passed++; }
  37   37      static void fail() { failed++; Thread.dumpStack(); }
  38   38      static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
  39   39      static void check(boolean cond) { if (cond) pass(); else fail(); }
  40   40      static void equal(Object x, Object y) {
  41   41          if (x == null ? y == null : x.equals(y)) pass();
  42   42          else {System.out.println(x + " not equal to " + y); fail(); }}
  43   43  
  44   44  
  45   45      private static class CustomTask<V> extends FutureTask<V> {
  46      -        public final static AtomicInteger births = new AtomicInteger(0);
       46 +        public static final AtomicInteger births = new AtomicInteger(0);
  47   47          CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); }
  48   48          CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); }
  49   49      }
  50   50  
  51   51      private static class CustomTPE extends ThreadPoolExecutor {
  52   52          CustomTPE() {
  53   53              super(threadCount, threadCount,
  54   54                    30, TimeUnit.MILLISECONDS,
  55   55                    new ArrayBlockingQueue<Runnable>(2*threadCount));
  56   56          }
  57   57          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
  58   58              return new CustomTask<V>(c);
  59   59          }
  60   60          protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
  61   61              return new CustomTask<V>(r, v);
  62   62          }
  63   63      }
  64   64  
  65   65      private static class CustomSTPE extends ScheduledThreadPoolExecutor {
  66      -        public final static AtomicInteger decorations = new AtomicInteger(0);
       66 +        public static final AtomicInteger decorations = new AtomicInteger(0);
  67   67          CustomSTPE() {
  68   68              super(threadCount);
  69   69          }
  70   70          protected <V> RunnableScheduledFuture<V> decorateTask(
  71   71              Runnable r, RunnableScheduledFuture<V> task) {
  72   72              decorations.getAndIncrement();
  73   73              return task;
  74   74          }
  75   75          protected <V> RunnableScheduledFuture<V> decorateTask(
  76   76              Callable<V> c, RunnableScheduledFuture<V> task) {
↓ open down ↓ 5 lines elided ↑ open up ↑
  82   82      static int countExecutorThreads() {
  83   83          Thread[] threads = new Thread[Thread.activeCount()+100];
  84   84          Thread.enumerate(threads);
  85   85          int count = 0;
  86   86          for (Thread t : threads)
  87   87              if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
  88   88                  count++;
  89   89          return count;
  90   90      }
  91   91  
  92      -    private final static int threadCount = 10;
       92 +    private static final int threadCount = 10;
  93   93  
  94   94      public static void main(String[] args) throws Throwable {
  95   95          CustomTPE tpe = new CustomTPE();
  96   96          equal(tpe.getCorePoolSize(), threadCount);
  97   97          equal(countExecutorThreads(), 0);
  98   98          for (int i = 0; i < threadCount; i++)
  99   99              tpe.submit(new Runnable() { public void run() {}});
 100  100          equal(countExecutorThreads(), threadCount);
 101  101          equal(CustomTask.births.get(), threadCount);
 102  102          tpe.shutdown();
↓ open down ↓ 18 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX