当前位置:网站首页>[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");
}
边栏推荐
- Display terrain database on osgearth ball
- Unity editor expansion - window, sub window, menu, right-click menu (context menu)
- Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
- Conversion between golang JSON format and structure
- [concurrent programming] Table hopping and blocking queue
- Unity editor expansion - the framework and context of unity imgui
- [concurrent programming] working mechanism and type of thread pool
- Unity notes 1
- Osgearth starry background
- Introduction to hexadecimal coding
猜你喜欢
Ue5 opencv plug-in use
P1596 [USACO10OCT]Lake Counting S
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Introduction to Base64 coding
XPath实现XML文档的查询
Simple demo of solving BP neural network by gradient descent method
Dealing with duplicate data in Excel with xlwings
Graphics_ Games101/202 learning notes
jupyter远程服务器配置以及服务器开机自启
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
随机推荐
796 · 开锁
UE4 call DLL
Cesium for unreal quick start - simple scenario configuration
Markdown directory generation
请求参数的发送和接收
Transmit pictures with Base64 encoding
Minimap plug-in
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
Introduction to Base64 coding
Graphics_ Games101/202 learning notes
如何应对数仓资源不足导致的核心任务延迟
单调栈-84. 柱状图中最大的矩形
【Rust笔记】05-错误处理
Base64 and base64url
UE4 source code reading_ Bone model and animation system_ Animation node
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
C course design employee information management system
Pit & ADB wireless debugging of vivo real machine debugging
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation