当前位置:网站首页>AQS-AbstractQueuedSynchronizer
AQS-AbstractQueuedSynchronizer
2022-08-02 11:31:00 【Is the food dish XiaoYan pity ah】
目录
AQSImplemented thread wait queue maintenance method
AQS简介
Previous article finishing,知道了synchronized的使用,以及synchronized的原理,还有管程
The monitor is to ensure concurrency safety by locking,用的比较多的,就是MESA
MESAInternally there are synchronous wait queues and many conditional wait queues,Threads that fail to acquire the lock will enter the synchronization wait queue
而synchronized是JVM层面对管程的实现,是基于对象的,通过ObjectMonitor来实现,获取锁失败的线程,会进入到cxq队列,This is the synchronous wait queue,而wait,notify,notifyAll,It is a conditional waiting queue
但是synchronized也有缺点:自动加锁解锁,Manual unlocking is not supported;Implementation is heavier
这是synchronized的缺点,And there is no way to change it
那,在保证性能的前提下,JavaHow to implement the monitoring process at the level,So the big developer came up with the next thing
并发这块,Used a lot of locks,除了synchronized,很多人都会想到ReentrantLock,而ReentrantLock也是有基础的,那就是基于AQS
AQS是什么,就是AbstractQueuedSynchronizer的简写
java.util.concurrentMost synchronizer implementations in the package,如Lock、Latch、Barrier,All revolve around a common basic behavior,比如等待队列、条件队列、独占获取、shared resources, etc,The abstraction of these behaviors is based onAQS实现的,AQS是一个抽象同步框架,可以用来实现一个依赖状态的同步器
AQS具备的特性
AQS具备如下特性:
- 阻塞等待队列
- 共享/独占
- 公平/非公平
- 可重入
- 允许中断
AQS内部维护属性
volatile int state
state表示资源的可用状态,访问state,有三种方式
getState(), setState(), compareAndSetState()
AQS资源共享方式
AQS定义了两种资源共享方式
- Exclusive-独占,只有一个线程能执行,比如,ReentrantLock
- Share-共享,多个线程可以同时执行,比如,Semaphore、CountDownLatch
AQS队列
AQS有两种队列,They are the synchronous waiting queue and the conditional waiting queue, respectively
The synchronous waiting queue is mainly used to maintain the thread that enters the pair when the acquisition of the mutex fails
Conditional wait queues are mainly used,线程调用await()method to release the lock,进入条件队列,等调用了signal()方法唤醒的时候,Will move the thread node in the condition queue to the synchronization queue,Waiting to reacquire the lock
AQS队列中节点状态
AQSDefines the node state of five threads in the queue
- 值为0,初始化状态,表示当前节点在sync队列中,等待获取锁
- CANCELLED,值为1,表示当前线程被取消
- SIGNAL,值为-1,表示当前节点的后继节点包含的线程需要运行,也就是unpark
- CONDITION,值为-2,表示当前节点在等待condition,也就是在condition队列中
- PROPAGATE,值为-3,Indicates the subsequent ones in the current field sceneacquireshared能够得以执行

AQSImplemented thread wait queue maintenance method
Different custom synchronizers compete for shared resources differently,自定义同步器在实现时只需要实现共享资源stateThe acquisition and release can be done,As for the specific thread waiting for the maintenance of the queue,For example, resource acquisition fails to enter the queue,Wake up to dequeue and so on,AQSAlready implemented at the top level,自定义同步器实现时主要实现以下几种方法
- isHeldExclusively():该线程是否正在独占资源,只有用到了condition才需要去实现它
- tryAcquire(int):独占方式,尝试获取资源,成功就返回true,失败就返回false
- tryRelease(int):独占方式,尝试释放资源,成功就返回true,失败就返回false
- tryAcquireShared(int):共享方式,尝试获取资源,负数表示失败;0表示成功,但是iThere are no remaining resources available;The letter indicates success,And there are remaining resources
- tryReleaseShared(int):共享方式,尝试释放资源,如果释放后允许唤醒后续等待节点返回true,否则返回false
同步等待队列
AQSSynchronous wait queue in ,也称为CLH队列,It is a queue based on a doubly linked list structure,It is a first-in, first-out waiting queue,AQS依赖于CLHQueue to complete the management of the synchronization state,至于为什么叫CLH队列,那是因为,CLH队列是Craig、Landin、HagerstenInvented by three people
如果当前线程获取同步状态失败,AQSIt will construct information such as the current thread's waiting state into a node,并将其加入到CLH队列,同时会阻塞当前线程
当同步状态被释放时,will wake up the first node(公平锁),Make it try to get sync status here
通过signal或者signalAllNodes in the conditional queue can be transferred to the synchronization queue

条件等待队列
AQSMedium condition waiting queues are kept in a one-way list,用nextWaiter来拼接
调用await()方法阻塞线程
当前线程存在于同步队列的头节点,调用await()方法进行阻塞,Thus, the synchronization queue is converted into a conditional queue
Condition
可以将ConditionCommonly understood as a conditional queue
调用Condition#await方法会释放当前持有的锁,然后阻塞当前线程,同时向Condition队列尾部添加一个节点,所以调用Condition#await方法的时候必须持有锁
调用Condition#signal方法会将Condition队列的首节点移动到阻塞队列尾部,然后唤醒因调用Condition#await方法而阻塞的线程(唤醒之后这个线程就可以去竞争锁了),所以调用Condition#signal方法的时候必须持有锁,持有锁的线程唤醒被因调用Condition#await方法而阻塞的线程
边栏推荐
猜你喜欢

配置mysql失败了,这是怎么回事呢?

FinClip | 来了, 2022 年 7 月更新大盘点

看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!

Multithreading (Basic) - 40,000 word summary

ECCV22|PromptDet:无需手动标注,迈向开放词汇的目标检测
![[kali-information collection] (1.9) Metasploit + search engine tool Shodan](/img/d2/6fae03d7597daa908a6816abc34e04.png)
[kali-information collection] (1.9) Metasploit + search engine tool Shodan

CCF paper conference IEEE how to query all articles of a conference journal

细学常用类,集合类,IO流

STM32+MPU6050 Design Portable Mini Desktop Clock (Automatically Adjust Time Display Direction)

Oracle根据时间查询
随机推荐
记一次mysql查询慢的优化历程
华为eNSP(基础实验通信)
Swift中什么时候不能用 () 代替 Void 来使用
OLED的HAL库代码介绍及使用(stm32f1/I2C/HAL库版/100%一次点亮)
ansible module --yum module
QT笔记——在一个窗口上显示另外一个透明窗口
细学常用类,集合类,IO流
图形处理单元(GPU)的演进
WPF 截图控件之文字(七)「仿微信」
org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
配置mysql失败了,这是怎么回事呢?
ansible module --copy module
LeetCode笔记:Weekly Contest 304
循环语句综合练习
基于threejs的商品VR展示平台的设计与实现思路
SQL 经典50题(题目+解答)(1)
有奖征集|TaoCloud&宝德联合举办全闪POC!
jvmxmx和xms参数分析(设定优化校准)
Geoffery Hinton: The Next Big Thing in Deep Learning
【kali-信息收集】(1.8)ARP侦查工具_Netdiscover