当前位置:网站首页>[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");
}边栏推荐
猜你喜欢

分配异常的servlet

Unity4.3.1 engine source code compilation process

Gradle's method of dynamically modifying APK package name

Unity editor expansion - draw lines

Vscode, idea, VIM development tool shortcut keys

Explain sizeof, strlen, pointer, array and other combination questions in detail

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

【云原生】微服务之Feign的介绍与使用

Markdown learning

jupyter远程服务器配置以及服务器开机自启
随机推荐
Monotonic stack -503 Next bigger Element II
LinkedList set
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Servlet的生命周期
Introduction to hexadecimal coding
P1596 [USACO10OCT]Lake Counting S
Image processing 8-cnn image classification
Vscode, idea, VIM development tool shortcut keys
Advanced OSG collision detection
Dom4j遍历和更新XML
Unity Editor Extension - event handling
[public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
796 · 开锁
【Rust 笔记】13-迭代器(上)
Message pack in C deserializes array objects
【Rust 笔记】10-操作符重载
【Rust 笔记】11-实用特型
[redis] redis persistent RDB vs AOF (source code)
UE4 plug in development