--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2009/11/18 08:22:57 1.26 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2009/11/21 02:33:20 1.30 @@ -10,6 +10,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.concurrent.locks.*; import java.io.*; @@ -65,31 +66,27 @@ public class AbstractQueuedSynchronizerT } /** - * 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); } } @@ -97,7 +94,7 @@ public class AbstractQueuedSynchronizerT * isHeldExclusively is false upon construction */ public void testIsHeldExclusively() { - Mutex rl = new Mutex(); + Mutex rl = new Mutex(); assertFalse(rl.isHeldExclusively()); } @@ -105,7 +102,7 @@ public class AbstractQueuedSynchronizerT * acquiring released sync succeeds */ public void testAcquire() { - Mutex rl = new Mutex(); + Mutex rl = new Mutex(); rl.acquire(1); assertTrue(rl.isHeldExclusively()); rl.release(1); @@ -116,7 +113,7 @@ public class AbstractQueuedSynchronizerT * tryAcquire on an released sync succeeds */ public void testTryAcquire() { - Mutex rl = new Mutex(); + Mutex rl = new Mutex(); assertTrue(rl.tryAcquire(1)); assertTrue(rl.isHeldExclusively()); rl.release(1); @@ -126,7 +123,7 @@ public class AbstractQueuedSynchronizerT * hasQueuedThreads reports whether there are waiting threads */ public void testhasQueuedThreads() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertFalse(sync.hasQueuedThreads()); @@ -151,7 +148,7 @@ public class AbstractQueuedSynchronizerT * isQueued(null) throws NPE */ public void testIsQueuedNPE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); try { sync.isQueued(null); shouldThrow(); @@ -162,7 +159,7 @@ public class AbstractQueuedSynchronizerT * isQueued reports whether a thread is queued. */ public void testIsQueued() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertFalse(sync.isQueued(t1)); @@ -192,7 +189,7 @@ public class AbstractQueuedSynchronizerT * getFirstQueuedThread returns first waiting thread or null if none */ public void testGetFirstQueuedThread() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertNull(sync.getFirstQueuedThread()); @@ -219,7 +216,7 @@ public class AbstractQueuedSynchronizerT * hasContended reports false if no thread has ever blocked, else true */ public void testHasContended() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertFalse(sync.hasContended()); @@ -244,7 +241,7 @@ public class AbstractQueuedSynchronizerT * getQueuedThreads includes waiting threads */ public void testGetQueuedThreads() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertTrue(sync.getQueuedThreads().isEmpty()); @@ -272,7 +269,7 @@ public class AbstractQueuedSynchronizerT * getExclusiveQueuedThreads includes waiting threads */ public void testGetExclusiveQueuedThreads() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); @@ -300,7 +297,7 @@ public class AbstractQueuedSynchronizerT * getSharedQueuedThreads does not include exclusively waiting threads */ public void testGetSharedQueuedThreads() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); assertTrue(sync.getSharedQueuedThreads().isEmpty()); @@ -325,20 +322,17 @@ public class AbstractQueuedSynchronizerT /** * tryAcquireNanos is interruptible. */ - public void testInterruptedException2() { - 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) {} - } - }); + public void testInterruptedException2() throws InterruptedException { + final Mutex sync = new Mutex(); + sync.acquire(1); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); + }}); t.start(); t.interrupt(); + t.join(); } @@ -346,13 +340,12 @@ public class AbstractQueuedSynchronizerT * TryAcquire on exclusively held sync fails */ 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)); - } - }); + final Mutex sync = new Mutex(); + sync.acquire(1); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + threadAssertFalse(sync.tryAcquire(1)); + }}); t.start(); t.join(); @@ -363,17 +356,12 @@ public class AbstractQueuedSynchronizerT * tryAcquireNanos on an exclusively held sync times out */ 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(); - } - } - }); + final Mutex sync = new Mutex(); + sync.acquire(1); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); + }}); t.start(); t.join(); @@ -385,23 +373,17 @@ public class AbstractQueuedSynchronizerT * getState is true when acquired and false when not */ public void testGetState() throws InterruptedException { - final Mutex sync = new Mutex(); - sync.acquire(1); - 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(); - } - sync.release(1); - } - }); + final Mutex sync = new Mutex(); + sync.acquire(1); + assertTrue(sync.isHeldExclusively()); + sync.release(1); + assertFalse(sync.isHeldExclusively()); + 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); @@ -415,9 +397,9 @@ public class AbstractQueuedSynchronizerT * acquireInterruptibly is interruptible. */ public void testAcquireInterruptibly1() throws InterruptedException { - final Mutex sync = new Mutex(); - sync.acquire(1); - Thread t = new Thread(new InterruptedSyncRunnable(sync)); + final Mutex sync = new Mutex(); + sync.acquire(1); + Thread t = new Thread(new InterruptedSyncRunnable(sync)); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -431,9 +413,9 @@ public class AbstractQueuedSynchronizerT * acquireInterruptibly succeeds when released, else is interruptible */ public void testAcquireInterruptibly2() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); sync.acquireInterruptibly(1); - Thread t = new Thread(new InterruptedSyncRunnable(sync)); + Thread t = new Thread(new InterruptedSyncRunnable(sync)); t.start(); t.interrupt(); assertTrue(sync.isHeldExclusively()); @@ -444,7 +426,7 @@ public class AbstractQueuedSynchronizerT * owns is true for a condition created by sync else false */ public void testOwns() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); assertTrue(sync.owns(c)); @@ -455,33 +437,31 @@ public class AbstractQueuedSynchronizerT * Calling await without holding sync throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.await(); shouldThrow(); - } - catch (IllegalMonitorStateException success) {} + } catch (IllegalMonitorStateException success) {} } /** * Calling signal without holding sync throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.signal(); shouldThrow(); - } - catch (IllegalMonitorStateException success) {} + } catch (IllegalMonitorStateException success) {} } /** * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); sync.acquire(1); long t = c.awaitNanos(100); @@ -493,10 +473,10 @@ public class AbstractQueuedSynchronizerT * Timed await without a signal times out */ public void testAwait_Timeout() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); sync.acquire(1); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); sync.release(1); } @@ -504,7 +484,7 @@ public class AbstractQueuedSynchronizerT * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); sync.acquire(1); java.util.Date d = new java.util.Date(); @@ -516,20 +496,14 @@ public class AbstractQueuedSynchronizerT * await returns when signalled */ public void testAwait() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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(); - } - } - }); + 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); @@ -546,7 +520,7 @@ public class AbstractQueuedSynchronizerT * hasWaiters throws NPE if null */ public void testHasWaitersNPE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); try { sync.hasWaiters(null); shouldThrow(); @@ -557,7 +531,7 @@ public class AbstractQueuedSynchronizerT * getWaitQueueLength throws NPE if null */ public void testGetWaitQueueLengthNPE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); try { sync.getWaitQueueLength(null); shouldThrow(); @@ -569,7 +543,7 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws NPE if null */ public void testGetWaitingThreadsNPE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); try { sync.getWaitingThreads(null); shouldThrow(); @@ -581,9 +555,9 @@ public class AbstractQueuedSynchronizerT * hasWaiters throws IAE if not owned */ public void testHasWaitersIAE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - final Mutex sync2 = new Mutex(); + final Mutex sync2 = new Mutex(); try { sync2.hasWaiters(c); shouldThrow(); @@ -594,7 +568,7 @@ public class AbstractQueuedSynchronizerT * hasWaiters throws IMSE if not synced */ public void testHasWaitersIMSE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.hasWaiters(c); @@ -607,9 +581,9 @@ public class AbstractQueuedSynchronizerT * getWaitQueueLength throws IAE if not owned */ public void testGetWaitQueueLengthIAE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - final Mutex sync2 = new Mutex(); + final Mutex sync2 = new Mutex(); try { sync2.getWaitQueueLength(c); shouldThrow(); @@ -620,7 +594,7 @@ public class AbstractQueuedSynchronizerT * getWaitQueueLength throws IMSE if not synced */ public void testGetWaitQueueLengthIMSE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.getWaitQueueLength(c); @@ -633,9 +607,9 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws IAE if not owned */ public void testGetWaitingThreadsIAE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - final Mutex sync2 = new Mutex(); + final Mutex sync2 = new Mutex(); try { sync2.getWaitingThreads(c); shouldThrow(); @@ -646,7 +620,7 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws IMSE if not synced */ public void testGetWaitingThreadsIMSE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.getWaitingThreads(c); @@ -660,22 +634,16 @@ public class AbstractQueuedSynchronizerT * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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(); - } - } - }); + 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); @@ -697,37 +665,25 @@ public class AbstractQueuedSynchronizerT * getWaitQueueLength returns number of waiting threads */ public void testGetWaitQueueLength() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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(); - } - } - }); - - 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(); - } - } - }); + 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); @@ -753,35 +709,23 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads returns only and all waiting threads */ public void testGetWaitingThreads() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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(); - } - } - }); - - 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(); - } - } - }); + 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()); @@ -813,15 +757,14 @@ public class AbstractQueuedSynchronizerT * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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); @@ -837,20 +780,14 @@ public class AbstractQueuedSynchronizerT * await is interruptible */ public void testAwait_Interrupt() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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); @@ -863,20 +800,14 @@ public class AbstractQueuedSynchronizerT * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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); @@ -889,21 +820,15 @@ public class AbstractQueuedSynchronizerT * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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); @@ -916,33 +841,21 @@ public class AbstractQueuedSynchronizerT * signalAll wakes up all threads */ public void testSignalAll() throws InterruptedException { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.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(); @@ -993,41 +906,36 @@ public class AbstractQueuedSynchronizerT * tryReleaseShared setting state changes getState */ public void testGetStateWithReleaseShared() { - final BooleanLatch l = new BooleanLatch(); - assertFalse(l.isSignalled()); - l.releaseShared(0); - assertTrue(l.isSignalled()); + final BooleanLatch l = new BooleanLatch(); + assertFalse(l.isSignalled()); + l.releaseShared(0); + assertTrue(l.isSignalled()); } /** * releaseShared has no effect when already signalled */ public void testReleaseShared() { - final BooleanLatch l = new BooleanLatch(); - assertFalse(l.isSignalled()); - l.releaseShared(0); - assertTrue(l.isSignalled()); - l.releaseShared(0); - assertTrue(l.isSignalled()); + final BooleanLatch l = new BooleanLatch(); + assertFalse(l.isSignalled()); + l.releaseShared(0); + assertTrue(l.isSignalled()); + l.releaseShared(0); + assertTrue(l.isSignalled()); } /** * acquireSharedInterruptibly returns after release, but not before */ public void testAcquireSharedInterruptibly() throws InterruptedException { - final BooleanLatch l = new BooleanLatch(); + 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()); @@ -1042,20 +950,14 @@ public class AbstractQueuedSynchronizerT * acquireSharedTimed returns after release */ public void testAsquireSharedTimed() throws InterruptedException { - final BooleanLatch l = new BooleanLatch(); + 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()); @@ -1070,16 +972,13 @@ public class AbstractQueuedSynchronizerT */ 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) {} - } - }); - t.start(); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(l.isSignalled()); + l.acquireSharedInterruptibly(0); + }}); + + t.start(); assertFalse(l.isSignalled()); t.interrupt(); t.join(); @@ -1090,15 +989,12 @@ public class AbstractQueuedSynchronizerT */ 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); assertFalse(l.isSignalled()); @@ -1111,21 +1007,16 @@ public class AbstractQueuedSynchronizerT */ 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); assertFalse(l.isSignalled()); t.join(); } - }