test/java/util/concurrent/ThreadPoolExecutor/Custom.java

Print this page




  26  * @bug 6277663
  27  * @summary Test TPE extensibility framework
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.concurrent.*;
  32 import java.util.concurrent.atomic.*;
  33 
  34 public class Custom {
  35     static volatile int passed = 0, failed = 0;
  36     static void pass() { passed++; }
  37     static void fail() { failed++; Thread.dumpStack(); }
  38     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
  39     static void check(boolean cond) { if (cond) pass(); else fail(); }
  40     static void equal(Object x, Object y) {
  41         if (x == null ? y == null : x.equals(y)) pass();
  42         else {System.out.println(x + " not equal to " + y); fail(); }}
  43 
  44 
  45     private static class CustomTask<V> extends FutureTask<V> {
  46         public final static AtomicInteger births = new AtomicInteger(0);
  47         CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); }
  48         CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); }
  49     }
  50 
  51     private static class CustomTPE extends ThreadPoolExecutor {
  52         CustomTPE() {
  53             super(threadCount, threadCount,
  54                   30, TimeUnit.MILLISECONDS,
  55                   new ArrayBlockingQueue<Runnable>(2*threadCount));
  56         }
  57         protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
  58             return new CustomTask<V>(c);
  59         }
  60         protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
  61             return new CustomTask<V>(r, v);
  62         }
  63     }
  64 
  65     private static class CustomSTPE extends ScheduledThreadPoolExecutor {
  66         public final static AtomicInteger decorations = new AtomicInteger(0);
  67         CustomSTPE() {
  68             super(threadCount);
  69         }
  70         protected <V> RunnableScheduledFuture<V> decorateTask(
  71             Runnable r, RunnableScheduledFuture<V> task) {
  72             decorations.getAndIncrement();
  73             return task;
  74         }
  75         protected <V> RunnableScheduledFuture<V> decorateTask(
  76             Callable<V> c, RunnableScheduledFuture<V> task) {
  77             decorations.getAndIncrement();
  78             return task;
  79         }
  80     }
  81 
  82     static int countExecutorThreads() {
  83         Thread[] threads = new Thread[Thread.activeCount()+100];
  84         Thread.enumerate(threads);
  85         int count = 0;
  86         for (Thread t : threads)
  87             if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
  88                 count++;
  89         return count;
  90     }
  91 
  92     private final static int threadCount = 10;
  93 
  94     public static void main(String[] args) throws Throwable {
  95         CustomTPE tpe = new CustomTPE();
  96         equal(tpe.getCorePoolSize(), threadCount);
  97         equal(countExecutorThreads(), 0);
  98         for (int i = 0; i < threadCount; i++)
  99             tpe.submit(new Runnable() { public void run() {}});
 100         equal(countExecutorThreads(), threadCount);
 101         equal(CustomTask.births.get(), threadCount);
 102         tpe.shutdown();
 103         tpe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
 104         Thread.sleep(10);
 105         equal(countExecutorThreads(), 0);
 106 
 107         CustomSTPE stpe = new CustomSTPE();
 108         for (int i = 0; i < threadCount; i++)
 109             stpe.submit(new Runnable() { public void run() {}});
 110         equal(CustomSTPE.decorations.get(), threadCount);
 111         equal(countExecutorThreads(), threadCount);
 112         stpe.shutdown();


  26  * @bug 6277663
  27  * @summary Test TPE extensibility framework
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.concurrent.*;
  32 import java.util.concurrent.atomic.*;
  33 
  34 public class Custom {
  35     static volatile int passed = 0, failed = 0;
  36     static void pass() { passed++; }
  37     static void fail() { failed++; Thread.dumpStack(); }
  38     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
  39     static void check(boolean cond) { if (cond) pass(); else fail(); }
  40     static void equal(Object x, Object y) {
  41         if (x == null ? y == null : x.equals(y)) pass();
  42         else {System.out.println(x + " not equal to " + y); fail(); }}
  43 
  44 
  45     private static class CustomTask<V> extends FutureTask<V> {
  46         public static final AtomicInteger births = new AtomicInteger(0);
  47         CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); }
  48         CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); }
  49     }
  50 
  51     private static class CustomTPE extends ThreadPoolExecutor {
  52         CustomTPE() {
  53             super(threadCount, threadCount,
  54                   30, TimeUnit.MILLISECONDS,
  55                   new ArrayBlockingQueue<Runnable>(2*threadCount));
  56         }
  57         protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
  58             return new CustomTask<V>(c);
  59         }
  60         protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
  61             return new CustomTask<V>(r, v);
  62         }
  63     }
  64 
  65     private static class CustomSTPE extends ScheduledThreadPoolExecutor {
  66         public static final AtomicInteger decorations = new AtomicInteger(0);
  67         CustomSTPE() {
  68             super(threadCount);
  69         }
  70         protected <V> RunnableScheduledFuture<V> decorateTask(
  71             Runnable r, RunnableScheduledFuture<V> task) {
  72             decorations.getAndIncrement();
  73             return task;
  74         }
  75         protected <V> RunnableScheduledFuture<V> decorateTask(
  76             Callable<V> c, RunnableScheduledFuture<V> task) {
  77             decorations.getAndIncrement();
  78             return task;
  79         }
  80     }
  81 
  82     static int countExecutorThreads() {
  83         Thread[] threads = new Thread[Thread.activeCount()+100];
  84         Thread.enumerate(threads);
  85         int count = 0;
  86         for (Thread t : threads)
  87             if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
  88                 count++;
  89         return count;
  90     }
  91 
  92     private static final int threadCount = 10;
  93 
  94     public static void main(String[] args) throws Throwable {
  95         CustomTPE tpe = new CustomTPE();
  96         equal(tpe.getCorePoolSize(), threadCount);
  97         equal(countExecutorThreads(), 0);
  98         for (int i = 0; i < threadCount; i++)
  99             tpe.submit(new Runnable() { public void run() {}});
 100         equal(countExecutorThreads(), threadCount);
 101         equal(CustomTask.births.get(), threadCount);
 102         tpe.shutdown();
 103         tpe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
 104         Thread.sleep(10);
 105         equal(countExecutorThreads(), 0);
 106 
 107         CustomSTPE stpe = new CustomSTPE();
 108         for (int i = 0; i < threadCount; i++)
 109             stpe.submit(new Runnable() { public void run() {}});
 110         equal(CustomSTPE.decorations.get(), threadCount);
 111         equal(countExecutorThreads(), threadCount);
 112         stpe.shutdown();