当前位置:网站首页>12. AQS of abstractqueuedsynchronizer
12. AQS of abstractqueuedsynchronizer
2022-06-11 12:14:00 【Shixiaozan】
1、 Pre knowledge
Linked list of data structures
Template design pattern of design pattern
2、 What is it?
2.1、 Literally
AbstractQueuedLongSynchronizer
AbstractQueuedSynchronizer Usually :AbstractQueuedSynchronizer Referred to as AQS
2.2、 Technical explanation
Is used to build locks or other synchronizer components The heavyweight infrastructure and the whole JUC The cornerstone of the system , Through the built-in FIFO queue To complete the queuing work of the resource acquisition thread , And through a int Class variables Indicates the state of holding the lock
CLH:Craig、Landin and Hagersten queue , It's a one-way list ,AQS The queue in is CLH Virtual two-way queues for variants FIFO
3、AQS Why JUC The most important cornerstone of content
3.1、 and AQS Relevant
3.2、 Further understand the relationship between lock and synchronizer
It defines the use layer of programmer and lock interaction API, Hidden implementation details , You can call .
synchronizer , Lock oriented implementers
such as Java Concurrent God DougLee, A unified specification is proposed and the implementation of lock is simplified ,
The synchronization state management is blocked 、 Blocking thread queuing and notification 、 Wake up mechanism, etc .
4、 What can I do?
4.1、 Locking can cause blocking
If there is a jam, you need to line up , Queue is necessary to realize queuing
4.2、 interpretative statement
The thread that grabs the resource directly processes the business , Those who can't grab resources must involve a kind of Queuing mechanism . Threads that fail to preempt resources continue to wait ( Similar banking business processing windows are full , Customers who do not have an acceptance window can only go to Wait in line in the waiting area ), However, the waiting thread still retains the possibility of acquiring locks, and the process of acquiring locks continues ( Customers in the waiting area are also waiting for a call , It's time to go to the reception window to handle business ).
Now that we're here Queuing mechanism , Then there must be some kind of formation , What data structure is such a queue ?
If shared resources are occupied , A certain blocking waiting wake mechanism is needed to ensure lock allocation . The main use of this mechanism is CLH A variant of the queue implements , Add the thread that can't get the lock temporarily to the queue , This queue is AQS The abstract expression of . It encapsulates threads that request shared resources as nodes of the queue ( Node ), adopt CAS、 Spin and LockSupport.park() The way , maintain state The state of the variable , Make concurrency synchronous .
5、AQS preliminary
5.1、AQS First time to know
If there is a jam, you need to line up , Queue is necessary to realize queuing
AQS Use one volatile Of int Member variables of type to represent the synchronization state , Through the built-in FIFO Queue to complete the queuing work of resource acquisition, encapsulate each thread to preempt resources into a Node Node to achieve lock allocation , adopt CAS Finish right State Value modification .
5.2、AQS Internal architecture
5.2.1、AQS Oneself
AQS The synchronization state of State Member variables
The acceptance window status of the bank's business processing
Zero means no one , Free state can handle
Greater than or equal to 1, Someone is occupying the window , Wait
CLH queue ( The names of the three cows make up ), For a two-way queue
Waiting customers in the waiting area of the bank
If there is a jam, you need to line up , Queue is necessary to realize queuing
3.5.2、 Inner class Node(Node Class in AQS Intra class )
Node Waiting state waitState Member variables
Other customers in the waiting area ( Other threads ) Waiting state
Each individual in the queue is a Node
static final class Node{
// share
static final Node SHARED = new Node();
// Monopoly
static final Node EXCLUSIVE = null;
// The thread has been cancelled
static final int CANCELLED = 1;
// Subsequent threads need to wake up
static final int SIGNAL = -1;
// wait for condition Wake up the
static final int CONDITION = -2;
// Shared synchronous state acquisition will propagate unconditionally
static final int PROPAGATE = -3;
// For the initial 0, The states are the above
volatile int waitStatus;
// Front node
volatile Node prev;
// The subsequent nodes
volatile Node next;
// At present Node Threads
volatile Thread thread;
// Point to the next in CONDITION Nodes of state
Node nextWaiter;
// ...
5.3、AQS The basic structure of synchronization queue
CLH:Craig、Landin and Hagersten queue , It's a one-way linked list ,AQS The queue in is CLH Virtual two-way queues for variants (FIFO)
6、 From our ReentrantLock Start reading AQS
Large drawing description AQS Source code interpretation _ Shixiaozan's blog -CSDN Blog
Lock Implementation class of interface , Basically through 【 polymerization 】 One. 【 Queue synchronizer 】 The subclass of completes the thread access control
From the simplest lock Methods start to look at fairness and unfairness
It can be clearly seen that fair lock and unfair lock lock() The only difference of the method is that the fair lock has a restriction when it obtains the synchronization state :
hasQueuedPredecessors It is a method to judge whether there is an effective node in the waiting queue when a fair lock is applied
Unfair lock up , Method lock()
Compare the of fair lock and unfair lock tryAcquire() Method implementation code , The difference is When an unfair lock obtains a lock, there is one less judgment than in a fair lock !hasQueuedPredecessors()
hasQueuedPredecessors() To determine whether to queue up , The differences between fair locks and unfair locks are as follows :
Fair lock : Fair lock pays attention to first come, first served , When a thread acquires a lock , If there is already a thread waiting in the waiting queue of this lock , Then the current thread will enter the waiting queue ;
Not fair lock : Whether there is a waiting queue or not , If you can get the lock , Then immediately occupy the lock object . That is to say, the first queue thread of the queue is unpark(), After that, you still need to compete for the lock ( In case of thread contention )
Source code interpretation starts
acquire() Source code and 3 The big process is going to
tryAcquire(arg) This time, the lock is not fair
return false; Continue to advance the conditions , Next step
In a two-way list , The first node is a virtual node ( Also known as sentinel node ), It doesn't store any information , It's just occupation .
The real first node with data , It starts with the second node .
If 3 Number ThreadC The thread in
acquireQueued(addWaiter(Node.EXCLUSIVE), arg)
If you fail again, you will enter
shouldParkAfterFailedAcquire and parkAndCheckInterrupt In the method
If the precursor node's waitStatus yes SIGNAL state , namely shouldParkAfterFailedAcquire Method will return true The program will continue to go down parkAndCheckInterrupt Method , Used to suspend the current thread
边栏推荐
- Linux忘记MySQL密码后修改密码
- Addition of large numbers (C language)
- 9、聊聊ThreadLocal
- Notes on topic brushing (XIV) -- binary tree: sequence traversal and DFS, BFS
- Flink deployment mode and runtime architecture (session mode, single job mode, application mode, jobmanager, taskmanager, yarn mode deployment and runtime architecture)
- Elk - elastalert largest pit
- 深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)
- Solve the problem of swagger document interface 404
- 近期使用nodejs pinyin包时遇到的问题
- 数据如何在 Splunk 中老化?
猜你喜欢

