--- jsr166/src/test/loops/ReadHoldingWriteLock.java 2005/05/02 19:19:38 1.1 +++ jsr166/src/test/loops/ReadHoldingWriteLock.java 2011/10/25 20:29:12 1.6 @@ -1,3 +1,8 @@ +/* + * 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/publicdomain/zero/1.0/ + */ import java.util.concurrent.locks.*; class ReadHoldingWriteLock { @@ -21,21 +26,21 @@ class ReadHoldingWriteLock { /** * Readlocks succeed after a writing thread unlocks */ - public void testReadAfterWriteLock() throws Exception { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { + public void testReadAfterWriteLock() throws Exception { + 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() { + } + }); + Thread t2 = new Thread(new Runnable() { public void run() { lock.readLock().lock(); lock.readLock().unlock(); - } - }); + } + }); t1.start(); t2.start(); @@ -45,40 +50,40 @@ class ReadHoldingWriteLock { t2.join(MEDIUM_DELAY_MS); assertTrue(!t1.isAlive()); assertTrue(!t2.isAlive()); - - } + + } /** * Read trylock succeeds if write locked by current thread */ - public void testReadHoldingWriteLock()throws Exception { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - lock.writeLock().lock(); + public void testReadHoldingWriteLock()throws Exception { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + lock.writeLock().lock(); assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); lock.writeLock().unlock(); - } + } /** * Read lock succeeds if write locked by current thread even if * other threads are waiting */ - public void testReadHoldingWriteLock2() throws Exception{ - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { + public void testReadHoldingWriteLock2() throws Exception { + 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() { + } + }); + Thread t2 = new Thread(new Runnable() { public void run() { lock.readLock().lock(); lock.readLock().unlock(); - } - }); - + } + }); + t1.start(); t2.start(); lock.readLock().lock(); @@ -91,38 +96,38 @@ class ReadHoldingWriteLock { t2.join(MEDIUM_DELAY_MS); assertTrue(!t1.isAlive()); assertTrue(!t2.isAlive()); - } + } /** * Fair Read trylock succeeds if write locked by current thread */ - public void testReadHoldingWriteLockFair() throws Exception{ - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); - lock.writeLock().lock(); + public void testReadHoldingWriteLockFair() throws Exception { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.writeLock().lock(); assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); lock.writeLock().unlock(); - } + } /** * Fair Read lock succeeds if write locked by current thread even if * other threads are waiting */ - public void testReadHoldingWriteLockFair2() throws Exception { - final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); - lock.writeLock().lock(); - Thread t1 = new Thread(new Runnable() { + public void testReadHoldingWriteLockFair2() throws Exception { + 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() { + } + }); + Thread t2 = new Thread(new Runnable() { public void run() { lock.readLock().lock(); lock.readLock().unlock(); - } - }); + } + }); t1.start(); t2.start(); @@ -136,7 +141,7 @@ class ReadHoldingWriteLock { t2.join(MEDIUM_DELAY_MS); assertTrue(!t1.isAlive()); assertTrue(!t2.isAlive()); - - } + + } }