reentrantlocktest.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,089 行 · 第 1/3 页

JAVA
1,089
字号
/*
 * 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
 * Other contributors include Andrew Wright, Jeffrey Hayes,
 * Pat Fisher, Mike Judd.
 */

import junit.framework.*;
import edu.emory.mathcs.backport.java.util.concurrent.locks.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import edu.emory.mathcs.backport.java.util.*;
import java.io.*;
import java.util.*;

public class ReentrantLockTest extends JSR166TestCase {
    public static void main(String[] args) {
        junit.textui.TestRunner.run (suite());
    }
    public static Test suite() {
        return new TestSuite(ReentrantLockTest.class);
    }

    /**
     * A runnable calling lockInterruptibly
     */
    class InterruptibleLockRunnable implements Runnable {
        final ReentrantLock lock;
        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
        public void run() {
            try {
                lock.lockInterruptibly();
            } catch(InterruptedException success){}
        }
    }


    /**
     * A runnable calling lockInterruptibly that expects to be
     * interrupted
     */
    class InterruptedLockRunnable implements Runnable {
        final ReentrantLock lock;
        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
        public void run() {
            try {
                lock.lockInterruptibly();
                threadShouldThrow();
            } catch(InterruptedException success){}
        }
    }

    /**
     * Subclass to expose protected methods
     */
    static class PublicReentrantLock extends ReentrantLock {
//        PublicReentrantLock() { super(); }
        PublicReentrantLock() { super(true); }
        public Collection getQueuedThreads() {
            return super.getQueuedThreads();
        }
//        public Collection getWaitingThreads(Condition c) {
//            return super.getWaitingThreads(c);
//        }


    }

    /**
     * Constructor sets given fairness
     */
    public void testConstructor() {
        ReentrantLock rl = new ReentrantLock();
        assertFalse(rl.isFair());
        ReentrantLock r2 = new ReentrantLock(true);
        assertTrue(r2.isFair());
    }

    /**
     * locking an unlocked lock succeeds
     */
    public void testLock() {
        ReentrantLock rl = new ReentrantLock();
        rl.lock();
        assertTrue(rl.isLocked());
        rl.unlock();
    }

    /**
     * locking an unlocked fair lock succeeds
     */
    public void testFairLock() {
        ReentrantLock rl = new ReentrantLock(true);
        rl.lock();
        assertTrue(rl.isLocked());
        rl.unlock();
    }

    /**
     * Unlocking an unlocked lock throws IllegalMonitorStateException
     */
    public void testUnlock_IllegalMonitorStateException() {
        ReentrantLock rl = new ReentrantLock();
        try {
            rl.unlock();
            shouldThrow();

        } catch(IllegalMonitorStateException success){}
    }

    /**
     * tryLock on an unlocked lock succeeds
     */
    public void testTryLock() {
        ReentrantLock rl = new ReentrantLock();
        assertTrue(rl.tryLock());
        assertTrue(rl.isLocked());
        rl.unlock();
    }


    /**
     * hasQueuedThreads reports whether there are waiting threads
     */
    public void testhasQueuedThreads() {
//	final ReentrantLock lock = new ReentrantLock();
        final ReentrantLock lock = new ReentrantLock(true);
        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){
            e.printStackTrace();
            unexpectedException();
        }
    }

//    /**
//     * getQueueLength reports number of waiting threads
//     */
//    public void testGetQueueLength() {
//        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();
//        }
//    }

    /**
     * getQueueLength reports number of waiting threads
     */
    public void testGetQueueLength_fair() {
        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();
        }
    }

    /**
     * hasQueuedThread(null) throws NPE
     */
    public void testHasQueuedThreadNPE() {
//	final ReentrantLock sync = new ReentrantLock();
        final ReentrantLock sync = new ReentrantLock(true);
        try {
            sync.hasQueuedThread(null);
            shouldThrow();
        } catch (NullPointerException success) {
        }
    }

    /**
     * hasQueuedThread reports whether a thread is queued.
     */
    public void testHasQueuedThread() {
//        final ReentrantLock sync = new ReentrantLock();
        final ReentrantLock sync = new ReentrantLock(true);
        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){
            e.printStackTrace();
            unexpectedException();
        }
    }


    /**
     * getQueuedThreads includes waiting threads
     */
    public void testGetQueuedThreads() {
        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){
            e.printStackTrace();
            unexpectedException();
        }
    }


    /**
     * timed tryLock is interruptible.
     */
    public void testInterruptedException2() {
        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();
        }
    }


    /**
     * TryLock on a locked lock fails
     */
    public void testTryLockWhenLocked() {
        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();
        }
    }

    /**
     * Timed tryLock on a locked lock times out
     */
    public void testTryLock_Timeout() {
        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();
        }
    }

    /**
     * getHoldCount returns number of recursive holds

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?