当前位置:网站首页>Condition and AQS principle
Condition and AQS principle
2022-07-06 01:10:00 【@Overstep】
Condition brief introduction
Any one of them java Objects are all inherited from Object class , Communication between online programs is often applied to Object Several methods of , such as wait(),wait(long timeout),wait(long timeout, int nanos) And notify(),notifyAll() Several ways to implement wait / A notification mechanism , alike , stay java Lock Under the system, there will still be the same way to achieve waiting / A notification mechanism . On the whole Object Of wait and notify/notify It is to cooperate with the object monitor to complete the inter thread waiting / A notification mechanism , and Condition And Lock Cooperate with the completion waiting notification mechanism , The former is java At the bottom level , The latter is at the language level , It has higher controllability and expansibility . The two are different in use , There are still many differences in functional features :
- Condition Can support no response interrupt , But by using Object The way doesn't support ;
- Condition Can support multiple waiting queues (new Multiple Condition object ), and Object Only one... Can be supported ;
- Condition It can support the setting of timeout time , and Object I won't support it
Condition analysis :
(1)Condition The essence of waiting for notification is waiting queue and The interactive process of synchronizing queues , Follow object Of wait()/notify() The mechanism is the same ;Condition Based on synchronous lock state Realized , and objec Is based on monitor Pattern implementation .
(2) One lock(AQS) There can be multiple Condition, That is, multiple waiting queues , There is only one synchronization queue .
(3)Condition.await() Method execution , Will synchronize the... In the queue head Lock released , Encapsulate threads into new node Add to the waiting queue ;Condition.signal() Method execution , The first node in the waiting queue will be moved to the synchronization queue , Until the lock state It's only when you get it that you wake up .
AQS Principle Overview
It's a framework for building locks and synchronizers , Use AQS A large number of synchronizers can be constructed simply and efficiently
AQS The core idea is , If the requested shared resource is free , Set the thread of the current request resource to a valid worker thread , And set the shared resources to the locked state . If the requested shared resource is occupied , Then we need a set of mechanism for thread blocking waiting and lock allocation when it is awakened , This mechanism AQS Yes, it is CLH The implementation of queue lock , Join the queue with the thread temporarily unable to acquire the lock .
CLH(Craig,Landin,andHagersten) A queue is a virtual two-way queue ( Virtual two-way queue means there is no queue instance , There is only an association between nodes ).AQS It is to encapsulate each thread requesting shared resources into a CLH A node in a lock queue (Node) To achieve lock allocation .
AQS Define two ways to share resources
1)Exclusive( Monopoly )
Only one thread can execute , Such as ReentrantLock. It can be divided into fair lock and unfair lock ,ReentrantLock Two locks are supported at the same time , Let's say ReentrantLock Introduce the definition of these two kinds of locks :
- Fair lock : In the order in which threads are queued in the queue , First come, first get the lock
- Not fair lock : When a thread wants to acquire a lock , Pass it twice first CAS Operate to grab the lock , If you don't get , The current thread then joins the queue waiting for wakeup .
2)Share( share )
边栏推荐
- The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
- Obstacle detection
- File upload vulnerability test based on DVWA
- 测试/开发程序员的成长路线,全局思考问题的问题......
- Vulhub vulnerability recurrence 74_ Wordpress
- 2020.2.13
- Logstash clear sincedb_ Path upload records and retransmit log data
- China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
- Zhuhai's waste gas treatment scheme was exposed
- 朝招金安全吗 会不会亏损本金
猜你喜欢

Differences between standard library functions and operators

Vulhub vulnerability recurrence 74_ Wordpress

MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning

cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
![Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]](/img/9e/c933f454a39d906a407e4d415f0b87.png)
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]

WordPress collection plug-in automatically collects fake original free plug-ins

KDD 2022 | EEG AI helps diagnose epilepsy
![[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)](/img/52/021931181ad3f4bef271b4e98105c2.jpg)
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
![[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)](/img/09/9076de099147b2d0696fe979a68ada.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)

VSphere implements virtual machine migration
随机推荐
基於DVWA的文件上傳漏洞測試
Live broadcast system code, custom soft keyboard style: three kinds of switching: letters, numbers and punctuation
ThreeDPoseTracker项目解析
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
Overview of Zhuhai purification laboratory construction details
Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
esxi的安装和使用
Use of crawler manual 02 requests
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
看抖音直播Beyond演唱会有感
Introduction to robotics I. spatial transformation (1) posture, transformation
Daily practice - February 13, 2022
DD's command
Leetcode study - day 35
Leetcode 44 Wildcard matching (2022.02.13)
cf:C. The Third Problem【关于排列这件事】
Building core knowledge points
Arduino hexapod robot
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
