当前位置:网站首页>[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");
}边栏推荐
- 【Rust笔记】06-包和模块
- UE4 call DLL
- [rust notes] 08 enumeration and mode
- [linear table] basic operation of bidirectional linked list specify node exchange
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
- 数据库原理期末复习
- Location of package cache downloaded by unity packagemanager
- KunlunBase MeetUP 等您来!
- VIM learning notes from introduction to silk skating
- 【K&R】中文第二版 个人题解 Chapter1
猜你喜欢

Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network

Visual Studio (VS) shortcut keys

Introduction to hexadecimal coding

Graphics_ Learnopongl learning notes

Installation of PHP FPM software +openresty cache construction
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership

Minimap plug-in

matlab神经网络所有传递函数(激活函数)公式详解

数据分析练习题

梯度下降法求解BP神经网络的简单Demo
随机推荐
Unity editor expansion - scrolling list
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
LinkedList set
【Rust 笔记】10-操作符重载
[redis] redis persistent RDB vs AOF (source code)
Golang string segmentation, substitution and interception
Redis的数据结构
Downward compatibility and upward compatibility
UE4 plug in development
Development experience and experience
[concurrent programming] concurrent tool class of thread
【Rust 笔记】08-枚举与模式
Go resolve ID card
UE4 source code reading_ Mobile synchronization
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
【Rust笔记】02-所有权
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Golang's range
Gradle's method of dynamically modifying APK package name
Vscode, idea, VIM development tool shortcut keys