当前位置:网站首页>AQS Abstract queue synchronizer
AQS Abstract queue synchronizer
2022-06-27 14:29:00 【It takes time for fish to find water】
AQS What is it?
Abstract queue synchronizer
AbstractOwnableSynchronizer
AbstractQueuedLongSynchronizer
AbstractQueuedSynchronizer Usually :AbstractQueuedSynchronizer Referred to as AQS
explain :
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 of resource acquisition thread , And through a int Class variables represent 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
AQS Why JUC The most important cornerstone of content
The relationship between lock and synchronizer
lock , For lock users : 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 .
effect
Locking can cause blocking , If there is a jam, you need to line up , Queue is necessary to realize queuing
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 .
AQS preliminary
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 .
AQS Internal architecture
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)
from ReentrantLock Reading AQS
Lock Implementation class of interface , Basically through 【 polymerization 】 One. 【 Queue synchronizer 】 The subclass of completes the thread access control
ReentrantLock Principle
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()
hasQueuedPredecessors It is a method to judge whether there is an effective node in the waiting queue when a fair lock is applied
Not fair lock , 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 )
边栏推荐
- Design skills of main function of Blue Bridge Cup single chip microcomputer
- Pri3d: a representation learning method for 3D scene perception using inherent attributes of rgb-d data
- 重读经典:《The Craft of Research(1)》
- External memory
- Maximum profit of stock (offer 63)
- CMOS level circuit analysis
- [PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities
- 基于SSM的Web网页聊天室系统
- Talk about redis transactions
- my.ini文件配置
猜你喜欢
隐私计算FATE-离线预测
CV领域一代宗师黄煦涛教授86岁冥诞,UIUC专设博士奖学金激励新锐
Buuctf Misc
Redis 主从复制、哨兵模式、Cluster集群
全球芯片市场或陷入停滞,中国芯片逆势扩张加速提升自给率
巧用redis实现点赞功能,它不比mysql香吗?
[microservices sentinel] hotspot rules | authorization rules | cluster flow control | machine list
Pytorch learning 1 (learning documents on the official website)
[business security-04] universal user name and universal password experiment
AutoCAD - line width setting
随机推荐
线程同步之信号量
What is the difference between the FAT32 and NTFS formats on the USB flash disk
[business security-04] universal user name and universal password experiment
[安洵杯 2019]Attack
Why must Oracle cloud customers self test after the release of Oracle cloud quarterly update?
外部存储器
my.ini文件配置
Massive data! Second level analysis! Flink+doris build a real-time data warehouse scheme
每日3题(2):检查二进制字符串字段
NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Redis master-slave replication, sentinel mode, cluster cluster
易周金融 | Q1手机银行活跃用户规模6.5亿;理财子公司布局新兴领域
my. INI file configuration
Longest substring without repeated characters (Sword finger offer 48)
[WUSTCTF2020]girlfriend
隐私计算FATE-离线预测
Deep understanding of bit operations
[an Xun cup 2019]attack
【业务安全-02】业务数据安全测试及商品订购数量篡改实例
Abnormal analysis of pcf8591 voltage measurement data