FileDocCategorySizeDatePackage
ReentrantReadWriteLockTest.javaAPI DocAndroid 1.5 API42602Wed May 06 22:41:02 BST 2009tests.api.java.util.concurrent

ReentrantReadWriteLockTest

public class ReentrantReadWriteLockTest extends JSR166TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run (suite());        
    
public static junit.framework.Testsuite()

        return new TestSuite(ReentrantReadWriteLockTest.class);
    
public voidtestAwait()
await returns when signalled

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            c.signal();
            lock.writeLock().unlock();
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwaitNanos_Interrupt()
awaitNanos is interruptible

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
                        lock.writeLock().unlock();
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {
                    }
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwaitNanos_Timeout()
awaitNanos without a signal times out

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        try {
            lock.writeLock().lock();
            long t = c.awaitNanos(100);
            assertTrue(t <= 0);
            lock.writeLock().unlock();
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwaitUninterruptibly()
awaitUninterruptibly doesn't abort on interrupt

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    lock.writeLock().lock();
                    c.awaitUninterruptibly();
                    lock.writeLock().unlock();
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            lock.writeLock().lock();
            c.signal();
            lock.writeLock().unlock();
            assert(t.isInterrupted());
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwaitUntil_Interrupt()
awaitUntil is interruptible

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        java.util.Date d = new java.util.Date();
                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
                        lock.writeLock().unlock();
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {
                    }
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwaitUntil_Timeout()
awaitUntil without a signal times out

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        try {
            lock.writeLock().lock();
            java.util.Date d = new java.util.Date();
            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
            lock.writeLock().unlock();
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwait_IllegalMonitor()
Calling await without holding lock throws IllegalMonitorStateException

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        try {
            c.await();
            shouldThrow();
        }
        catch (IllegalMonitorStateException success) {
        }
        catch (Exception ex) {
            shouldThrow();
        }
    
public voidtestAwait_Interrupt()
await is interruptible

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        c.await();
                        lock.writeLock().unlock();
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {
                    }
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwait_Timeout()
timed await without a signal times out

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        try {
            lock.writeLock().lock();
            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
            lock.writeLock().unlock();
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestConstructor()
Constructor sets given fairness, and is in unlocked state

 
        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
        assertFalse(rl.isFair());
        assertFalse(rl.isWriteLocked());
        assertEquals(0, rl.getReadLockCount());
        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
        assertTrue(r2.isFair());
        assertFalse(r2.isWriteLocked());
        assertEquals(0, r2.getReadLockCount());
    
public voidtestFairLock()
locking an unlocked fair lock succeeds

 
        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
        rl.writeLock().lock();
        assertTrue(rl.isWriteLocked());
        assertTrue(rl.isWriteLockedByCurrentThread());
        assertEquals(0, rl.getReadLockCount());
        rl.writeLock().unlock();
        assertFalse(rl.isWriteLocked());
        assertFalse(rl.isWriteLockedByCurrentThread());
        assertEquals(0, rl.getReadLockCount());
        rl.readLock().lock();
        assertFalse(rl.isWriteLocked());
        assertFalse(rl.isWriteLockedByCurrentThread());
        assertEquals(1, rl.getReadLockCount());
        rl.readLock().unlock();
        assertFalse(rl.isWriteLocked());
        assertFalse(rl.isWriteLockedByCurrentThread());
        assertEquals(0, rl.getReadLockCount());
    
public voidtestGetHoldCount()
getWriteHoldCount returns number of recursive holds

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        for(int i = 1; i <= SIZE; i++) {
            lock.writeLock().lock();
            assertEquals(i,lock.getWriteHoldCount());
        }
        for(int i = SIZE; i > 0; i--) {
            lock.writeLock().unlock();
            assertEquals(i-1,lock.getWriteHoldCount());
        }
    
public voidtestGetQueueLength()
getQueueLength reports number of waiting threads

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertEquals(0, lock.getQueueLength());
            lock.writeLock().lock();
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(1, lock.getQueueLength());
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(2, lock.getQueueLength());
            t1.interrupt();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(1, lock.getQueueLength());
            lock.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(0, lock.getQueueLength());
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestGetQueuedThreads()
getQueuedThreads includes waiting threads

 
        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertTrue(lock.getQueuedThreads().isEmpty());
            lock.writeLock().lock();
            assertTrue(lock.getQueuedThreads().isEmpty());
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.getQueuedThreads().contains(t1));
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.getQueuedThreads().contains(t1));
            assertTrue(lock.getQueuedThreads().contains(t2));
            t1.interrupt();
            Thread.sleep(SHORT_DELAY_MS);
            assertFalse(lock.getQueuedThreads().contains(t1));
            assertTrue(lock.getQueuedThreads().contains(t2));
            lock.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.getQueuedThreads().isEmpty());
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestGetWaitQueueLength()
getWaitQueueLength returns number of waiting threads

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        final Condition c = (lock.writeLock().newCondition());
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        threadAssertFalse(lock.hasWaiters(c));
                        threadAssertEquals(0, lock.getWaitQueueLength(c));
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            assertTrue(lock.hasWaiters(c));
            assertEquals(1, lock.getWaitQueueLength(c));
            c.signal();
            lock.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            assertFalse(lock.hasWaiters(c));
            assertEquals(0, lock.getWaitQueueLength(c));
            lock.writeLock().unlock();
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitQueueLengthIAE()
getWaitQueueLength throws IAE if not owned

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        final Condition c = (lock.writeLock().newCondition());
        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
        try {
            lock2.getWaitQueueLength(c);
            shouldThrow();
        } catch (IllegalArgumentException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitQueueLengthIMSE()
getWaitQueueLength throws IMSE if not locked

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        final Condition c = (lock.writeLock().newCondition());
        try {
            lock.getWaitQueueLength(c);
            shouldThrow();
        } catch (IllegalMonitorStateException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitQueueLengthNPE()
getWaitQueueLength throws NPE if null

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        try {
            lock.getWaitQueueLength(null);
            shouldThrow();
        } catch (NullPointerException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitingThreads()
getWaitingThreads returns only and all waiting threads

        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t1 = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        Thread t2 = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        try {
            lock.writeLock().lock();
            assertTrue(lock.getWaitingThreads(c).isEmpty());
            lock.writeLock().unlock();
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            assertTrue(lock.hasWaiters(c));
            assertTrue(lock.getWaitingThreads(c).contains(t1));
            assertTrue(lock.getWaitingThreads(c).contains(t2));
            c.signalAll();
            lock.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            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());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitingThreadsIAE()
getWaitingThreads throws IAE if not owned

        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();        
        final Condition c = (lock.writeLock().newCondition());
        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();        
        try {
            lock2.getWaitingThreads(c);
            shouldThrow();
        } catch (IllegalArgumentException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitingThreadsIMSE()
getWaitingThreads throws IMSE if not locked

        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();        
        final Condition c = (lock.writeLock().newCondition());
        try {
            lock.getWaitingThreads(c);
            shouldThrow();
        } catch (IllegalMonitorStateException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitingThreadsNPE()
getWaitingThreads throws NPE if null

        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
        try {
            lock.getWaitingThreads(null);
            shouldThrow();
        } catch (NullPointerException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestHasQueuedThread()
hasQueuedThread reports whether a thread is queued.

 
        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
        try {
            assertFalse(sync.hasQueuedThread(t1));
            assertFalse(sync.hasQueuedThread(t2));
            sync.writeLock().lock();
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(sync.hasQueuedThread(t1));
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(sync.hasQueuedThread(t1));
            assertTrue(sync.hasQueuedThread(t2));
            t1.interrupt();
            Thread.sleep(SHORT_DELAY_MS);
            assertFalse(sync.hasQueuedThread(t1));
            assertTrue(sync.hasQueuedThread(t2));
            sync.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            assertFalse(sync.hasQueuedThread(t1));
            Thread.sleep(SHORT_DELAY_MS);
            assertFalse(sync.hasQueuedThread(t2));
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestHasQueuedThreadNPE()
hasQueuedThread(null) throws NPE

 
        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
        try {
            sync.hasQueuedThread(null);
            shouldThrow();
        } catch (NullPointerException success) {
        }
    
public voidtestHasWaiters()
hasWaiters returns true when a thread is waiting, else false

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        final Condition c = (lock.writeLock().newCondition());
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        threadAssertFalse(lock.hasWaiters(c));
                        threadAssertEquals(0, lock.getWaitQueueLength(c));
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            assertTrue(lock.hasWaiters(c));
            assertEquals(1, lock.getWaitQueueLength(c));
            c.signal();
            lock.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            lock.writeLock().lock();
            assertFalse(lock.hasWaiters(c));
            assertEquals(0, lock.getWaitQueueLength(c));
            lock.writeLock().unlock();
            t.join(SHORT_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestHasWaitersIAE()
hasWaiters throws IAE if not owned

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        final Condition c = (lock.writeLock().newCondition());
        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
        try {
            lock2.hasWaiters(c);
            shouldThrow();
        } catch (IllegalArgumentException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestHasWaitersIMSE()
hasWaiters throws IMSE if not locked

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        final Condition c = (lock.writeLock().newCondition());
        try {
            lock.hasWaiters(c);
            shouldThrow();
        } catch (IllegalMonitorStateException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestHasWaitersNPE()
hasWaiters throws NPE if null

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        try {
            lock.hasWaiters(null);
            shouldThrow();
        } catch (NullPointerException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestLock()
write-locking and read-locking an unlocked lock succeed

 
        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
        rl.writeLock().lock();
        assertTrue(rl.isWriteLocked());
        assertTrue(rl.isWriteLockedByCurrentThread());
        assertEquals(0, rl.getReadLockCount());
        rl.writeLock().unlock();
        assertFalse(rl.isWriteLocked());
        assertFalse(rl.isWriteLockedByCurrentThread());
        assertEquals(0, rl.getReadLockCount());
        rl.readLock().lock();
        assertFalse(rl.isWriteLocked());
        assertFalse(rl.isWriteLockedByCurrentThread());
        assertEquals(1, rl.getReadLockCount());
        rl.readLock().unlock();
        assertFalse(rl.isWriteLocked());
        assertFalse(rl.isWriteLockedByCurrentThread());
        assertEquals(0, rl.getReadLockCount());
    
public voidtestMultipleReadLocks()
Multiple threads can hold a read lock when not write-locked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.readLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    threadAssertTrue(lock.readLock().tryLock());
                    lock.readLock().unlock();
                }
            });
        try {
            t.start();
            t.join();
            lock.readLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestReadAfterWriteLock()
Readlocks succeed after a writing thread unlocks

 
        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();
                }
            });

        try {
            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());
           
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestReadLockInterruptibly()
read lockInterruptibly succeeds if lock free else is interruptible

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        try {
            lock.writeLock().lockInterruptibly();
        } catch(Exception e) {
            unexpectedException();
        }
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.readLock().lockInterruptibly();
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {
                    }
                }
            });
        try {
            t.start();
            t.interrupt();
            t.join();
            lock.writeLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestReadLockInterruptibly_Interrupted()
read-lockInterruptibly is interruptible

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        lock.readLock().lockInterruptibly();
                    } catch(InterruptedException success){}
                }
            });
        try {
            t.start();
            t.interrupt();
            lock.writeLock().unlock();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestReadLockToString()
readLock.toString indicates current lock state

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        String us = lock.readLock().toString();
        assertTrue(us.indexOf("Read locks = 0") >= 0);
        lock.readLock().lock();
        lock.readLock().lock();
        String rs = lock.readLock().toString();
        assertTrue(rs.indexOf("Read locks = 2") >= 0);
    
public voidtestReadTryLockWhenLocked()
read-tryLock fails if locked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    threadAssertFalse(lock.readLock().tryLock());
                }
            });
        try {
            t.start();
            t.join();
            lock.writeLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestReadTryLock_Interrupted()
timed read-tryLock is interruptible

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
                        threadShouldThrow();
                    } catch(InterruptedException success){}
                }
            });
        try {
            t.start();
            t.interrupt();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestReadTryLock_Timeout()
read timed tryLock times out if write-locked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
                    } catch (Exception ex) {
                        threadUnexpectedException();
                    }
                }
            });
        try {
            t.start();
            t.join();
            lock.writeLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestSerialization()
A serialized lock deserializes as unlocked

        ReentrantReadWriteLock l = new ReentrantReadWriteLock();
        l.readLock().lock();
        l.readLock().unlock();

        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
            out.writeObject(l);
            out.close();

            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
            ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
            r.readLock().lock();
            r.readLock().unlock();
        } catch(Exception e){
            e.printStackTrace();
            unexpectedException();
        }
    
