JUC - ReentrantLock源码解析 - 多图警告

ReentrantLock分类
FairSync公平锁 - 构造函数true - 线程轮流获得锁
NonfairSync非公平锁 - 默认,构造函数false - 跟Synchronized一样是非公平
2.1.1.0 构造函数

在这里插入图片描述

2.1.1.1 lockInterruptibly - 可中断锁 - 正在等待获取锁的线程可直接调用Thread.interrupt该线程直接放弃获取锁,且直接抛出异常


ReentrantLock
在这里插入图片描述


AbstractQueuedSynchronizer
在这里插入图片描述

tryAcquire


NonfairSync
在这里插入图片描述


Sync
在这里插入图片描述

doAcquireInterruptibly

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.1.1.2 lock - 等待锁的线程在另一个线程被interrupt不会立刻终止的原因,只有获取到锁然后才会终止

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.1.1.3 unlock

在这里插入图片描述

在这里插入图片描述


Sync
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.1.1.5 如果你看懂了前面的代码解析,这段代码的输出你应该也能看懂
区别
1. lockInterruptibly:等待获取锁的线程1,在此过程中,如果被其他线程nterrupt,则该线程1立刻直接不等待获取锁,丢弃锁,直接抛出异常
2. lock:等待获取锁的线程1,在此过程中,如果被其他线程interrupt,则延迟到获取锁才interrupt
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 + "结束,并释放锁");
        }
    }
}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值