ReentrantReadWriteLock----可重入读写锁

25 篇文章 0 订阅

读写锁:允许同一时刻被多个读线程访问,但是在写线程访问时,所有的读线程与其他的写线程均会阻塞。

写线程能够获取到锁的前提条件:没有任何读、写线程拿到锁。

1. 写锁-WriteLock-独占锁

1.1 写锁的获取 - 模板方法tryAcquire()
源码:

protected final boolean tryAcquire(int acquires) {
            /*
             * Walkthrough:
             * 1. If read count nonzero or write count nonzero
             *    and owner is a different thread, fail.
             * 2. If count would saturate, fail. (This can only 
             *    happen if count is already nonzero.)
             * 3. Otherwise, this thread is eligible for lock if
             *    it is either a reentrant acquire or
             *    queue policy allows it. If so, update state
             *    and set owner.
             */
            Thread current = Thread.currentThread();
            //判断当前同步状态
            int c = getState();
            //判断写锁的获取次数
            int w = exclusiveCount(c);
            if (c != 0) {
                // (Note: if c != 0 and w == 0 then shared count != 0)
                if (w == 0 || current != getExclusiveOwnerThread())
                    return false;
                if (w + exclusiveCount(acquires) > MAX_COUNT)
                    throw new Error("Maximum lock count exceeded");
                // Reentrant acquire
                setState(c + acquires);
                return true;
            }
            if (writerShouldBlock() ||
                !compareAndSetState(c, c + acquires))
                return false;
            //获取写锁成功
            setExclusiveOwnerThread(current);
            return true;
        }

c只是一个整型,那它到底表示的是读锁还是写锁?
我们通过读源码可以发现,同步状态的低16位表示写锁获取次数,高16位表示读锁获取次数。

写锁的获取逻辑:
当读锁已被线程获取或者写锁已被其他线程获取,则写线程获取锁失败;
否则,当前同步状态没有被任何读写线程获取,则当前线程获取写锁成功并且支持重入。

1.2 写锁的释放
写锁的释放逻辑与独占式锁的释放(release)逻辑一样:https://mp.csdn.net/mdeditor/87895586#

2. 读锁-共享式锁-tryAcquireShared()

3. 读写锁的应用场景:缓存的实现。

4. 锁降级:写锁可以降级为读锁,读锁不可以升级为写锁。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值