--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/02 00:03:34 1.46 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/05/02 01:07:15 1.54 @@ -59,12 +59,13 @@ public class ReentrantReadWriteLockTest } /** - * Releases lock, checking that it had a hold count of 1. + * Releases write lock, checking that it had a hold count of 1. */ - void releaseLock(ReentrantReadWriteLock.WriteLock lock) { - assertTrue(lock.isHeldByCurrentThread()); - lock.unlock(); - assertFalse(lock.isHeldByCurrentThread()); + void releaseWriteLock(ReentrantReadWriteLock lock) { + ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); + assertTrue(writeLock.isHeldByCurrentThread()); + writeLock.unlock(); + assertFalse(writeLock.isHeldByCurrentThread()); } /** @@ -207,8 +208,8 @@ public class ReentrantReadWriteLockTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); - releaseLock(lock.writeLock()); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -224,8 +225,8 @@ public class ReentrantReadWriteLockTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - lock.writeLock().unlock(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -241,9 +242,8 @@ public class ReentrantReadWriteLockTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - lock.writeLock().unlock(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -259,7 +259,8 @@ public class ReentrantReadWriteLockTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } @@ -274,8 +275,8 @@ public class ReentrantReadWriteLockTest assertFalse(lock.writeLock().tryLock()); }}); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -289,8 +290,8 @@ public class ReentrantReadWriteLockTest assertFalse(lock.readLock().tryLock()); }}); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -305,7 +306,7 @@ public class ReentrantReadWriteLockTest lock.readLock().unlock(); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -328,10 +329,8 @@ public class ReentrantReadWriteLockTest 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); } /** @@ -352,11 +351,9 @@ public class ReentrantReadWriteLockTest }}); Thread.sleep(SHORT_DELAY_MS); - lock.writeLock().unlock(); - t1.join(MEDIUM_DELAY_MS); - t2.join(MEDIUM_DELAY_MS); - assertTrue(!t1.isAlive()); - assertTrue(!t2.isAlive()); + releaseWriteLock(lock); + awaitTermination(t1, LONG_DELAY_MS); + awaitTermination(t2, LONG_DELAY_MS); } /** @@ -394,10 +391,8 @@ public class ReentrantReadWriteLockTest 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); } /** @@ -424,10 +419,8 @@ public class ReentrantReadWriteLockTest 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); } @@ -455,10 +448,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); } @@ -497,10 +488,8 @@ public class ReentrantReadWriteLockTest 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); } @@ -528,10 +517,8 @@ public class ReentrantReadWriteLockTest 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); } @@ -562,10 +549,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); } @@ -581,7 +566,7 @@ public class ReentrantReadWriteLockTest lock.readLock().unlock(); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -596,7 +581,7 @@ public class ReentrantReadWriteLockTest assertFalse(lock.writeLock().tryLock()); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -613,7 +598,7 @@ public class ReentrantReadWriteLockTest lock.readLock().unlock(); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -630,7 +615,7 @@ public class ReentrantReadWriteLockTest assertFalse(lock.writeLock().tryLock()); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); lock.readLock().unlock(); } @@ -647,7 +632,7 @@ public class ReentrantReadWriteLockTest assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); assertTrue(lock.writeLock().isHeldByCurrentThread()); lock.writeLock().unlock(); } @@ -663,7 +648,7 @@ public class ReentrantReadWriteLockTest assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); }}); - t.join(); + awaitTermination(t, LONG_DELAY_MS); assertTrue(lock.writeLock().isHeldByCurrentThread()); lock.writeLock().unlock(); } @@ -682,9 +667,8 @@ public class ReentrantReadWriteLockTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - Thread.sleep(SHORT_DELAY_MS); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -700,8 +684,8 @@ public class ReentrantReadWriteLockTest Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); - lock.writeLock().unlock(); + awaitTermination(t, LONG_DELAY_MS); + releaseWriteLock(lock); } /** @@ -782,8 +766,7 @@ public class ReentrantReadWriteLockTest 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 */ @@ -836,9 +819,8 @@ public class ReentrantReadWriteLockTest lock.writeLock().unlock(); } - thread.join(); + awaitTermination(thread, LONG_DELAY_MS); assertTrue(thread.interrupted); - assertFalse(thread.isAlive()); } /** @@ -847,17 +829,22 @@ public class ReentrantReadWriteLockTest public void testAwait_Interrupt() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); + 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(); } }}); - 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()); } /** @@ -866,17 +853,22 @@ public class ReentrantReadWriteLockTest public void testAwaitNanos_Interrupt() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); + 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(); } }}); - 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()); } /** @@ -885,18 +877,23 @@ public class ReentrantReadWriteLockTest public void testAwaitUntil_Interrupt() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); final Condition c = lock.writeLock().newCondition(); + 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(); } }}); - 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()); } /** @@ -923,10 +920,8 @@ public class ReentrantReadWriteLockTest 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); } /** @@ -970,8 +965,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); } /** @@ -1011,8 +1006,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); } @@ -1037,8 +1032,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); } /** @@ -1065,8 +1060,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); } /** @@ -1207,8 +1202,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); } /** @@ -1237,8 +1231,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); } @@ -1282,10 +1275,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); } /**