test/java/util/concurrent/CyclicBarrier/Basic.java

Print this page




  66     }
  67 
  68     //----------------------------------------------------------------
  69     // Mechanism to get all victim threads into "running" mode.
  70     // The fact that this also uses CyclicBarrier is entirely coincidental.
  71     //----------------------------------------------------------------
  72     private static final CyclicBarrier atTheStartingGate = new CyclicBarrier(3);
  73 
  74     private static void toTheStartingGate() {
  75         try { atTheStartingGate.await(10, SECONDS); pass(); }
  76         catch (Throwable t) {
  77             unexpected(t);
  78             reset(atTheStartingGate);
  79             throw new Error(t);
  80         }
  81     }
  82 
  83     //----------------------------------------------------------------
  84     // Convenience methods for creating threads that call CyclicBarrier.await
  85     //----------------------------------------------------------------
  86     private static abstract class Awaiter extends Thread {
  87         static AtomicInteger count = new AtomicInteger(1);
  88 
  89         {
  90             this.setName("Awaiter:"+count.getAndIncrement());
  91             this.setDaemon(true);
  92         }
  93 
  94         private volatile Throwable result = null;
  95         protected void result(Throwable result) { this.result = result; }
  96         public Throwable result() { return this.result; }
  97     }
  98 
  99     private static Awaiter awaiter(final CyclicBarrier barrier) {
 100         return new Awaiter() { public void run() {
 101             toTheStartingGate();
 102 
 103             try { barrier.await(); }
 104             catch (Throwable result) { result(result); }}};
 105     }
 106 


 400             equal(countBrokenBarrierException, N-1);
 401             checkBroken(barrier);
 402             reset(barrier);
 403         } catch (Throwable t) { unexpected(t); }
 404     }
 405 
 406     //--------------------- Infrastructure ---------------------------
 407     static volatile int passed = 0, failed = 0;
 408     static void pass() {passed++;}
 409     static void fail() {failed++; Thread.dumpStack();}
 410     static void fail(String msg) {System.out.println(msg); fail();}
 411     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 412     static void check(boolean cond) {if (cond) pass(); else fail();}
 413     static void equal(Object x, Object y) {
 414         if (x == null ? y == null : x.equals(y)) pass();
 415         else fail(x + " not equal to " + y);}
 416     public static void main(String[] args) throws Throwable {
 417         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 418         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 419         if (failed > 0) throw new AssertionError("Some tests failed");}
 420     static abstract class Fun { abstract void f() throws Throwable; }
 421     private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
 422         for (Fun f : fs)
 423             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
 424             catch (Throwable t) {
 425                 if (k.isAssignableFrom(t.getClass())) pass();
 426                 else unexpected(t);}}
 427     private static abstract class CheckedThread extends Thread {
 428         abstract void realRun() throws Throwable;
 429         public void run() {
 430             try {realRun();} catch (Throwable t) {unexpected(t);}}}
 431 }


  66     }
  67 
  68     //----------------------------------------------------------------
  69     // Mechanism to get all victim threads into "running" mode.
  70     // The fact that this also uses CyclicBarrier is entirely coincidental.
  71     //----------------------------------------------------------------
  72     private static final CyclicBarrier atTheStartingGate = new CyclicBarrier(3);
  73 
  74     private static void toTheStartingGate() {
  75         try { atTheStartingGate.await(10, SECONDS); pass(); }
  76         catch (Throwable t) {
  77             unexpected(t);
  78             reset(atTheStartingGate);
  79             throw new Error(t);
  80         }
  81     }
  82 
  83     //----------------------------------------------------------------
  84     // Convenience methods for creating threads that call CyclicBarrier.await
  85     //----------------------------------------------------------------
  86     private abstract static class Awaiter extends Thread {
  87         static AtomicInteger count = new AtomicInteger(1);
  88 
  89         {
  90             this.setName("Awaiter:"+count.getAndIncrement());
  91             this.setDaemon(true);
  92         }
  93 
  94         private volatile Throwable result = null;
  95         protected void result(Throwable result) { this.result = result; }
  96         public Throwable result() { return this.result; }
  97     }
  98 
  99     private static Awaiter awaiter(final CyclicBarrier barrier) {
 100         return new Awaiter() { public void run() {
 101             toTheStartingGate();
 102 
 103             try { barrier.await(); }
 104             catch (Throwable result) { result(result); }}};
 105     }
 106 


 400             equal(countBrokenBarrierException, N-1);
 401             checkBroken(barrier);
 402             reset(barrier);
 403         } catch (Throwable t) { unexpected(t); }
 404     }
 405 
 406     //--------------------- Infrastructure ---------------------------
 407     static volatile int passed = 0, failed = 0;
 408     static void pass() {passed++;}
 409     static void fail() {failed++; Thread.dumpStack();}
 410     static void fail(String msg) {System.out.println(msg); fail();}
 411     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 412     static void check(boolean cond) {if (cond) pass(); else fail();}
 413     static void equal(Object x, Object y) {
 414         if (x == null ? y == null : x.equals(y)) pass();
 415         else fail(x + " not equal to " + y);}
 416     public static void main(String[] args) throws Throwable {
 417         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 418         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 419         if (failed > 0) throw new AssertionError("Some tests failed");}
 420     abstract static class Fun { abstract void f() throws Throwable; }
 421     private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
 422         for (Fun f : fs)
 423             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
 424             catch (Throwable t) {
 425                 if (k.isAssignableFrom(t.getClass())) pass();
 426                 else unexpected(t);}}
 427     private abstract static class CheckedThread extends Thread {
 428         abstract void realRun() throws Throwable;
 429         public void run() {
 430             try {realRun();} catch (Throwable t) {unexpected(t);}}}
 431 }