--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/03/15 19:47:07 1.44 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/02 00:49:26 1.53 @@ -59,6 +59,15 @@ public class ReentrantReadWriteLockTest } /** + * Releases lock, checking that it had a hold count of 1. + */ + void releaseLock(ReentrantReadWriteLock.WriteLock lock) { + assertTrue(lock.isHeldByCurrentThread()); + lock.unlock(); + assertFalse(lock.isHeldByCurrentThread()); + } + + /** * Constructor sets given fairness, and is in unlocked state */ public void testConstructor() { @@ -190,21 +199,16 @@ public class ReentrantReadWriteLockTest */ public void testWriteLockInterruptibly_Interrupted() throws Exception { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + lock.writeLock().lock(); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lockInterruptibly(); - lock.writeLock().unlock(); - lock.writeLock().lockInterruptibly(); - lock.writeLock().unlock(); }}); - lock.writeLock().lock(); - t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - lock.writeLock().unlock(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseLock(lock.writeLock()); } /** @@ -213,16 +217,15 @@ public class ReentrantReadWriteLockTest public void testWriteTryLock_Interrupted() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - lock.writeLock().unlock(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseLock(lock.writeLock()); } /** @@ -231,17 +234,15 @@ public class ReentrantReadWriteLockTest public void testReadLockInterruptibly_Interrupted() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().lockInterruptibly(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - lock.writeLock().unlock(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseLock(lock.writeLock()); } /** @@ -250,15 +251,15 @@ public class ReentrantReadWriteLockTest public void testReadTryLock_Interrupted() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseLock(lock.writeLock()); } @@ -268,13 +269,12 @@ public class ReentrantReadWriteLockTest public void testWriteTryLockWhenLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertFalse(lock.writeLock().tryLock()); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.writeLock().unlock(); } @@ -284,13 +284,12 @@ public class ReentrantReadWriteLockTest public void testReadTryLockWhenLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertFalse(lock.readLock().tryLock()); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.writeLock().unlock(); } @@ -300,14 +299,13 @@ public class ReentrantReadWriteLockTest public void testMultipleReadLocks() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -317,25 +315,21 @@ public class ReentrantReadWriteLockTest public void testWriteAfterMultipleReadLocks() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - t1.start(); - t2.start(); Thread.sleep(SHORT_DELAY_MS); lock.readLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -344,25 +338,21 @@ public class ReentrantReadWriteLockTest public void testReadAfterWriteLock() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - t1.start(); - t2.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -383,29 +373,25 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLock2() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - t1.start(); - t2.start(); lock.readLock().lock(); lock.readLock().unlock(); Thread.sleep(SHORT_DELAY_MS); lock.readLock().lock(); lock.readLock().unlock(); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -415,29 +401,25 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLock3() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - t1.start(); - t2.start(); lock.readLock().lock(); lock.readLock().unlock(); Thread.sleep(SHORT_DELAY_MS); lock.readLock().lock(); lock.readLock().unlock(); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } @@ -448,29 +430,25 @@ public class ReentrantReadWriteLockTest public void testWriteHoldingWriteLock4() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - t1.start(); - t2.start(); lock.writeLock().lock(); lock.writeLock().unlock(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); lock.writeLock().unlock(); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } @@ -492,29 +470,25 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLockFair2() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.readLock().lock(); lock.readLock().unlock(); }}); - t1.start(); - t2.start(); lock.readLock().lock(); lock.readLock().unlock(); Thread.sleep(SHORT_DELAY_MS); lock.readLock().lock(); lock.readLock().unlock(); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } @@ -525,29 +499,25 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLockFair3() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - t1.start(); - t2.start(); lock.readLock().lock(); lock.readLock().unlock(); Thread.sleep(SHORT_DELAY_MS); lock.readLock().lock(); lock.readLock().unlock(); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } @@ -558,19 +528,17 @@ public class ReentrantReadWriteLockTest public void testWriteHoldingWriteLockFair4() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.writeLock().lock(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { lock.writeLock().lock(); lock.writeLock().unlock(); }}); - t1.start(); - t2.start(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.isWriteLockedByCurrentThread()); assertEquals(1, lock.getWriteHoldCount()); @@ -580,10 +548,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().lock(); lock.writeLock().unlock(); lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } @@ -593,32 +559,28 @@ public class ReentrantReadWriteLockTest public void testTryLockWhenReadLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } - - /** * write tryLock fails when readlocked */ public void testWriteTryLockWhenReadLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertFalse(lock.writeLock().tryLock()); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -629,14 +591,13 @@ public class ReentrantReadWriteLockTest public void testTryLockWhenReadLockedFair() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.readLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -648,13 +609,12 @@ public class ReentrantReadWriteLockTest public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.readLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { assertFalse(lock.writeLock().tryLock()); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -666,13 +626,12 @@ public class ReentrantReadWriteLockTest public void testWriteTryLock_Timeout() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); assertTrue(lock.writeLock().isHeldByCurrentThread()); lock.writeLock().unlock(); } @@ -683,13 +642,12 @@ public class ReentrantReadWriteLockTest public void testReadTryLock_Timeout() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); assertTrue(lock.writeLock().isHeldByCurrentThread()); lock.writeLock().unlock(); } @@ -701,17 +659,15 @@ public class ReentrantReadWriteLockTest public void testWriteLockInterruptibly() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lockInterruptibly(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lockInterruptibly(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseLock(lock.writeLock()); } /** @@ -720,16 +676,15 @@ public class ReentrantReadWriteLockTest public void testReadLockInterruptibly() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lockInterruptibly(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.readLock().lockInterruptibly(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseLock(lock.writeLock()); } /** @@ -799,20 +754,18 @@ public class ReentrantReadWriteLockTest public void testAwait() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); c.await(); lock.writeLock().unlock(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); c.signal(); lock.writeLock().unlock(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t, LONG_DELAY_MS); } /** A helper class for uninterruptible wait tests */ @@ -865,9 +818,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); } - thread.join(); + awaitTermination(thread, LONG_DELAY_MS); assertTrue(thread.interrupted); - assertFalse(thread.isAlive()); } /** @@ -876,18 +828,22 @@ public class ReentrantReadWriteLockTest public void testAwait_Interrupt() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - c.await(); - lock.writeLock().unlock(); + assertTrue(lock.isWriteLocked()); + locked.countDown(); + try { c.await(); } + finally { lock.writeLock().unlock(); } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); + while (lock.isWriteLocked()) + Thread.yield(); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t, LONG_DELAY_MS); + assertFalse(lock.isWriteLocked()); } /** @@ -896,18 +852,22 @@ public class ReentrantReadWriteLockTest public void testAwaitNanos_Interrupt() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); - lock.writeLock().unlock(); + assertTrue(lock.isWriteLocked()); + locked.countDown(); + try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); } + finally { lock.writeLock().unlock(); } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); + while (lock.isWriteLocked()) + Thread.yield(); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t, LONG_DELAY_MS); + assertFalse(lock.isWriteLocked()); } /** @@ -916,19 +876,23 @@ public class ReentrantReadWriteLockTest public void testAwaitUntil_Interrupt() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch locked = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); + assertTrue(lock.isWriteLocked()); + locked.countDown(); java.util.Date d = new java.util.Date(); - c.awaitUntil(new java.util.Date(d.getTime() + 10000)); - lock.writeLock().unlock(); + try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); } + finally { lock.writeLock().unlock(); } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + locked.await(); + while (lock.isWriteLocked()) + Thread.yield(); t.interrupt(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t, LONG_DELAY_MS); + assertFalse(lock.isWriteLocked()); } /** @@ -937,30 +901,26 @@ public class ReentrantReadWriteLockTest public void testSignalAll() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t1 = new Thread(new CheckedRunnable() { + Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); c.await(); lock.writeLock().unlock(); }}); - Thread t2 = new Thread(new CheckedRunnable() { + Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); c.await(); lock.writeLock().unlock(); }}); - t1.start(); - t2.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); c.signalAll(); lock.writeLock().unlock(); - t1.join(SHORT_DELAY_MS); - t2.join(SHORT_DELAY_MS); - assertFalse(t1.isAlive()); - assertFalse(t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -1004,8 +964,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); Thread.sleep(SHORT_DELAY_MS); assertFalse(lock.hasQueuedThreads()); - t1.join(); - t2.join(); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -1045,8 +1005,8 @@ public class ReentrantReadWriteLockTest assertFalse(sync.hasQueuedThread(t1)); Thread.sleep(SHORT_DELAY_MS); assertFalse(sync.hasQueuedThread(t2)); - t1.join(); - t2.join(); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } @@ -1071,8 +1031,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); Thread.sleep(SHORT_DELAY_MS); assertEquals(0, lock.getQueueLength()); - t1.join(); - t2.join(); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -1099,8 +1059,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); Thread.sleep(SHORT_DELAY_MS); assertTrue(lock.getQueuedThreads().isEmpty()); - t1.join(); - t2.join(); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -1221,7 +1181,7 @@ public class ReentrantReadWriteLockTest public void testHasWaiters() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); assertFalse(lock.hasWaiters(c)); @@ -1230,7 +1190,6 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); assertTrue(lock.hasWaiters(c)); @@ -1242,8 +1201,7 @@ public class ReentrantReadWriteLockTest assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); lock.writeLock().unlock(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t, LONG_DELAY_MS); } /** @@ -1252,7 +1210,7 @@ public class ReentrantReadWriteLockTest public void testGetWaitQueueLength() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); assertFalse(lock.hasWaiters(c)); @@ -1261,7 +1219,6 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); assertTrue(lock.hasWaiters(c)); @@ -1273,8 +1230,7 @@ public class ReentrantReadWriteLockTest assertFalse(lock.hasWaiters(c)); assertEquals(0, lock.getWaitQueueLength(c)); lock.writeLock().unlock(); - t.join(SHORT_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t, LONG_DELAY_MS); } @@ -1318,10 +1274,8 @@ public class ReentrantReadWriteLockTest assertFalse(lock.hasWaiters(c)); assertTrue(lock.getWaitingThreads(c).isEmpty()); lock.writeLock().unlock(); - t1.join(SHORT_DELAY_MS); - t2.join(SHORT_DELAY_MS); - assertFalse(t1.isAlive()); - assertFalse(t2.isAlive()); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /**