当前位置:网站首页>跟我一起AQS SOS AQS
跟我一起AQS SOS AQS
2022-06-27 19:23:00 【高粱】
ReentrantLock简单介绍
先拿ReentrantLock来说说把
里面有AQS抽象队列的实现类NonfairSync和FairSync 分别实现的是非公平和公平锁
抽象队列是一个双向队列 里面有node节点 每个node节点存放当前线程 还有pre和next 和 waitStatus状态值
ReentrantLock实现原理
简单一句话 自旋(for) cas park
抽象队列不存在锁竞争时是不会初始化的不会有额外的内存消耗
存在锁竞争时会初始化一个空的node节点 thread=null 然后再初始化自己线程的node节点 把pre指向头节点
这里维持着一个标准
队列头部thread永远是空
持有锁的线程永远不在队列 即不参与排队机制

入队时会把上一个node节点的waitStatus改为-1 表示让上一个节点睡眠, (为什么上一个节点自己不把自己睡眠 因为自己不知道自己睡着了哈哈解释的牵强一些但是符合实际生活)
抢锁成功会把当队列头node节点丢弃 自己成为头节点 并把自己node节点的thread置空
如果是第一个排队的人(在队列第二位)就尝试cas抢锁加锁
如果不是第一个排队的人 轮不到抢锁
以下补充
加锁过程
1.用一个变量state作为锁的标志位,默认是0,表示此时所有线程都可以加锁,加锁的时候通过cas将state从0变为1,cas执行成功表示加锁成功
2.当有线程占有了锁,这时候有其他线程来加锁,判断当前来抢锁的线程是不是占用锁的线程?是:重入锁,state+1,当释放的时候state-1,用state表示加锁的次数 否:加锁失败,将线程放入等待队列,并且阻塞
3.有没有什么其他可以优化的地方?当放入等待队列的时候,看看有没有其他线程?有,锁被占用了,并且轮不到当前线程来抢,直接阻塞就行了 在放入队列时候,通过cas再尝试获取一波锁,如果获取成功,就不用阻塞了,提高了效率
解锁过程
1.通过cas对state-1,如果是重入锁,释放一次减一次,当state=0时表示锁被释放。2.唤醒等待队列中的线程
入队过程
当把线程放入队列中时,后续应该做哪些操作呢?
如果让你写是不是直接放入队列中就完事了?但Doug Lea是这样做的
1.如果当前线程是队列中的第二个节点则再尝试抢一下锁(不是第二个节点就不用抢来,轮不到),这样避免了频繁的阻塞和唤醒线程,提高了效率
2.上闹钟,让上一个线程来唤醒自己(后续会说到,即更改上一个节点的waitStatus)
3. parkAndCheckInterrupt() 这个方法调用LockSupport.park(this);会阻塞当前竞争锁的线程, 直到有线程unlock出队然后unpark下一个阻塞的线程,此时方法才调用return Thread.interrupted(); 为什么调用(重置线程打断标志)方法呢 用此来判断阻塞的线程是否被其他线程被打断, 如果被打断返回true 跳出方法 后继续执行acquire() 内的selfInterrupt(); 将唤醒的竞争锁的线程还原线程打断状态
4. lock.lock() 无法接受线程被打断的讯息
lock.lockInterruptibly() 可接受线程在等待抢锁时被打断的讯息
阻塞
出队
当A线程释放锁,唤醒队列中的B线程,A线程会从队列中删除
那出队这个事情由谁来做?是由被唤醒的线程来做,即B线程
链接文章
https://mp.weixin.qq.com/s/uUAUGQ_WGHJzhJAyBmcWYg
边栏推荐
- 数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
- MySQL速成——第一天--基础入门
- Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
- Go从入门到实战——共享内存并发机制(笔记)
- Here are 12 commonly used function formulas for you. All used ones are good
- KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
- Acwing周赛57-数字操作-(思维+分解质因数)
- mysql使用笔记一
- Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
- White whoring red team goby & POC, how do you call white whoring?
猜你喜欢

Go从入门到实战——共享内存并发机制(笔记)

Oracle的CTAS能不能将约束等属性带到新表?

How to do a good job of gateway high availability protection in the big promotion scenario

Use the storcli tool to configure raid. Just collect this article

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

White whoring red team goby & POC, how do you call white whoring?

Squid proxy server

JPA踩坑系列之save方法

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China

SQL Server for循环用法
随机推荐
squid代理服務器
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
通过CE修改器修改大型网络游戏
Go从入门到实战——共享内存并发机制(笔记)
A set of system to reduce 10 times the traffic pressure in crowded areas
Industry case | see the operation of bank digital transformation from the king of retail
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Galaxy Kirin system LAN file sharing tutorial
Unity3d button adapts the size according to the text content
麒麟V10安装字体
Go从入门到实战——协程机制(笔记)
Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control
Flask----应用案例
DO280OpenShift访问控制--security policy和章节实验
送你12个常用函数公式,用过的都说好
Share an experience of self positioning + problem solving
Cortical traceability analysis of ERP
100 important knowledge points that SQL must master: combining where clauses
Go从入门到实战——所有任务完成(笔记)