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

ReentrantLockTest

public class ReentrantLockTest 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(ReentrantLockTest.class);
    
public voidtestAwait()
await returns when signalled

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

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

        final ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.lock();
                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
                        lock.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 ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        try {
            lock.lock();
            long t = c.awaitNanos(100);
            assertTrue(t <= 0);
            lock.unlock();
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwaitUninterruptibly()
awaitUninterruptibly doesn't abort on interrupt

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

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

        final ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.lock();
                        java.util.Date d = new java.util.Date();
                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
                        lock.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 ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        try {
            lock.lock();
            java.util.Date d = new java.util.Date();
            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
            lock.unlock();
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestAwait_IllegalMonitor()
Calling await without holding lock throws IllegalMonitorStateException

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

        final ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.lock();
                        c.await();
                        lock.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 ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        try {
            lock.lock();
            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
            lock.unlock();
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestConstructor()
Constructor sets given fairness

 
        ReentrantLock rl = new ReentrantLock();
        assertFalse(rl.isFair());
        ReentrantLock r2 = new ReentrantLock(true);
        assertTrue(r2.isFair());
    
public voidtestFairLock()
locking an unlocked fair lock succeeds

 
        ReentrantLock rl = new ReentrantLock(true);
        rl.lock();
        assertTrue(rl.isLocked());
        rl.unlock();
    
public voidtestGetHoldCount()
getHoldCount returns number of recursive holds

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

 
        final ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertEquals(0, lock.getQueueLength());
            lock.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.unlock();
            Thread.sleep(SHORT_DELAY_MS);
            assertEquals(0, lock.getQueueLength());
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestGetQueueLength_fair()
getQueueLength reports number of waiting threads

 
        final ReentrantLock lock = new ReentrantLock(true);
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertEquals(0, lock.getQueueLength());
            lock.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.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 PublicReentrantLock lock = new PublicReentrantLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertTrue(lock.getQueuedThreads().isEmpty());
            lock.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.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 ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        Thread t1 = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.lock();
                        threadAssertFalse(lock.hasWaiters(c));
                        threadAssertEquals(0, lock.getWaitQueueLength(c));
                        c.await();
                        lock.unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

        Thread t2 = new Thread(new Runnable() { 
                public void run() {
                    try {
                        lock.lock();
                        threadAssertTrue(lock.hasWaiters(c));
                        threadAssertEquals(1, lock.getWaitQueueLength(c));
                        c.await();
                        lock.unlock();
                    }
                    catch(InterruptedException e) {
                        threadUnexpectedException();
                    }
                }
            });

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

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

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

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

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

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

        try {
            lock.lock();
            assertTrue(lock.getWaitingThreads(c).isEmpty());
            lock.unlock();
            t1.start();
            Thread.sleep(SHORT_DELAY_MS);
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            lock.lock();
            assertTrue(lock.hasWaiters(c));
            assertTrue(lock.getWaitingThreads(c).contains(t1));
            assertTrue(lock.getWaitingThreads(c).contains(t2));
            c.signalAll();
            lock.unlock();
            Thread.sleep(SHORT_DELAY_MS);
            lock.lock();
            assertFalse(lock.hasWaiters(c));
            assertTrue(lock.getWaitingThreads(c).isEmpty());
            lock.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 PublicReentrantLock lock = new PublicReentrantLock();        
        final Condition c = (lock.newCondition());
        final PublicReentrantLock lock2 = new PublicReentrantLock();        
        try {
            lock2.getWaitingThreads(c);
            shouldThrow();
        } catch (IllegalArgumentException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetWaitingThreadsIMSE()
getWaitingThreads throws IMSE if not locked

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

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

 
        final ReentrantLock sync = new ReentrantLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
        try {
            assertFalse(sync.hasQueuedThread(t1));
            assertFalse(sync.hasQueuedThread(t2));
            sync.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.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 ReentrantLock sync = new ReentrantLock();
        try {
            sync.hasQueuedThread(null);
            shouldThrow();
        } catch (NullPointerException success) {
        }
    
public voidtestHasWaiters()
hasWaiters returns true when a thread is waiting, else false

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

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

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

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

        final ReentrantLock lock = new ReentrantLock();
        try {
            lock.hasWaiters(null);
            shouldThrow();
        } catch (NullPointerException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestInterruptedException2()
timed tryLock is interruptible.

 
        final ReentrantLock lock = new ReentrantLock();
        lock.lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
                        threadShouldThrow();
                    } catch(InterruptedException success){}
                }
            });
        try {
            t.start();
            t.interrupt();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestIsLocked()
isLocked is true when locked and false when not

        final ReentrantLock lock = new ReentrantLock();
        lock.lock();
        assertTrue(lock.isLocked());
        lock.unlock();
        assertFalse(lock.isLocked());
        Thread t = new Thread(new Runnable() { 
                public void run() {
                    lock.lock();
                    try {
                        Thread.sleep(SMALL_DELAY_MS);
                    }
                    catch(Exception e) {
                        threadUnexpectedException();
                    }
                    lock.unlock();
                }
            });
        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(lock.isLocked());
            t.join();
            assertFalse(lock.isLocked());
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestLock()
locking an unlocked lock succeeds

 
        ReentrantLock rl = new ReentrantLock();
        rl.lock();
        assertTrue(rl.isLocked());
        rl.unlock();
    
public voidtestLockInterruptibly1()
lockInterruptibly is interruptible.

 
        final ReentrantLock lock = new ReentrantLock();
        lock.lock();
        Thread t = new Thread(new InterruptedLockRunnable(lock));
        try {
            t.start();
            t.interrupt();
            lock.unlock();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestLockInterruptibly2()
lockInterruptibly succeeds when unlocked, else is interruptible

        final ReentrantLock lock = new ReentrantLock();        
        try {
            lock.lockInterruptibly();
        } catch(Exception e) {
            unexpectedException();
        }
        Thread t = new Thread(new InterruptedLockRunnable(lock));
        try {
            t.start();
            t.interrupt();
            assertTrue(lock.isLocked());
            assertTrue(lock.isHeldByCurrentThread());
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestSerialization()
A serialized lock deserializes as unlocked

        ReentrantLock l = new ReentrantLock();
        l.lock();
        l.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));
            ReentrantLock r = (ReentrantLock) in.readObject();
            r.lock();
            r.unlock();
        } catch(Exception e){
            e.printStackTrace();
            unexpectedException();
        }
    
public voidtestSignalAll()
signalAll wakes up all threads

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

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

        try {
            t1.start();
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            lock.lock();
            c.signalAll();
            lock.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 ReentrantLock lock = new ReentrantLock();        
        final Condition c = lock.newCondition();
        try {
            c.signal();
            shouldThrow();
        }
        catch (IllegalMonitorStateException success) {
        }
        catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestToString()
toString indicates current lock state

        ReentrantLock lock = new ReentrantLock();
        String us = lock.toString();
        assertTrue(us.indexOf("Unlocked") >= 0);
        lock.lock();
        String ls = lock.toString();
        assertTrue(ls.indexOf("Locked") >= 0);
    
public voidtestTryLock()
tryLock on an unlocked lock succeeds

 
        ReentrantLock rl = new ReentrantLock();
        assertTrue(rl.tryLock());
        assertTrue(rl.isLocked());
        rl.unlock();
    
public voidtestTryLockWhenLocked()
TryLock on a locked lock fails

 
        final ReentrantLock lock = new ReentrantLock();
        lock.lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    threadAssertFalse(lock.tryLock());
                }
            });
        try {
            t.start();
            t.join();
            lock.unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestTryLock_Timeout()
Timed tryLock on a locked lock times out

 
        final ReentrantLock lock = new ReentrantLock();
        lock.lock();
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
                    } catch (Exception ex) {
                        threadUnexpectedException();
                    }
                }
            });
        try {
            t.start();
            t.join();
            lock.unlock();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestUnlock_IllegalMonitorStateException()
Unlocking an unlocked lock throws IllegalMonitorStateException

 
        ReentrantLock rl = new ReentrantLock();
        try {
            rl.unlock();
            shouldThrow();

        } catch(IllegalMonitorStateException success){}
    
public voidtesthasQueuedThreads()
hasQueuedThreads reports whether there are waiting threads

 
        final ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
        try {
            assertFalse(lock.hasQueuedThreads());
            lock.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.unlock();
            Thread.sleep(SHORT_DELAY_MS);
            assertFalse(lock.hasQueuedThreads());
            t1.join();
            t2.join();
        } catch(Exception e){
            unexpectedException();
        }