--- jsr166/src/test/tck/PhaserTest.java 2009/07/31 23:02:50 1.1 +++ jsr166/src/test/tck/PhaserTest.java 2009/08/03 22:06:50 1.6 @@ -40,7 +40,7 @@ public class PhaserTest extends JSR166Te public void testConstructor2() { try { new Phaser(-1); - this.shouldThrow(); + shouldThrow(); } catch (IllegalArgumentException success) { } } @@ -88,7 +88,7 @@ public class PhaserTest extends JSR166Te } /** - * Registering any more then 65536 parties causes IllegalStateExceptiom + * Registering more than 65536 parties causes IllegalStateException */ public void testRegister2() { Phaser phaser = new Phaser(0); @@ -101,8 +101,6 @@ public class PhaserTest extends JSR166Te phaser.register(); shouldThrow(); } catch (IllegalStateException success) { - } catch (Exception ex) { - threadUnexpectedException(ex); } } @@ -137,7 +135,7 @@ public class PhaserTest extends JSR166Te /** * Invoking bulkRegister with a negative parameter throws an - * IllegalArgumentExceptiom + * IllegalArgumentException */ public void testBulkRegister1() { try { @@ -158,8 +156,8 @@ public class PhaserTest extends JSR166Te } /** - * Registering with a number of parties greater then or equal to 1<<16 - * throws IllegalStateExceptiom. + * Registering with a number of parties greater than or equal to 1<<16 + * throws IllegalStateException. */ public void testBulkRegister3() { try { @@ -200,13 +198,11 @@ public class PhaserTest extends JSR166Te Thread thread = null; for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) { phaser.register(); - thread = new Thread() { - - public void run() { + thread = new Thread(new CheckedRunnable() { + void realRun() { r.run(); phaser.arriveAndDeregister(); - } - }; + }}); thread.start(); } @@ -216,7 +212,7 @@ public class PhaserTest extends JSR166Te } /** - * arrive() returns a negative number if the Phaser is termindated + * arrive() returns a negative number if the Phaser is terminated */ public void testArrive3() { Phaser phaser = new Phaser(1); @@ -227,20 +223,19 @@ public class PhaserTest extends JSR166Te /** * arriveAndDeregister() throws IllegalStateException if number of - * registered or unnarived parties would become negative + * registered or unarrived parties would become negative */ public void testArriveAndDeregister1() { try { Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); - } catch (IllegalStateException success) { } } /** - * arriveAndDeregister derigisters reduces the number of arrived parties + * arriveAndDeregister deregisters reduces the number of arrived parties */ public void testArriveAndDergeister2() { final Phaser phaser = new Phaser(1); @@ -306,13 +301,11 @@ public class PhaserTest extends JSR166Te */ public void testArriveAndDeregister6() { final Phaser phaser = new Phaser(2); - new Thread() { - - public void run() { + new Thread(new CheckedRunnable() { + void realRun() { getRunnable(SHORT_DELAY_MS).run(); phaser.arrive(); - } - }.start(); + }}).start(); phaser.arriveAndAwaitAdvance(); int phase = phaser.arriveAndDeregister(); assertEquals(phase, phaser.getPhase()); @@ -331,42 +324,28 @@ public class PhaserTest extends JSR166Te * phaser */ public void testAwaitAdvance2() { - try { - Phaser phaser = new Phaser(); - phaser.awaitAdvance(-1); - } catch (Exception failure) { - this.unexpectedException(); - } + Phaser phaser = new Phaser(); + phaser.awaitAdvance(-1); } /** * awaitAdvance while waiting does not abort on interrupt. */ - public void testAwaitAdvance3() { + public void testAwaitAdvance3() throws InterruptedException { final Phaser phaser = new Phaser(); - Thread th1 = new Thread() { - public void run() { - try { - phaser.register(); - getRunnable(LONG_DELAY_MS).run(); - phaser.awaitAdvance(phaser.arrive()); - } catch (Exception failure) { - threadUnexpectedException(failure); - } - - } - }; + Thread th1 = new Thread(new CheckedRunnable() { + void realRun() throws InterruptedException { + phaser.register(); + getRunnable(LONG_DELAY_MS).run(); + phaser.awaitAdvance(phaser.arrive()); + }}); phaser.register(); th1.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - th1.interrupt(); - Thread.sleep(LONG_DELAY_MS); - phaser.arrive(); - } catch (Exception failure) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + th1.interrupt(); + Thread.sleep(LONG_DELAY_MS); + phaser.arrive(); assertFalse(th1.isInterrupted()); } @@ -374,21 +353,24 @@ public class PhaserTest extends JSR166Te * awaitAdvance atomically waits for all parties within the same phase to * complete before continuing */ - public void testAwaitAdvance4() { - final Phaser phaser = new Phaser(four); + public void testAwaitAdvance4() throws InterruptedException { + final Phaser phaser = new Phaser(4); final AtomicInteger phaseCount = new AtomicInteger(0); - for (int i = 0; i < four; i++) { - new Thread() { - - public void run() { + List threads = new ArrayList(); + for (int i = 0; i < 4; i++) { + threads.add(new Thread(new CheckedRunnable() { + void realRun() { int phase = phaser.arrive(); phaseCount.incrementAndGet(); getRunnable(LONG_DELAY_MS).run(); phaser.awaitAdvance(phase); - assertTrue(phaseCount.get() == four); - } - }.start(); + threadAssertTrue(phaseCount.get() == 4); + }})); } + for (Thread thread : threads) + thread.start(); + for (Thread thread : threads) + thread.join(); } /** @@ -400,15 +382,13 @@ public class PhaserTest extends JSR166Te assertEquals(phase, phaser.getPhase()); phaser.register(); for (int i = 0; i < eight; i++) { - new Thread() { - - public void run() { + new Thread(new CheckedRunnable() { + void realRun() { getRunnable(SHORT_DELAY_MS).run(); phaser.arrive(); - } - }.start(); + }}).start(); phase = phaser.awaitAdvance(phaser.arrive()); - assertEquals(phase, phaser.getPhase()); + threadAssertEquals(phase, phaser.getPhase()); } } @@ -423,9 +403,8 @@ public class PhaserTest extends JSR166Te * in the main thread arrives quickly so at best this thread * waits for the second thread's party to arrive */ - new Thread() { - - public void run() { + new Thread(new CheckedRunnable() { + void realRun() { getRunnable(SMALL_DELAY_MS).run(); int phase = phaser.awaitAdvance(phaser.arrive()); /* @@ -433,23 +412,20 @@ public class PhaserTest extends JSR166Te */ threadAssertTrue(phase < 0); threadAssertTrue(phaser.isTerminated()); - } - }.start(); + }}).start(); /* * This thread will cause the first thread run to wait, in doing so - * the main thread will force termination in which the first thread - * should exit peacefully as this one + * the main thread will force termination in which the first thread + * should exit peacefully as this one */ - new Thread() { - - public void run() { + new Thread(new CheckedRunnable() { + void realRun() { getRunnable(LONG_DELAY_MS).run(); int p1 = phaser.arrive(); int phase = phaser.awaitAdvance(p1); threadAssertTrue(phase < 0); threadAssertTrue(phaser.isTerminated()); - } - }.start(); + }}).start(); phaser.arrive(); phaser.forceTermination(); @@ -471,27 +447,18 @@ public class PhaserTest extends JSR166Te /** * Interrupted arriveAndAwaitAdvance does not throw InterruptedException */ - public void testArriveAndAwaitAdvance2() { + public void testArriveAndAwaitAdvance2() throws InterruptedException { final Phaser phaser = new Phaser(2); - Thread th = new Thread() { - public void run() { - try { - phaser.arriveAndAwaitAdvance(); - } catch (Exception failure) { - threadUnexpectedException(failure); - } - } - }; - - try { - th.start(); - Thread.sleep(LONG_DELAY_MS); - th.interrupt(); - Thread.sleep(LONG_DELAY_MS); - phaser.arrive(); - } catch (InterruptedException failure) { - this.unexpectedException(); - } + Thread th = new Thread(new CheckedRunnable() { + void realRun() { + phaser.arriveAndAwaitAdvance(); + }}); + + th.start(); + Thread.sleep(LONG_DELAY_MS); + th.interrupt(); + Thread.sleep(LONG_DELAY_MS); + phaser.arrive(); assertFalse(th.isInterrupted()); } @@ -504,15 +471,13 @@ public class PhaserTest extends JSR166Te final Phaser phaser = new Phaser(1); final AtomicInteger arrivingCount = new AtomicInteger(0); for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) { - new Thread() { - - public void run() { + new Thread(new CheckedRunnable() { + void realRun() { phaser.register(); run.run(); arrivingCount.getAndIncrement(); phaser.arrive(); - } - }.start(); + }}).start(); } int phaseNumber = phaser.arriveAndAwaitAdvance(); arrivingCount.incrementAndGet(); @@ -530,17 +495,13 @@ public class PhaserTest extends JSR166Te } private Runnable getRunnable(final long wait) { - return new Runnable() { - - public void run() { + return new CheckedRunnable() { + void realRun() { try { Thread.sleep(wait); } catch (InterruptedException noop) { // sleep interruption isn't a problem case for these example - } catch (Exception ex) { - threadUnexpectedException(ex); } - } }; }