test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java

Print this page




  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6233235 6268386
  27  * @summary Test allowsCoreThreadTimeOut
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.concurrent.*;
  32 
  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 
  43     static int countExecutorThreads() {











  44         Thread[] threads = new Thread[Thread.activeCount()+100];
  45         Thread.enumerate(threads);
  46         int count = 0;
  47         for (Thread t : threads)
  48             if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))


  49                 count++;
  50         return count;
  51     }
  52 
  53     public static void main(String[] args) throws Throwable {




  54         final int threadCount = 10;

  55         BlockingQueue<Runnable> q
  56             = new ArrayBlockingQueue<Runnable>(2*threadCount);
  57         ThreadPoolExecutor tpe
  58             = new ThreadPoolExecutor(threadCount, threadCount,
  59                                      30, TimeUnit.MILLISECONDS,
  60                                      q);
  61         equal(tpe.getCorePoolSize(), threadCount);
  62         check(! tpe.allowsCoreThreadTimeOut());
  63         tpe.allowCoreThreadTimeOut(true);
  64         check(tpe.allowsCoreThreadTimeOut());
  65         equal(countExecutorThreads(), 0);

  66         for (int i = 0; i < threadCount; i++)
  67             tpe.submit(new Runnable() { public void run() {}});
  68         equal(countExecutorThreads(), threadCount);
  69         Thread.sleep(500);



  70         equal(countExecutorThreads(), 0);
  71         tpe.shutdown();
  72         check(tpe.allowsCoreThreadTimeOut());

  73 
  74         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  75         if (failed > 0) throw new Exception("Some tests failed");
  76     }

















  77 }


  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6233235 6268386
  27  * @summary Test allowsCoreThreadTimeOut
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.concurrent.*;
  32 
  33 public class CoreThreadTimeOut {








  34 
  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() {
  47         Thread[] threads = new Thread[Thread.activeCount()+100];
  48         Thread.enumerate(threads);
  49         int count = 0;
  50         for (Thread t : threads)
  51             if (t != null &&
  52                 t.getName().matches
  53                 ("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+"))
  54                 count++;
  55         return count;
  56     }
  57 
  58     long millisElapsedSince(long t0) {
  59         return (System.nanoTime() - t0) / (1000L * 1000L);
  60     }
  61 
  62     void test(String[] args) throws Throwable {
  63         final int threadCount = 10;
  64         final int timeoutMillis = 30;
  65         BlockingQueue<Runnable> q
  66             = new ArrayBlockingQueue<Runnable>(2*threadCount);
  67         ThreadPoolExecutor tpe
  68             = new ThreadPoolExecutor(threadCount, threadCount,
  69                                      timeoutMillis, TimeUnit.MILLISECONDS,
  70                                      q, new IdentifiableThreadFactory());
  71         equal(tpe.getCorePoolSize(), threadCount);
  72         check(! tpe.allowsCoreThreadTimeOut());
  73         tpe.allowCoreThreadTimeOut(true);
  74         check(tpe.allowsCoreThreadTimeOut());
  75         equal(countExecutorThreads(), 0);
  76         long t0 = System.nanoTime();
  77         for (int i = 0; i < threadCount; i++)
  78             tpe.submit(new Runnable() { public void run() {}});
  79         int count = countExecutorThreads();
  80         if (millisElapsedSince(t0) < timeoutMillis)
  81             equal(count, threadCount);
  82         while (countExecutorThreads() > 0 &&
  83                millisElapsedSince(t0) < 10 * 1000);
  84         equal(countExecutorThreads(), 0);
  85         tpe.shutdown();
  86         check(tpe.allowsCoreThreadTimeOut());
  87         check(tpe.awaitTermination(10, TimeUnit.SECONDS));
  88 
  89         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  90         if (failed > 0) throw new Exception("Some tests failed");
  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");}
 109 }