当前位置:网站首页>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 )
边栏推荐
- Novice entry depth learning | 3-6: optimizer optimizers
- Leetcode study - day 35
- FFT 学习笔记(自认为详细)
- Fibonacci number
- [groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
- KDD 2022 | 脑电AI助力癫痫疾病诊断
- After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
- 小程序容器可以发挥的价值
- esxi的安装和使用
- JVM_ 15_ Concepts related to garbage collection
猜你喜欢

从 1.5 开始搭建一个微服务框架——调用链追踪 traceId

2020.2.13

关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号

VMware Tools installation error: unable to automatically install vsock driver
![Cf:h. maximum and [bit operation practice + K operations + maximum and]](/img/c2/9e58f18eec2ff92e164d8d156629cf.png)
Cf:h. maximum and [bit operation practice + K operations + maximum and]

基於DVWA的文件上傳漏洞測試

vSphere实现虚拟机迁移

ThreeDPoseTracker项目解析

Finding the nearest common ancestor of binary search tree by recursion

Building core knowledge points
随机推荐
Logstash clear sincedb_ Path upload records and retransmit log data
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
devkit入门
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
Xunrui CMS plug-in automatically collects fake original free plug-ins
A preliminary study of geojson
Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
Recursive method converts ordered array into binary search tree
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
VSphere implements virtual machine migration
激动人心,2022开放原子全球开源峰会报名火热开启
MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Study diary: February 13, 2022
RAID disk redundancy queue
Cglib dynamic agent -- example / principle
Getting started with devkit
WordPress collection plug-in automatically collects fake original free plug-ins
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
