ReentrantReadWriteLock api详解

今天学习ReentrantReadWriteLock工具类,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充

package test.java.util.concurrent.locks;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.junit.Test;

/**
 * ReentrantReadWriteLock的测试类
 *
 * @author zqw
 * @date 2020-06-22 22:18:53
 */
public class ReentrantReadWriteLockTest {
        /**
         * 无参构造函数,默认非公平锁,效率高
         * 同时初始化readLock和writeLock
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testConstruct0()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                System.out.println(testObj.toString());
        }
        /**
         * 有参构造函数,指定公平锁和非公平锁
         * 公平锁抢不到的线程入队等待执行
         * 同时初始化readLock和writeLock
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testConstruct1()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                System.out.println(testObj.toString());
        }
        class TestReadWriteLockThread extends Thread{
                ReentrantReadWriteLock lock=null;
                public TestReadWriteLockThread(String name,ReentrantReadWriteLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                        try {
                                lock.writeLock().lock();
                                System.out.println("获得锁:"+getName());
//                                Thread.sleep(100); ???? 为啥不能加睡眠,加上就释放不了锁
                        }catch (Exception e){
                        }finally {
                                System.out.println("释放锁:"+getName());
                                lock.writeLock().unlock();
                        }
                }
        }
        /**
         * 写锁为公共静态内部类,外部可以访问
         * 读读共享,读写互斥,写读互斥,写写互斥
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testWriteLock()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                TestReadWriteLockThread test=new TestReadWriteLockThread("lock",testObj);
                TestReadWriteLockThread test1=new TestReadWriteLockThread("lock1",testObj);
                TestReadWriteLockThread test2=new TestReadWriteLockThread("lock2",testObj);
                test.start();
                test1.start();
                test2.start();

        }
        class TestReadWriteLockThread1 extends Thread{
                ReentrantReadWriteLock lock=null;
                public TestReadWriteLockThread1(String name,ReentrantReadWriteLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                        try {
                                lock.readLock().lock();
                                System.out.println("获得锁:"+getName());
//                                Thread.sleep(2000);
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                System.out.println("释放锁:"+getName());
                                lock.readLock().unlock();
                        }
                }
        }
        /**
         * 读锁为公共静态内部类,外部可以访问
         * 读读共享,读写互斥,写读互斥,写写互斥
         *结果: 获得读锁:lock
         * 获得读锁:lock1
         * 释放读锁:lock1
         * 释放读锁:lock
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testReadLock()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                TestReadWriteLockThread1 test=new TestReadWriteLockThread1("lock",testObj);
                TestReadWriteLockThread1 test1=new TestReadWriteLockThread1("lock1",testObj);
                test.start();
                test1.start();
        }
        /**
         * 是否是公平锁
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testIsFair()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                System.out.println(testObj.isFair());
        }
        class TestReadWriteLockThread2 extends Thread{
                ReentrantReadWriteLock lock=null;
                public TestReadWriteLockThread2(String name,ReentrantReadWriteLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                        try {
                                lock.readLock().lock();
                                System.out.println("获取锁:"+getName());
                                System.out.println(getName()+":"+lock.getReadLockCount());
//                                Thread.sleep(3000);
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                 System.out.println("释放锁:"+getName());
                                lock.readLock().unlock();
                        }
                }
        }
        /**
         * 获取占有读锁的线程数量
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetReadLockCount()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                TestReadWriteLockThread2 test=new TestReadWriteLockThread2("lock",testObj);
                TestReadWriteLockThread2 test1=new TestReadWriteLockThread2("lock1",testObj);
                test.start();
                test1.start();
        }
        /**
         * 写锁是否被锁住
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testIsWriteLocked()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                testObj.writeLock().lock();
                System.out.println(testObj.isWriteLocked());
        }
        /**
         * 写锁是否被当前线程持有
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testIsWriteLockedByCurrentThread()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                testObj.writeLock().lock();
                System.out.println(testObj.isWriteLockedByCurrentThread());
        }
        /**
         * 写锁被当前线程重入的次数
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetWriteHoldCount()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                testObj.writeLock().lock();
                testObj.writeLock().lock();
                System.out.println(testObj.getWriteHoldCount());
        }
        /**
         * 读锁被当前线程重入的次数
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetReadHoldCount()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                testObj.readLock().lock();
                testObj.readLock().lock();
                System.out.println(testObj.getReadHoldCount());
        }
        /**
         * 等待队列中是否还有线程
         * 即head!=tail
         * true 有  false 没有
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testHasQueuedThreads()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                System.out.println(testObj.hasQueuedThreads());
        }
        /**
         * 参数线程是否在等待队列中
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testHasQueuedThread()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                TestReadWriteLockThread2 test1=new TestReadWriteLockThread2("lock1",testObj);
                test1.start();
                Thread.sleep(5000);
                System.out.println(testObj.hasQueuedThread(test1));
        }
        /**
         * 获取等待获取锁定的线程数
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetQueueLength()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                System.out.println(testObj.getQueueLength());
        }
        /**
         * 是否有等待线程
         * 读锁不支持,写锁支持
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testHasWaiters()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                testObj.writeLock().lock();
                System.out.println(testObj.hasWaiters(testObj.writeLock().newCondition()));
        }
        /**
         * w.waitStatus == Node.CONDITION
         * 获取cond.await中的线程数量
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetWaitQueueLength()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                testObj.writeLock().lock();
                System.out.println(testObj.getWaitQueueLength(testObj.writeLock().newCondition()));
        }
        /**
        * toString
         * [Write locks = 0, Read locks = 0]
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/23 1:36
         */
        @Test
        public void testToString()throws Exception{
                ReentrantReadWriteLock testObj=new ReentrantReadWriteLock(true);
                System.out.println(testObj.toString());
        }

}

 

你可能感兴趣的:(jdk源码解析)