当前位置:网站首页>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
(ReentrantLock
Take 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
边栏推荐
- Latex mathematical formula
- Technological innovation in government affairs in the construction of Digital Government
- Summary of common concepts and problems of linear algebra in postgraduate entrance examination
- Whether the same binary search tree (25 points)
- A must see wechat applet development guide 1 - basic knowledge
- 資源打包關系依賴樹
- 调包求得每个样本的k个邻居
- LeetCode396. Rotate array
- Use include in databinding
- Idea package jar file
猜你喜欢
Common errors of VMware building es8
npm ERR! network
Download and install bashdb
Strength comparison vulnerability of PHP based on hash algorithm
Flink SQL realizes the integration of stream and batch
Arbre de dépendance de l'emballage des ressources
Project upload part
First principle mind map
Applet error: cannot read property'currenttarget'of undefined
The most concerned occupations after 00: civil servants ranked second. What was the first?
随机推荐
web页面如何渲染
MySQL小练习(仅适合初学者,非初学者勿进)
How does kubernetes use harbor to pull private images
Emuelec compilation summary
valgrind和kcachegrind使用運行分析
Judgment on heap (25 points) two insertion methods
Withholding agent
RSA 加密解密签名验签
valgrind和kcachegrind使用运行分析
501. 二叉搜索树中的众数
Failed to prepare device for development
Machine learning (VI) -- Bayesian classifier
Node installation
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
kettle实验
[58] length of the last word [leetcode]
Distributed message oriented middleware framework selection - Digital Architecture Design (7)
Concave hull acquisition method based on convex hull of point cloud
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
Go language self-study series | golang nested structure