当前位置:网站首页>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
边栏推荐
- Quick excel export according to customized excel Title Template
- 猜拳游戏专题训练
- Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
- GBase 8a数据库用户密码安全相关参数汇总
- Codeforces Round #717 (Div. 2)
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- QT base64 encryption and decryption
- 集合代码练习
- 我想我要开始写我自己的博客了。
- Codeforces Round #723 (Div. 2)
猜你喜欢
Go from introduction to practice -- coordination mechanism (note)
Go从入门到实战—— 多路选择和超时控制(笔记)
[LeetCode]动态规划解分割数组II[Arctic Fox]
Bit. Store: long bear market, stable stacking products may become the main theme
[leetcode] dynamic programming solution partition array i[red fox]
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
Simulink method for exporting FMU model files
语言弱点列表--CWE,一个值得学习的网站
随机推荐
List of language weaknesses --cwe, a website worth learning
Go从入门到实战——仅执行一次(笔记)
[LeetCode]515. Find the maximum value in each tree row
富文本 考试 填空题
∫(0→1) ln(1+x) / (x ² + 1) dx
关于异常处理的知识整理
Tiktok's interest in e-commerce has hit the traffic ceiling?
Codeforces Round #717 (Div. 2)
[leetcode] dynamic programming solution partition array ii[arctic fox]
Array assignment
[LeetCode]572. A subtree of another tree
洛谷P5706 再分肥宅水
Burp suite遇到的常见问题
Go from introduction to practice - polymorphism (note)
快递e栈——数组篇小型项目
Go from introduction to actual combat - context and task cancellation (notes)
∫(0→1) ln(1+x) / (x² + 1) dx
GBase 8a OLAP分析函数cume_dist的使用样例
JVM memory structure when creating objects
[Sword Offer II]剑指 Offer II 029. 排序的循环链表