天天看點

JUC - ReentrantLock源碼解析 - 多圖警告

文章目錄

          • 2.1.1.0 構造函數
          • 2.1.1.1 lockInterruptibly - 可中斷鎖 - 正在等待擷取鎖的線程可直接調用Thread.interrupt該線程直接放棄擷取鎖,且直接抛出異常
            • tryAcquire
            • doAcquireInterruptibly
          • 2.1.1.2 lock - 等待鎖的線程在另一個線程被interrupt不會立刻終止的原因,隻有擷取到鎖然後才會終止
          • 2.1.1.3 unlock
          • 2.1.1.5 如果你看懂了前面的代碼解析,這段代碼的輸出你應該也能看懂
2.1.1.0 構造函數
JUC - ReentrantLock源碼解析 - 多圖警告
2.1.1.1 lockInterruptibly - 可中斷鎖 - 正在等待擷取鎖的線程可直接調用Thread.interrupt該線程直接放棄擷取鎖,且直接抛出異常

ReentrantLock

JUC - ReentrantLock源碼解析 - 多圖警告

AbstractQueuedSynchronizer

JUC - ReentrantLock源碼解析 - 多圖警告

tryAcquire

NonfairSync

JUC - ReentrantLock源碼解析 - 多圖警告

Sync

JUC - ReentrantLock源碼解析 - 多圖警告

doAcquireInterruptibly

JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告

2.1.1.2 lock - 等待鎖的線程在另一個線程被interrupt不會立刻終止的原因,隻有擷取到鎖然後才會終止
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告

2.1.1.3 unlock
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告

Sync

JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告
JUC - ReentrantLock源碼解析 - 多圖警告

2.1.1.5 如果你看懂了前面的代碼解析,這段代碼的輸出你應該也能看懂
package top.linruchang.springdemo;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 作用:
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2021/03/14
 * @since 1.8
 **/
@Slf4j
public class OtherTest3 {
    public static void main(String[] args) throws InterruptedException {

        System.out.println("\n=========LockInterruptibly===========\n");
        testLockInterruptibly();


        Thread.sleep(15000);

        System.out.println("\n==========Lock==========\n");
        testLock();

    }


    public static void testLockInterruptibly() throws InterruptedException {
        TestLock testLock = new TestLock();

        Thread thread1 = new Thread(() -> {
            testLock.testLockInterruptibly();
        });
        thread1.start();



        Thread.sleep(500);

        Thread thread2 = new Thread(() -> {
            testLock.testLockInterruptibly();
        });
        thread2.start();


        Thread.sleep(500);
        log.info("中斷第二個線程");
        thread2.interrupt();

    }


    public static void testLock() throws InterruptedException {
        TestLock testLock = new TestLock();

        Thread thread1 = new Thread(() -> {
            testLock.testLock();
        });
        thread1.start();



        Thread.sleep(500);

        Thread thread2 = new Thread(() -> {
            testLock.testLock();
        });
        thread2.start();


        Thread.sleep(500);
        log.info("中斷第二個線程");
        thread2.interrupt();

    }


}

@Slf4j
class TestLock {

    ReentrantLock reentrantLock = new ReentrantLock();


    public void testLockInterruptibly() {
        String threadName = Thread.currentThread().getName();
        log.info(threadName + "啟動");
        try {
            //擷取到鎖,但可以調用interceprt使得鎖失效
            reentrantLock.lockInterruptibly();
            log.info(threadName + ":休眠10s");
            Thread.sleep(10000);
            log.info(threadName + ":休眠起來啦");
        } catch (Exception e) {
            log.error(threadName + ":發生異常" + e);
        } finally {
            reentrantLock.unlock();
            log.info(threadName + "結束,并釋放鎖");
        }
    }


    public void testLock()  {
        String threadName = Thread.currentThread().getName();
        log.info(threadName + "啟動");
        try {
            //擷取到鎖,但可以調用interceprt使得鎖失效
            reentrantLock.lock();
            log.info(threadName + ":第一個休眠10s");
            try {
                Thread.sleep(10000);
            }catch (Exception e) {
                log.error(threadName + ":發生異常【第一個休眠】" + e);
            }
            log.info(threadName + ":第一個休眠起來啦");

            log.info(threadName + ":第二個休眠10s");
            try {
                Thread.sleep(10000);
            }catch (Exception e) {
                log.error(threadName + ":發生異常【第二個休眠】" + e);
            }
            log.info(threadName + ":第二個休眠起來啦");


        } catch (Exception e) {
            log.error(threadName + ":發生異常" + e);
        } finally {
            reentrantLock.unlock();
            log.info(threadName + "結束,并釋放鎖");
        }
    }
}


           
JUC - ReentrantLock源碼解析 - 多圖警告