--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2009/11/21 02:07:27 1.35 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2011/03/15 19:47:07 1.44 @@ -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. */ @@ -9,12 +9,13 @@ import junit.framework.*; import java.util.concurrent.locks.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.io.*; 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); @@ -214,10 +215,11 @@ public class ReentrantReadWriteLockTest lock.writeLock().lock(); Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); + lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); }}); t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); lock.writeLock().unlock(); t.join(); @@ -250,10 +252,11 @@ public class ReentrantReadWriteLockTest lock.writeLock().lock(); Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); + lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); }}); t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -265,11 +268,10 @@ public class ReentrantReadWriteLockTest public void testWriteTryLockWhenLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertFalse(lock.writeLock().tryLock()); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertFalse(lock.writeLock().tryLock()); + }}); t.start(); t.join(); @@ -282,11 +284,10 @@ public class ReentrantReadWriteLockTest public void testReadTryLockWhenLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertFalse(lock.readLock().tryLock()); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertFalse(lock.readLock().tryLock()); + }}); t.start(); t.join(); @@ -299,12 +300,11 @@ public class ReentrantReadWriteLockTest public void testMultipleReadLocks() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertTrue(lock.readLock().tryLock()); - lock.readLock().unlock(); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + }}); t.start(); t.join(); @@ -317,18 +317,16 @@ public class ReentrantReadWriteLockTest public void testWriteAfterMultipleReadLocks() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t1 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); t1.start(); t2.start(); @@ -346,18 +344,16 @@ public class ReentrantReadWriteLockTest public void testReadAfterWriteLock() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); t1.start(); t2.start(); @@ -387,18 +383,16 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLock2() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); t1.start(); t2.start(); @@ -415,24 +409,22 @@ public class ReentrantReadWriteLockTest } /** - * 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 Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); t1.start(); t2.start(); @@ -450,24 +442,22 @@ public class ReentrantReadWriteLockTest /** - * 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 Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); t1.start(); t2.start(); @@ -502,18 +492,16 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLockFair2() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.readLock().lock(); - lock.readLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.readLock().lock(); + lock.readLock().unlock(); + }}); t1.start(); t2.start(); @@ -537,18 +525,16 @@ public class ReentrantReadWriteLockTest public void testReadHoldingWriteLockFair3() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); t1.start(); t2.start(); @@ -572,26 +558,24 @@ public class ReentrantReadWriteLockTest public void testWriteHoldingWriteLockFair4() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - lock.writeLock().lock(); - lock.writeLock().unlock(); - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() { + lock.writeLock().lock(); + lock.writeLock().unlock(); + }}); + Thread t2 = new Thread(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(); @@ -609,12 +593,11 @@ public class ReentrantReadWriteLockTest public void testTryLockWhenReadLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertTrue(lock.readLock().tryLock()); - lock.readLock().unlock(); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + }}); t.start(); t.join(); @@ -629,11 +612,10 @@ public class ReentrantReadWriteLockTest public void testWriteTryLockWhenReadLocked() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertFalse(lock.writeLock().tryLock()); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertFalse(lock.writeLock().tryLock()); + }}); t.start(); t.join(); @@ -647,12 +629,11 @@ public class ReentrantReadWriteLockTest public void testTryLockWhenReadLockedFair() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.readLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertTrue(lock.readLock().tryLock()); - lock.readLock().unlock(); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + }}); t.start(); t.join(); @@ -667,11 +648,10 @@ public class ReentrantReadWriteLockTest public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); lock.readLock().lock(); - Thread t = new Thread(new Runnable() { - public void run() { - threadAssertFalse(lock.writeLock().tryLock()); - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + assertFalse(lock.writeLock().tryLock()); + }}); t.start(); t.join(); @@ -688,7 +668,7 @@ public class ReentrantReadWriteLockTest lock.writeLock().lock(); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); + assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); }}); t.start(); @@ -705,7 +685,7 @@ public class ReentrantReadWriteLockTest lock.writeLock().lock(); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); + assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); }}); t.start(); @@ -735,7 +715,7 @@ public class ReentrantReadWriteLockTest } /** - * 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(); @@ -791,13 +771,13 @@ 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(); final Condition c = lock.writeLock().newCondition(); lock.writeLock().lock(); - assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); lock.writeLock().unlock(); } @@ -919,7 +899,7 @@ public class ReentrantReadWriteLockTest Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { lock.writeLock().lock(); - c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); + c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); lock.writeLock().unlock(); }}); @@ -1244,8 +1224,8 @@ public class ReentrantReadWriteLockTest Thread t = new Thread(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(); }}); @@ -1275,8 +1255,8 @@ public class ReentrantReadWriteLockTest Thread t = new Thread(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(); }}); @@ -1307,7 +1287,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(); }}); @@ -1315,7 +1295,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(); }});