刷题笔记(十四)--二叉树:层序遍历和DFS,BFS

Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr

PS does not display text cursor, text box, and does not highlight after selection

splunk 证书过期 使KV-store不能启动

CentOS installation mysql5.7

gocron 定时任务管理平台

【LeetCode】494. Objective and (2 wrong questions)

Error occurred when MySQL imported the database data in pagoda as 0000-00-00 and enum as null

让你搞懂冒泡排序(C语言)

flink Window Join、Interval Join、Window CoGroup (两流匹配 指定key联结,开窗口进行窗口操作)
随机推荐
[JUC supplementary] immutable object, shared meta mode, final principle
Flick grouping sets multidimensional aggregation and setting table state expiration time
flink 滚动窗口、滑动窗口、会话窗口、全局窗口
Flick controls window behavior (trigger, remover, allow delay, put late data into side output stream)
Use of RadioButton in QT
C # convert ofd to PDF
一些比较的常用网站
Using fast and slow pointer method to solve the problem of array (C language)
Take you to know about direct insertion sorting (C language)
Solve the problem of swagger document interface 404
What is a Gerber file? Introduction to PCB Gerber file
Acwing50+acwing51 weeks +acwing3493 Maximum sum (open)
Fast build elk7.3
Merge two ordered arrays (C language)
Is the SSL certificate reliable in ensuring the information security of the website?
你管这破玩意儿叫 MQ?
Live app development to determine whether the user is logging in to the platform for the first time
You call this shit MQ?
Installation and use of saltstack
Notes on topic brushing (XIV) -- binary tree: sequence traversal and DFS, BFS






























