test/java/util/concurrent/ThreadPoolExecutor/ShutdownNowExecuteRace.java

Print this page




  23 
  24 /*
  25  * @test
  26  * @bug 6523756
  27  * @summary Race task submission against shutdownNow
  28  * @author Martin Buchholz
  29  */
  30 
  31 // For extra chances to detect shutdownNow vs. execute races,
  32 // crank up the iterations to, say 1<<22 and
  33 // add a call to Thread.yield() before the call to t.start()
  34 // in ThreadPoolExecutor.addWorker.
  35 
  36 import java.util.*;
  37 import java.util.concurrent.*;
  38 
  39 public class ShutdownNowExecuteRace {
  40     static volatile boolean quit = false;
  41     static volatile ThreadPoolExecutor pool = null;
  42 
  43     final static Runnable sleeper = new Runnable() { public void run() {
  44         final long ONE_HOUR = 1000L * 60L * 60L;
  45         try { Thread.sleep(ONE_HOUR); }
  46         catch (InterruptedException ie) {}
  47         catch (Throwable t) { unexpected(t); }}};
  48 
  49     static void realMain(String[] args) throws Throwable {
  50         final int iterations = 1 << 8;
  51         Thread thread = new Thread() { public void run() {
  52             while (! quit) {
  53                 ThreadPoolExecutor pool = ShutdownNowExecuteRace.pool;
  54                 if (pool != null)
  55                     try { pool.execute(sleeper); }
  56                     catch (RejectedExecutionException e) {/* OK */}
  57                     catch (Throwable t) { unexpected(t); }}}};
  58         thread.start();
  59         for (int i = 0; i < iterations; i++) {
  60             pool = new ThreadPoolExecutor(
  61                 10, 10, 3L, TimeUnit.DAYS,
  62                 new ArrayBlockingQueue<Runnable>(10));
  63             pool.shutdownNow();
  64             check(pool.awaitTermination(3L, TimeUnit.MINUTES));
  65         }
  66         quit = true;
  67         thread.join();
  68     }
  69 
  70     //--------------------- Infrastructure ---------------------------
  71     static volatile int passed = 0, failed = 0;
  72     static void pass() {passed++;}
  73     static void fail() {failed++; Thread.dumpStack();}
  74     static void fail(String msg) {System.out.println(msg); fail();}
  75     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  76     static void check(boolean cond) {if (cond) pass(); else fail();}
  77     static void equal(Object x, Object y) {
  78         if (x == null ? y == null : x.equals(y)) pass();
  79         else fail(x + " not equal to " + y);}
  80     public static void main(String[] args) throws Throwable {
  81         try {realMain(args);} catch (Throwable t) {unexpected(t);}
  82         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  83         if (failed > 0) throw new AssertionError("Some tests failed");}
  84     private static abstract class Fun {abstract void f() throws Throwable;}
  85     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
  86         for (Fun f : fs)
  87             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
  88             catch (Throwable t) {
  89                 if (k.isAssignableFrom(t.getClass())) pass();
  90                 else unexpected(t);}}
  91     private static abstract class CheckedThread extends Thread {
  92         abstract void realRun() throws Throwable;
  93         public void run() {
  94             try {realRun();} catch (Throwable t) {unexpected(t);}}}
  95 }


  23 
  24 /*
  25  * @test
  26  * @bug 6523756
  27  * @summary Race task submission against shutdownNow
  28  * @author Martin Buchholz
  29  */
  30 
  31 // For extra chances to detect shutdownNow vs. execute races,
  32 // crank up the iterations to, say 1<<22 and
  33 // add a call to Thread.yield() before the call to t.start()
  34 // in ThreadPoolExecutor.addWorker.
  35 
  36 import java.util.*;
  37 import java.util.concurrent.*;
  38 
  39 public class ShutdownNowExecuteRace {
  40     static volatile boolean quit = false;
  41     static volatile ThreadPoolExecutor pool = null;
  42 
  43     static final Runnable sleeper = new Runnable() { public void run() {
  44         final long ONE_HOUR = 1000L * 60L * 60L;
  45         try { Thread.sleep(ONE_HOUR); }
  46         catch (InterruptedException ie) {}
  47         catch (Throwable t) { unexpected(t); }}};
  48 
  49     static void realMain(String[] args) throws Throwable {
  50         final int iterations = 1 << 8;
  51         Thread thread = new Thread() { public void run() {
  52             while (! quit) {
  53                 ThreadPoolExecutor pool = ShutdownNowExecuteRace.pool;
  54                 if (pool != null)
  55                     try { pool.execute(sleeper); }
  56                     catch (RejectedExecutionException e) {/* OK */}
  57                     catch (Throwable t) { unexpected(t); }}}};
  58         thread.start();
  59         for (int i = 0; i < iterations; i++) {
  60             pool = new ThreadPoolExecutor(
  61                 10, 10, 3L, TimeUnit.DAYS,
  62                 new ArrayBlockingQueue<Runnable>(10));
  63             pool.shutdownNow();
  64             check(pool.awaitTermination(3L, TimeUnit.MINUTES));
  65         }
  66         quit = true;
  67         thread.join();
  68     }
  69 
  70     //--------------------- Infrastructure ---------------------------
  71     static volatile int passed = 0, failed = 0;
  72     static void pass() {passed++;}
  73     static void fail() {failed++; Thread.dumpStack();}
  74     static void fail(String msg) {System.out.println(msg); fail();}
  75     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  76     static void check(boolean cond) {if (cond) pass(); else fail();}
  77     static void equal(Object x, Object y) {
  78         if (x == null ? y == null : x.equals(y)) pass();
  79         else fail(x + " not equal to " + y);}
  80     public static void main(String[] args) throws Throwable {
  81         try {realMain(args);} catch (Throwable t) {unexpected(t);}
  82         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  83         if (failed > 0) throw new AssertionError("Some tests failed");}
  84     private abstract static class Fun {abstract void f() throws Throwable;}
  85     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
  86         for (Fun f : fs)
  87             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
  88             catch (Throwable t) {
  89                 if (k.isAssignableFrom(t.getClass())) pass();
  90                 else unexpected(t);}}
  91     private abstract static class CheckedThread extends Thread {
  92         abstract void realRun() throws Throwable;
  93         public void run() {
  94             try {realRun();} catch (Throwable t) {unexpected(t);}}}
  95 }