当前位置:网站首页>[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");
}边栏推荐
- Osgearth target selection
- Mall management system of database application technology course design
- Use of ue5 QRcode plug-in
- 數據庫應用技術課程設計之商城管理系統
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- 【更新中】微信小程序学习笔记_3
- Creation and content of mapnode -- osgearth rendering engine series (2)
- [cloud native] introduction and use of feign of microservices
- Golang's range
- 【Rust 笔记】11-实用特型
猜你喜欢
随机推荐
Image processing 8-cnn image classification
UE4 source code reading_ Bone model and animation system_ Animation process
UE4 call DLL
VIM learning notes from introduction to silk skating
图像处理8-CNN图像分类
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Unity editor expansion - the framework and context of unity imgui
Graphics_ Learnopongl learning notes
请求参数的发送和接收
Intersectionpicker in osgearth
Unity Editor Extension - drag and drop
swagger文档配置
简易入手《SOM神经网络》的本质与原理
Data analysis exercises
Unity4.3.1 engine source code compilation process
KunlunBase MeetUP 等您来!
【Rust 笔记】11-实用特型
Redis data structure
【Rust 笔记】07-结构体
UE4 source code reading_ Bone model and animation system_ Animation compression
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)








