当前位置:网站首页>AQS SOS AQS with me
AQS SOS AQS with me
2022-06-27 21:54:00 【sorghum】
ReentrantLock Brief introduction
First take ReentrantLock Say something
There are AQS Implementation class of abstract queue NonfairSync and FairSync They implement unfair and fair locks respectively
An abstract queue is a two-way queue There are node node Every node Node stores the current thread also pre and next and waitStatus The status value
ReentrantLock Realization principle
In a word The spin (for) cas park
The abstract queue will not be initialized when there is no lock competition, and there will be no additional memory consumption
An empty... Is initialized when lock contention exists node node thread=null Then initialize your own thread node node hold pre Point to the head node
A standard is maintained here
Queue header thread Always empty
The thread holding the lock is never in the queue That is, it does not participate in the queuing mechanism

When you join the team, you will put the last node Node waitStatus Change it to -1 Indicates that the previous node is allowed to sleep , ( Why didn't the last node sleep itself Because I don't know I'm asleep. Haha, the explanation is far fetched, but it conforms to the actual life )
Successful lock snatching will make you the head of the queue node Node drop Become the head node And put yourself node Node thread empty 
If it's the first person in line ( Second in the queue ) Just try cas Lock up
If not the first person in line It's too late to grab the lock 
The following supplement
Locking process
1. Use a variable state As the flag bit of the lock , The default is 0, Indicates that all threads can be locked at this time , Pass when locked cas take state from 0 Turn into 1,cas Successful execution means successful locking
2. When a thread takes possession of a lock , At this time, there are other threads to lock , Determine whether the thread that is currently trying to grab the lock is the thread that is occupying the lock ? yes : Reentrant lock ,state+1, When released state-1, use state Indicates the number of locks no : Locking failed , Put the thread in the waiting queue , And blocking
3. Is there anything else you can optimize ? When placed in the waiting queue , See if there are any other threads ? Yes , The lock is occupied , And the current thread cannot be used to rob , Just block it directly When you put it in the queue , adopt cas Try again to acquire a wave of locks , If successful , There is no need to block , Improved efficiency
Unlocking process
1. adopt cas Yes state-1, If it's a re-entry lock , One release minus one , When state=0 When, the lock is released .2. Wake up the thread in the waiting queue
The process of joining the team
When a thread is put in a queue , What should I do next ?
If you are asked to write whether it is directly put into the queue, you are done ? but Doug Lea Here's how it works
1. If the current thread is the second node in the queue, try to rob the lock again ( If it's not the second node, you don't have to grab it , It's not the turn ), This avoids frequent blocking and wakeup of threads , Improved efficiency
2. Set the alarm clock , Let the previous thread wake itself up ( I'll talk about it later , That is, change the of the previous node waitStatus)
3. parkAndCheckInterrupt() This method calls LockSupport.park(this); Will block the thread that currently competes for the lock , Until there are threads unlock Get out of the team and unpark Next blocked thread , Only then does the method call return Thread.interrupted(); Why call ( Reset thread break flag ) Methods? Use this to determine whether a blocked thread is interrupted by another thread , If interrupted, return to true Jump out of the way And then go ahead with acquire() Internal selfInterrupt(); Restore the thread of the awakened contention lock to the thread interrupt state
4. lock.lock() Unable to accept the message that the thread is interrupted
lock.lockInterruptibly() It can accept the message that the thread is interrupted while waiting for lock snatching 
Blocking
Out of the team
When A Thread release lock , Wake up in the queue B Threads ,A The thread is removed from the queue
Who will do the task of leaving the team ? It is done by the awakened thread , namely B Threads 
Linked articles
https://mp.weixin.qq.com/s/uUAUGQ_WGHJzhJAyBmcWYg
边栏推荐
- 富文本 考试 填空题
- 关于异常处理的知识整理
- 畅游动态规划之区间DP
- Go从入门到实战—— 多路选择和超时控制(笔记)
- C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
- 猜拳游戏专题训练
- How to participate in openharmony code contribution
- 流程控制任务
- VMware virtual machine PE startup
- GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
猜你喜欢

Go從入門到實戰——接口(筆記)

Go从入门到实战——Panic和recover(笔记)

Go from introduction to actual combat - panic and recover (notes)

Simulink method for exporting FMU model files

【Redis】零基础十分钟学会Redis

vmware虚拟机PE启动

CORBA 架构体系指南(通用对象请求代理体系架构)

Knowledge sorting of exception handling
![[leetcode] dynamic programming solution split integer i[silver fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[leetcode] dynamic programming solution split integer i[silver fox]
![[LeetCode]动态规划解拆分整数I[Silver Fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[LeetCode]动态规划解拆分整数I[Silver Fox]
随机推荐
Special tutorial - Captain selection game
专题教程——选队长游戏
Interval DP of Changyou dynamic programming
富文本 考试 填空题
洛谷P5706 再分肥宅水
有时间看看ognl表达式
uniapp拦截请求
GBase 8a OLAP分析函数cume_dist的使用样例
Go从入门到实战——所有任务完成(笔记)
关于异常处理的知识整理
I think I should start writing my own blog.
[LeetCode]30. 串联所有单词的子串
Acwing周赛57-最长连续子序列-(二分or树状数组)
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
∫(0→1) ln(1+x) / (x ² + 1) dx
Go从入门到实战——CSP并发机制(笔记)
[LeetCode]513. 找树左下角的值
豆沙绿保护你的双眼
Codeforces Round #717 (Div. 2)
Go from introduction to practice - Interface (notes)