--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2009/11/21 21:59:50 1.38 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/02 01:15:26 1.55 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -15,7 +15,7 @@ import java.util.*; public class ReentrantReadWriteLockTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(ReentrantReadWriteLockTest.class); @@ -59,6 +59,16 @@ public class ReentrantReadWriteLockTest } /** + * Releases write lock, checking that it had a hold count of 1. + */ + void releaseWriteLock(ReentrantReadWriteLock lock) { + ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); + assertTrue(writeLock.isHeldByCurrentThread()); + writeLock.unlock(); + assertFalse(writeLock.isHeldByCurrentThread()); + } + + /** * Constructor sets given fairness, and is in unlocked state */ public void testConstructor() { @@ -190,21 +200,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); + releaseWriteLock(lock); } /** @@ -213,16 +218,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); + releaseWriteLock(lock); } /** @@ -231,17 +235,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); + releaseWriteLock(lock); } /** @@ -250,14 +252,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(1000,MILLISECONDS); + lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); }}); - t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } @@ -267,14 +270,13 @@ 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() { - threadAssertFalse(lock.writeLock().tryLock()); + assertFalse(lock.writeLock().tryLock()); }}); - t.start(); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -283,14 +285,13 @@ 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() { - threadAssertFalse(lock.readLock().tryLock()); + assertFalse(lock.readLock().tryLock()); }}); - t.start(); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -299,14 +300,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() { - threadAssertTrue(lock.readLock().tryLock()); + assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -316,25 +316,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); } /** @@ -343,25 +339,23 @@ 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()); + assertTrue(t1.isAlive()); + assertTrue(t2.isAlive()); + releaseWriteLock(lock); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -382,94 +376,82 @@ 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); } /** - * Read lock succeeds if write locked by current thread even if + * Read lock succeeds if write locked by current thread even if * other threads are waiting for writelock */ 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); } /** - * Write lock succeeds if write locked by current thread even if + * Write lock succeeds if write locked by current thread even if * other threads are waiting for writelock */ 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); } @@ -491,29 +473,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); } @@ -524,29 +502,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); } @@ -557,32 +531,28 @@ 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()); - assertTrue(lock.getWriteHoldCount() == 1); + assertEquals(1, lock.getWriteHoldCount()); lock.writeLock().lock(); - assertTrue(lock.getWriteHoldCount() == 2); + assertEquals(2, lock.getWriteHoldCount()); lock.writeLock().unlock(); 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); } @@ -592,32 +562,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() { - threadAssertTrue(lock.readLock().tryLock()); + 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() { - threadAssertFalse(lock.writeLock().tryLock()); + assertFalse(lock.writeLock().tryLock()); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -628,14 +594,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() { - threadAssertTrue(lock.readLock().tryLock()); + assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -647,13 +612,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() { - threadAssertFalse(lock.writeLock().tryLock()); + assertFalse(lock.writeLock().tryLock()); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -665,13 +629,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 { - threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); + assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); assertTrue(lock.writeLock().isHeldByCurrentThread()); lock.writeLock().unlock(); } @@ -682,13 +645,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 { - threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); + assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); }}); - t.start(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); assertTrue(lock.writeLock().isHeldByCurrentThread()); lock.writeLock().unlock(); } @@ -700,35 +662,32 @@ 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); + releaseWriteLock(lock); } /** - * read lockInterruptibly succeeds if lock free else is interruptible + * read lockInterruptibly succeeds if lock free else is interruptible */ 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); + releaseWriteLock(lock); } /** @@ -770,7 +729,7 @@ public class ReentrantReadWriteLockTest /** - * timed await without a signal times out + * timed await without a signal times out */ public void testAwait_Timeout() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); @@ -798,20 +757,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 */ @@ -864,9 +821,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); } - thread.join(); + awaitTermination(thread, LONG_DELAY_MS); assertTrue(thread.interrupted); - assertFalse(thread.isAlive()); } /** @@ -875,18 +831,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()); } /** @@ -895,18 +855,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(SHORT_DELAY_MS * 2 * 1000000); - 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()); } /** @@ -915,19 +879,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()); } /** @@ -936,30 +904,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); } /** @@ -1003,8 +967,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); } /** @@ -1044,8 +1008,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); } @@ -1070,8 +1034,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); } /** @@ -1098,8 +1062,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); } /** @@ -1220,16 +1184,15 @@ 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(); - threadAssertFalse(lock.hasWaiters(c)); - threadAssertEquals(0, lock.getWaitQueueLength(c)); + assertFalse(lock.hasWaiters(c)); + assertEquals(0, lock.getWaitQueueLength(c)); c.await(); lock.writeLock().unlock(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); assertTrue(lock.hasWaiters(c)); @@ -1241,8 +1204,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); } /** @@ -1251,16 +1213,15 @@ 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(); - threadAssertFalse(lock.hasWaiters(c)); - threadAssertEquals(0, lock.getWaitQueueLength(c)); + assertFalse(lock.hasWaiters(c)); + assertEquals(0, lock.getWaitQueueLength(c)); c.await(); lock.writeLock().unlock(); }}); - t.start(); Thread.sleep(SHORT_DELAY_MS); lock.writeLock().lock(); assertTrue(lock.hasWaiters(c)); @@ -1272,8 +1233,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); } @@ -1286,7 +1246,7 @@ public class ReentrantReadWriteLockTest Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); + assertTrue(lock.getWaitingThreads(c).isEmpty()); c.await(); lock.writeLock().unlock(); }}); @@ -1294,7 +1254,7 @@ public class ReentrantReadWriteLockTest Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); + assertFalse(lock.getWaitingThreads(c).isEmpty()); c.await(); lock.writeLock().unlock(); }}); @@ -1317,10 +1277,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); } /**