public voidtestSignalAll()
signalAll wakes up all threads

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        Thread t1 = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        Thread t2 = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lock();
                        c.await();
                        lock.writeLock().unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        try {
            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());
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestSignal_IllegalMonitor()
Calling signal without holding lock throws IllegalMonitorStateException

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();        
        final Condition c = lock.writeLock().newCondition();
        try {
            c.signal();
            shouldThrow();
        }
        catch (IllegalMonitorStateException success) {
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestToString()
toString indicates current lock state

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        String us = lock.toString();
        assertTrue(us.indexOf("Write locks = 0") >= 0);
        assertTrue(us.indexOf("Read locks = 0") >= 0);
        lock.writeLock().lock();
        String ws = lock.toString();
        assertTrue(ws.indexOf("Write locks = 1") >= 0);
        assertTrue(ws.indexOf("Read locks = 0") >= 0);
        lock.writeLock().unlock();
        lock.readLock().lock();
        lock.readLock().lock();
        String rs = lock.toString();
        assertTrue(rs.indexOf("Write locks = 0") >= 0);
        assertTrue(rs.indexOf("Read locks = 2") >= 0);
    
public voidtestTryLockWhenReadLocked()
Read tryLock succeeds if readlocked but not writelocked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.readLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    threadAssertTrue(lock.readLock().tryLock());
                    lock.readLock().unlock();
                }
            });
        try {
            t.start();
            t.join();
            lock.readLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestUnlock_IllegalMonitorStateException()
write-unlocking an unlocked lock throws IllegalMonitorStateException

 
        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
        try {
            rl.writeLock().unlock();
            shouldThrow();
        } catch(IllegalMonitorStateException success){}
    
public voidtestWriteAfterMultipleReadLocks()
A writelock succeeds after reading threads unlock

 
        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();
                }
            });

        try {
            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());
           
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestWriteLockInterruptibly()
write lockInterruptibly succeeds if lock free else is interruptible

        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        try {
            lock.writeLock().lockInterruptibly();
        } catch(Exception e) {
            unexpectedException();
        }
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.writeLock().lockInterruptibly();
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {
                    }
                }
            });
        try {
            t.start();
            t.interrupt();
            t.join();
            lock.writeLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestWriteLockInterruptibly_Interrupted()
write-lockInterruptibly is interruptible

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        lock.writeLock().lockInterruptibly();
                        lock.writeLock().unlock();
                        lock.writeLock().lockInterruptibly();
                        lock.writeLock().unlock();
                    } catch(InterruptedException success){}
                }
            });
        try {
            lock.writeLock().lock();
            t.start();
            t.interrupt();
            lock.writeLock().unlock();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestWriteLockToString()
writeLock.toString indicates current lock state

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        String us = lock.writeLock().toString();
        assertTrue(us.indexOf("Unlocked") >= 0);
        lock.writeLock().lock();
        String ls = lock.writeLock().toString();
        assertTrue(ls.indexOf("Locked") >= 0);
    
public voidtestWriteTryLockWhenLocked()
write-tryLock fails if locked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    threadAssertFalse(lock.writeLock().tryLock());
                }
            });
        try {
            t.start();
            t.join();
            lock.writeLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestWriteTryLockWhenReadLocked()
write tryLock fails when readlocked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.readLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    threadAssertFalse(lock.writeLock().tryLock());
                }
            });
        try {
            t.start();
            t.join();
            lock.readLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestWriteTryLock_Interrupted()
timed write-tryLock is interruptible

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
                    } catch(InterruptedException success){}
                }
            });
        try {
            t.start();
            t.interrupt();
            lock.writeLock().unlock();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestWriteTryLock_Timeout()
write timed tryLock times out if locked

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.writeLock().lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
                    } catch (Exception ex) {
                        threadUnexpectedException();
                    }
                }
            });
        try {
            t.start();
            t.join();
            lock.writeLock().unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtesthasQueuedThreads()
hasQueuedThreads reports whether there are waiting threads

 
        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertFalse(lock.hasQueuedThreads());
            lock.writeLock().lock();
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.hasQueuedThreads());
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.hasQueuedThreads());
            t1.interrupt();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.hasQueuedThreads());
            lock.writeLock().unlock();
            Thread.sleep(SHORT_DELAY_MS);
            assertFalse(lock.hasQueuedThreads());
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }