ReentrantLock api 詳解

今天學習ReentrantLock工具類,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大致瞭解了底層實現,初學乍練,有很多一知半解的地方,待後續有了深入理解再來補充

package test.java.util.concurrent.locks;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import org.junit.Test;

/**
 * ReentrantLock的測試類
 *
 * @author zqw
 * @date 2020-06-21 20:46:26
 */
public class ReentrantLockTest {

        class TestReentrantLockInnerClass extends Thread{
                ReentrantLock lock=null;
                public TestReentrantLockInnerClass(String name,ReentrantLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                                lock.lock();
                                try {
                                        System.out.println("name:"+getName());
                                }catch (Exception e){
                                        e.printStackTrace();
                                }finally {
                                        lock.unlock();
                                }

                }
        }
        /**
        * 無參構造函數,默認非公平鎖,效率高
         * 非公平鎖搶不到則不執行,不如隊列
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testConstruct0()throws Exception{
                ReentrantLock lock=new ReentrantLock();
                TestReentrantLockInnerClass lockTest=new TestReentrantLockInnerClass("lock",lock);
                TestReentrantLockInnerClass lockTest1=new TestReentrantLockInnerClass("lock1",lock);
                lockTest.start();
                Thread.sleep(2000);
                lockTest1.start();
        }
        /**
         * 有參構造函數,指定公平鎖和非公平鎖
         * 公平鎖搶不到的線程入隊等等執行
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testConstruct1()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass lockTest=new TestReentrantLockInnerClass("lock",lock);
                TestReentrantLockInnerClass lockTest1=new TestReentrantLockInnerClass("lock1",lock);
                lockTest.start();
                lockTest1.start();
        }
        /**
         * lock和unlock必須同時使用
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testLock()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass lockTest=new TestReentrantLockInnerClass("lock",lock);
                TestReentrantLockInnerClass lockTest1=new TestReentrantLockInnerClass("lock1",lock);
                lockTest.start();
                lockTest1.start();
        }
        class TestReentrantLockInnerClass1 extends Thread{
                ReentrantLock lock=null;
                public TestReentrantLockInnerClass1(String name,ReentrantLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                        try {
                                lock.lockInterruptibly();
                                Thread.sleep(3000);
                                System.out.println("name:"+getName());
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        /**
         *
         * 線程在請求lock並被阻塞時,如果被interrupt,則此線程會被喚醒並被要求處理InterruptedException
         * 和lock使用類似
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:21
         */
        @Test
        public void testLockInterruptibly()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass1 lockTest=new TestReentrantLockInnerClass1("lock",lock);
                lockTest.start();
                lockTest.interrupt();
                Thread.sleep(1000000);
        }
        class TestReentrantLockInnerClass2 extends Thread{
                ReentrantLock lock=null;
                public TestReentrantLockInnerClass2(String name,ReentrantLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {

                        try {
                                while (!lock.tryLock()){
                                }
                                System.out.println("name:"+getName());
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        /**
        * 嘗試獲取鎖,如果獲取成功,則返回true,如果獲取失敗(即鎖已被其他線程獲取),則返回false
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:59
         */
        @Test
        public void testTryLock()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass2 lockTest=new TestReentrantLockInnerClass2("lock",lock);
                TestReentrantLockInnerClass2 lockTest1=new TestReentrantLockInnerClass2("lock1",lock);
                lockTest.start();
                lockTest1.start();
        }
        class TestReentrantLockInnerClass3 extends Thread{
                ReentrantLock lock=null;
                public TestReentrantLockInnerClass3(String name,ReentrantLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {

                        try {
                                while (!lock.tryLock(3000, TimeUnit.MILLISECONDS)){
                                }
                                System.out.println("name:"+getName());
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        /**
         * 嘗試獲取鎖3秒,如果獲取成功,則返回true,如果超過3秒還沒獲取到,獲取失敗(即鎖已被其他線程獲取),則返回false
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/21 23:59
         */
        @Test
        public void testTryLock1()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass3 lockTest=new TestReentrantLockInnerClass3("lock",lock);
                TestReentrantLockInnerClass3 lockTest1=new TestReentrantLockInnerClass3("lock1",lock);
                lockTest.start();
                lockTest1.start();
        }
        /**
        * 釋放鎖
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:02
         */
        @Test
        public void testUnlock()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass3 lockTest=new TestReentrantLockInnerClass3("lock",lock);
                TestReentrantLockInnerClass3 lockTest1=new TestReentrantLockInnerClass3("lock1",lock);
                lockTest.start();
                lockTest1.start();
        }
        class TestReentrantLockInnerClass4 extends Thread{
                ReentrantLock lock=null;
                Condition condition=null;
                public TestReentrantLockInnerClass4(String name,ReentrantLock lock,Condition condition){
                        super(name);
                        this.lock=lock;
                        this.condition=condition;
                }
                @Override
                public void run() {
                        lock.lock();
                        try {
                                System.out.println("name:"+getName());
                                condition.await();
                                System.out.println("name:"+getName());
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        class TestReentrantLockInnerClass5 extends Thread{
                ReentrantLock lock=null;
                Condition condition=null;
                public TestReentrantLockInnerClass5(String name,ReentrantLock lock,Condition condition){
                        super(name);
                        this.lock=lock;
                        this.condition=condition;
                }
                @Override
                public void run() {
                        lock.lock();
                        try {
                                System.out.println("name:"+getName());
                                condition.signal();
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        /**
         * condition 等待和釋放
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:02
         */
        @Test
        public void testNewCondition()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                Condition condition=lock.newCondition();
                TestReentrantLockInnerClass4 lockTest=new TestReentrantLockInnerClass4("lock",lock,condition);
                TestReentrantLockInnerClass5 lockTest1=new TestReentrantLockInnerClass5("lock1",lock,condition);
                lockTest.start();
                lockTest1.start();
        }
        class TestReentrantLockInnerClass6 extends Thread{
                ReentrantLock lock=null;
                public TestReentrantLockInnerClass6(String name,ReentrantLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                        lock.lock();
                        try {
                                System.out.println(lock.getHoldCount());
                                System.out.println("name:"+getName());
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        /**
        * 查詢當前線程保持鎖定的個數,也就是調用lock()方法的次數
         * 注意:當前線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetHoldCount()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass6 lockTest=new TestReentrantLockInnerClass6("lock",lock);
                lockTest.start();
        }
        /**
         * 查詢持有鎖的線程是否是當前線程
         * 注意:當前線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testIsHeldByCurrentThread()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                lock.lock();
                System.out.println(lock.isHeldByCurrentThread());
        }
        /**
         * 是否鎖定,state!=0
         * 注意:當前線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testIsLocked()throws Exception{
                ReentrantLock testObj=new ReentrantLock();
                testObj.lock();
                System.out.println(testObj.isLocked());
        }
        /**
         * 是否是公平鎖
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testIsFair()throws Exception{
                ReentrantLock testObj=new ReentrantLock(true);
                System.out.println(testObj.isFair());
        }
        /**
         * 等待隊列中是否還有線程
         * 即head!=tail
         * true 有  false 沒有
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testHasQueuedThreads()throws Exception{
                ReentrantLock testObj=new ReentrantLock();
                System.out.println(testObj.hasQueuedThreads());
        }
        /**
         * 參數線程是否在等待隊列中
         * 即head!=tail
         * true 有  false 沒有
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testHasQueuedThread()throws Exception{
                ReentrantLock lock=new ReentrantLock(true);
                TestReentrantLockInnerClass6 lockTest=new TestReentrantLockInnerClass6("lock",lock);
                lockTest.start();
                System.out.println(lock.hasQueuedThread(lockTest));
        }
        /**
         * 獲取等待獲取鎖定的線程數
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetQueueLength()throws Exception{
                ReentrantLock testObj=new ReentrantLock();
                System.out.println(testObj.getQueueLength());
        }
        class TestReentrantLockInnerClass7 extends Thread{
                ReentrantLock lock=null;
                public TestReentrantLockInnerClass7(String name,ReentrantLock lock){
                        super(name);
                        this.lock=lock;
                }
                @Override
                public void run() {
                        lock.lock();
                        try {
                                System.out.println(lock.hasWaiters(lock.newCondition()));
                                System.out.println("name:"+getName());
                        }catch (Exception e){
                                e.printStackTrace();
                        }finally {
                                lock.unlock();
                        }

                }
        }
        /**
         * w.waitStatus == Node.CONDITION
         * 是否有等待線程
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testHasWaiters()throws Exception{
                ReentrantLock testObj=new ReentrantLock(true);
                TestReentrantLockInnerClass7 test=new TestReentrantLockInnerClass7("lock7",testObj);
                test.start();
        }
        /**
         * w.waitStatus == Node.CONDITION
         * 獲取cond.await中的線程數量
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testGetWaitQueueLength()throws Exception{
                ReentrantLock testObj=new ReentrantLock();
                testObj.lock();
                System.out.println(testObj.getWaitQueueLength(testObj.newCondition()));
        }
        /**
         * Object.toString[鎖定狀態 by 線程名稱]
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/22 0:20
         */
        @Test
        public void testToString()throws Exception{
        ReentrantLock testObj=new ReentrantLock();
                testObj.lock();
                System.out.println(testObj.toString());
        }









}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章