当前位置:网站首页>AQS & reentrantlock implementation principle
AQS & reentrantlock implementation principle
2022-04-23 09:09:00 【MARIO ODYSSEY】
List of articles
1 AQS (AbstractQuenedSynchronizer)

- AQS Maintenance state
volatile int state(ReentrantLockTake it as the number of re-entry times of the lock ) And a FIFO Thread waiting queue , When a multi thread contention resource is blocked, it will enter this queue ; After the thread at the head of the queue is executed , It calls its successor thread - Threads pass through CAS Change status symbol state, Success is the success of the lock , If it fails, it will enter the waiting queue , Waiting to be awakened
- AQS use spinlocks The mechanism of
2 ReentrantLock Unfair locking process
- An unfair lock is a new thread that follows AQS The thread at the head of the queue competes for the lock , Other threads in the queue still queue normally
- Fair lock is strictly enforced FIFO, New threads can only join the tail of the queue

- Unfair lock attempts to lock , The perform
tryAcquire()The process is : Check state Field , if 0, Indicates that the lock is not occupied , Try to occupy the lock ; If not 0, Check whether the current lock is occupied by itself , If you are occupied by yourself , Update state Field , Reentry times plus 1 - If none of the above works , Failed to acquire lock , Enter the waiting queue
- The thread entering the waiting queue attempts to acquire the lock ( The top thread is qualified to try ), If successful, it becomes the new head node of the queue , If the acquisition fails, try to suspend
- The prerequisite for a thread to be able to suspend after it is queued is , The state of its precursor node is SIGNAL, Status as SIGNAL The node will wake up the next node after leaving the queue
3 ReentrantLock and synchronized Similarities and differences

- In response to interrupt : If a thread A Executing code in lock , Another thread B Waiting to acquire the lock , It may be due to the long waiting time , Threads B Don't want to wait , Want to deal with other things first , We can let it interrupt itself or interrupt it in another thread , This is the interruptible lock .Lock In the process of waiting for the lock, you can use interrupt To interrupt the waiting
- Overtime waiting : Specify the timeout waiting time , Avoid threads waiting indefinitely for lock release
- Fair lock and unfair lock : Fair lock is when multiple threads try to acquire the same lock at the same time , The order in which the locks are acquired is the order in which the threads are reached , Non fair locks allow threads to “ Jump the queue ”, Specifically, the new thread competes with the thread of the team leader for lock
版权声明
本文为[MARIO ODYSSEY]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230718011634.html
边栏推荐
- ATSS(CVPR2020)
- L2-3 romantic silhouette (25 points)
- [boutique] using dynamic agent to realize unified transaction management II
- [indexof] [lastIndexOf] [split] [substring] usage details
- Go language self-study series | initialization of golang structure
- Trc20 fund collection solution based on thinkphp5 version
- Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
- Correct method of calculating inference time of neural network
- 基于点云凸包的凹包获取方法
- 【原创】使用System.Text.Json对Json字符串进行格式化
猜你喜欢
随机推荐
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
Wechat applet catchtap = "todetail" event problem
Kettle实验 转换案例
Node installation
L2-3 romantic silhouette (25 points)
Technological innovation in government affairs in the construction of Digital Government
Correct method of calculating inference time of neural network
Complete binary search tree (30 points)
Colorui solves the problem of blocking content in bottom navigation
Cadence process angle simulation, Monte Carlo simulation, PSRR
JS prototype chain
调包求得每个样本的k个邻居
Write down the post order traversal of the ~ binary tree
Go language self-study series | initialization of golang structure
Strength comparison vulnerability of PHP based on hash algorithm
机器学习(六)——贝叶斯分类器
L2-022 重排链表 (25 分)(map+结构体模拟)
I don't understand time, timestamp and time zone. Look at this article
小女孩行走
A must see wechat applet development guide 1 - basic knowledge
![[58] length of the last word [leetcode]](/img/c5/3ba1fe5a81593d9576bb597f0d5f45.png)








