--- jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java 2009/11/18 08:22:57 1.9 +++ jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java 2009/11/18 16:04:34 1.10 @@ -68,31 +68,27 @@ public class AbstractQueuedLongSynchroni } /** - * A runnable calling acquireInterruptibly + * A runnable calling acquireInterruptibly that does not expect to + * be interrupted. */ - class InterruptibleSyncRunnable implements Runnable { + class InterruptibleSyncRunnable extends CheckedRunnable { final Mutex sync; InterruptibleSyncRunnable(Mutex l) { sync = l; } - public void run() { - try { - sync.acquireInterruptibly(1); - } catch (InterruptedException success) {} + public void realRun() throws InterruptedException { + sync.acquireInterruptibly(1); } } /** * A runnable calling acquireInterruptibly that expects to be - * interrupted + * interrupted. */ - class InterruptedSyncRunnable implements Runnable { + class InterruptedSyncRunnable extends CheckedInterruptedRunnable { final Mutex sync; InterruptedSyncRunnable(Mutex l) { sync = l; } - public void run() { - try { - sync.acquireInterruptibly(1); - threadShouldThrow(); - } catch (InterruptedException success) {} + public void realRun() throws InterruptedException { + sync.acquireInterruptibly(1); } } @@ -331,14 +327,10 @@ public class AbstractQueuedLongSynchroni public void testInterruptedException2() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); - Thread t = new Thread(new Runnable() { - public void run() { - try { - sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); - threadShouldThrow(); - } catch (InterruptedException success) {} - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); + }}); t.start(); t.interrupt(); @@ -352,11 +344,10 @@ public class AbstractQueuedLongSynchroni public void testTryAcquireWhenSynced() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertFalse(sync.tryAcquire(1)); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + threadAssertFalse(sync.tryAcquire(1)); + }}); t.start(); t.join(); @@ -369,15 +360,10 @@ public class AbstractQueuedLongSynchroni public void testAcquireNanos_Timeout() throws InterruptedException { final Mutex sync = new Mutex(); sync.acquire(1); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); - } catch (Exception ex) { - threadUnexpectedException(ex); - } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); + }}); t.start(); t.join(); @@ -394,18 +380,12 @@ public class AbstractQueuedLongSynchroni assertTrue(sync.isHeldExclusively()); sync.release(1); assertFalse(sync.isHeldExclusively()); - Thread t = new Thread(new Runnable() { - public void run() { - sync.acquire(1); - try { - Thread.sleep(SMALL_DELAY_MS); - } - catch (Exception e) { - threadUnexpectedException(e); - } - sync.release(1); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + Thread.sleep(SMALL_DELAY_MS); + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -463,9 +443,7 @@ public class AbstractQueuedLongSynchroni try { c.await(); shouldThrow(); - } - catch (IllegalMonitorStateException success) { - } + } catch (IllegalMonitorStateException success) {} } /** @@ -477,8 +455,7 @@ public class AbstractQueuedLongSynchroni try { c.signal(); shouldThrow(); - } - catch (IllegalMonitorStateException success) {} + } catch (IllegalMonitorStateException success) {} } /** @@ -522,18 +499,12 @@ public class AbstractQueuedLongSynchroni public void testAwait() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(e); - } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + c.await(); + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -666,20 +637,14 @@ public class AbstractQueuedLongSynchroni public void testHasWaiters() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - threadAssertFalse(sync.hasWaiters(c)); - threadAssertEquals(0, sync.getWaitQueueLength(c)); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(e); - } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + threadAssertFalse(sync.hasWaiters(c)); + threadAssertEquals(0, sync.getWaitQueueLength(c)); + c.await(); + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -703,35 +668,23 @@ public class AbstractQueuedLongSynchroni public void testGetWaitQueueLength() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - threadAssertFalse(sync.hasWaiters(c)); - threadAssertEquals(0, sync.getWaitQueueLength(c)); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(e); - } - } - }); - - Thread t2 = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - threadAssertTrue(sync.hasWaiters(c)); - threadAssertEquals(1, sync.getWaitQueueLength(c)); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(e); - } - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + threadAssertFalse(sync.hasWaiters(c)); + threadAssertEquals(0, sync.getWaitQueueLength(c)); + c.await(); + sync.release(1); + }}); + + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + threadAssertTrue(sync.hasWaiters(c)); + threadAssertEquals(1, sync.getWaitQueueLength(c)); + c.await(); + sync.release(1); + }}); t1.start(); Thread.sleep(SHORT_DELAY_MS); @@ -759,33 +712,21 @@ public class AbstractQueuedLongSynchroni public void testGetWaitingThreads() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(e); - } - } - }); - - Thread t2 = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(e); - } - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); + c.await(); + sync.release(1); + }}); + + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); + c.await(); + sync.release(1); + }}); sync.acquire(1); assertTrue(sync.getWaitingThreads(c).isEmpty()); @@ -819,13 +760,12 @@ public class AbstractQueuedLongSynchroni public void testAwaitUninterruptibly() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - sync.acquire(1); - c.awaitUninterruptibly(); - sync.release(1); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + sync.acquire(1); + c.awaitUninterruptibly(); + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -843,18 +783,12 @@ public class AbstractQueuedLongSynchroni public void testAwait_Interrupt() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - c.await(); - sync.release(1); - threadShouldThrow(); - } - catch (InterruptedException success) { - } - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + c.await(); + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -869,18 +803,12 @@ public class AbstractQueuedLongSynchroni public void testAwaitNanos_Interrupt() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - c.awaitNanos(1000 * 1000 * 1000); // 1 sec - sync.release(1); - threadShouldThrow(); - } - catch (InterruptedException success) { - } - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + c.awaitNanos(1000 * 1000 * 1000); // 1 sec + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -895,19 +823,13 @@ public class AbstractQueuedLongSynchroni public void testAwaitUntil_Interrupt() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - java.util.Date d = new java.util.Date(); - c.awaitUntil(new java.util.Date(d.getTime() + 10000)); - sync.release(1); - threadShouldThrow(); - } - catch (InterruptedException success) { - } - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + java.util.Date d = new java.util.Date(); + c.awaitUntil(new java.util.Date(d.getTime() + 10000)); + sync.release(1); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -922,31 +844,19 @@ public class AbstractQueuedLongSynchroni public void testSignalAll() throws InterruptedException { final Mutex sync = new Mutex(); final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - Thread t2 = new Thread(new Runnable() { - public void run() { - try { - sync.acquire(1); - c.await(); - sync.release(1); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + c.await(); + sync.release(1); + }}); + + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + sync.acquire(1); + c.await(); + sync.release(1); + }}); t1.start(); t2.start(); @@ -1021,17 +931,12 @@ public class AbstractQueuedLongSynchroni public void testAcquireSharedInterruptibly() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(l.isSignalled()); - l.acquireSharedInterruptibly(0); - threadAssertTrue(l.isSignalled()); - } catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(l.isSignalled()); + l.acquireSharedInterruptibly(0); + threadAssertTrue(l.isSignalled()); + }}); t.start(); assertFalse(l.isSignalled()); @@ -1048,18 +953,12 @@ public class AbstractQueuedLongSynchroni public void testAsquireSharedTimed() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(l.isSignalled()); - threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); - threadAssertTrue(l.isSignalled()); - - } catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(l.isSignalled()); + threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); + threadAssertTrue(l.isSignalled()); + }}); t.start(); assertFalse(l.isSignalled()); @@ -1074,15 +973,11 @@ public class AbstractQueuedLongSynchroni */ public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(l.isSignalled()); - l.acquireSharedInterruptibly(0); - threadShouldThrow(); - } catch (InterruptedException success) {} - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(l.isSignalled()); + l.acquireSharedInterruptibly(0); + }}); t.start(); assertFalse(l.isSignalled()); @@ -1095,15 +990,11 @@ public class AbstractQueuedLongSynchroni */ public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(l.isSignalled()); - l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); - threadShouldThrow(); - } catch (InterruptedException success) {} - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(l.isSignalled()); + l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -1117,16 +1008,11 @@ public class AbstractQueuedLongSynchroni */ public void testAcquireSharedNanos_Timeout() throws InterruptedException { final BooleanLatch l = new BooleanLatch(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(l.isSignalled()); - threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); - } catch (InterruptedException ie) { - threadUnexpectedException(); - } - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(l.isSignalled()); + threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -1134,5 +1020,4 @@ public class AbstractQueuedLongSynchroni t.join(); } - }