当前位置:网站首页>[concurrent programming] explicit lock and AQS
[concurrent programming] explicit lock and AQS
2022-07-03 08:37:00 【keeper42】
An exclusive lock
Reentrant lock
Usual business work , Read more and write less 、 Read / write separation
Read-write lock
Read thread -> Read the lock , Read lock sharing , Do not exclude read lock , But write lock is excluded
Write a thread -> Write lock , Write lock exclusive , Reading and writing are mutually exclusive
Syn wait / notify
Lock Condition await/signal
Explicit lock
Lock Interfaces and core methods
Lock Interface and synchronized Comparison
synchronized The code is concise ,Lock: Obtaining a lock can be interrupted , Timeout lock acquisition , Attempt to acquire lock , Read and write more and use less read-write lock
Reentrant lock ReentrantLock、 The so-called fairness and unfairness of lock
If in time , The request to acquire the lock first , Must be satisfied first , This lock is fair , dissatisfaction , It's not fair
Unfair efficiency is generally higher
ReadWriteLock Interface and read / write lock ReentrantReadWriteLock
ReentrantLock and Syn keyword , All exclusive locks ,
Read-write lock : Allow multiple read threads to access at the same time , But when writing thread access , All reads and writes are blocked , Most suitable for the situation of reading more and writing less
Condition Interface
use Lock and Condition Realize the wait / notice
understand LockSupport Tools
park Opening method : Responsible for blocking threads
unpark(Thread thread) Method : Responsible for waking up threads
AbstractQueuedSynchronizer In depth analysis
What is? AQS?
AQS Usage and design patterns
Inherit , Template method pattern
AQS Methods in the source code
Template method :
Exclusive access
accquire
acquireInterruptibly
tryAcquireNanos
Shared access
acquireShared
acquireSharedInterruptibly
tryAcquireSharedNanos
Exclusive release lock
release
Shared release lock
releaseShared
Process methods that need subclass coverage
Exclusive access tryAcquire
Exclusive release tryRelease
Shared access tryAcquireShared
Shared release tryReleaseShared
Is this synchronizer in exclusive mode isHeldExclusively
sync state:
getState: Get the current synchronization status
setState: Set the current synchronization state
compareAndSetState Use CAS Set the state of , Ensure atomicity of state settings
AQS Data structure in - Nodes and synchronization queues
Threads that fail to compete will be packaged into Node Put it in the synchronization queue ,
Node In the possible state :
CANCELLED: The thread timed out or was interrupted , Need to be removed from the queue
SIGNAL: Subsequent nodes are waiting , Current node , Inform the following nodes to run
CONDITION : The current node is in the waiting queue
PROPAGATE: share , Indicates that the state is to be propagated to the following nodes
0: Represents the initial state
The addition and removal of nodes in the synchronization queue
Nodes join the synchronization queue

Change of node head

Exclusive synchronization state acquisition and release
Acquisition and release of exclusive lock ?
( Release : Traverse backwards from the tail to find the actual non cancelled successor ...)

Condition analysis
await/signal
public void changeKm(){
lock.lock();
try {
this.km = 101;
keCond.signalAll();
}finally {
lock.unlock();
}
}
public void changeSite(){
lock.lock();
try {
this.site = "BeiJing";
siteCond.signal();
}finally {
lock.unlock();
}
}
public void waitKm(){
lock.lock();
try {
while(this.km<=100) {
try {
keCond.await();
System.out.println("check km thread["+Thread.currentThread().getId()
+"] is be notifed.");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}finally {
lock.unlock();
}
System.out.println("the Km is "+this.km+",I will change db");
}
public void waitSite(){
lock.lock();
try {
while(CITY.equals(this.site)) {
try {
siteCond.await();
System.out.println("check site thread["+Thread.currentThread().getId()
+"] is be notifed.");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}finally {
lock.unlock();
}
System.out.println("the site is "+this.site+",I will call user");
}边栏推荐
- [K & R] Chinese Second Edition personal questions Chapter1
- Animation_ IK overview
- Minimap plug-in
- Redis的数据结构
- 图像处理8-CNN图像分类
- Gradle's method of dynamically modifying APK package name
- Clion toolchains are not configured configure disable profile problem solving
- [rust notes] 05 error handling
- [RPC] RPC remote procedure call
- Unity editor expansion - scrolling list
猜你喜欢

VIM learning notes from introduction to silk skating

Monotonic stack -503 Next bigger Element II

详解sizeof、strlen、指针和数组等组合题
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

【更新中】微信小程序学习笔记_3
![[updating] wechat applet learning notes_ three](/img/05/958b8d62d3a42b38ca1a2d8631a7f8.png)
[updating] wechat applet learning notes_ three

UE4 source code reading_ Mobile synchronization

Markdown learning

Graphics_ Learnopongl learning notes

单调栈-84. 柱状图中最大的矩形
随机推荐
Exe file running window embedding QT window
Conversion between golang JSON format and structure
[rust notes] 06 package and module
Creation and content of mapnode -- osgearth rendering engine series (2)
【更新中】微信小程序学习笔记_3
Advanced OSG collision detection
Cesium for unreal quick start - simple scenario configuration
Introduction to hexadecimal coding
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
[concurrent programming] concurrent tool class of thread
详解sizeof、strlen、指针和数组等组合题
Delete the last character of the string in golang
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
Encoding and decoding of golang URL
Intersectionpicker in osgearth
Graphics_ Games101/202 learning notes
LinkList
[K & R] Chinese Second Edition personal questions Chapter1
MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
VIM learning notes from introduction to silk skating