JDK源码04

AQS中等待队列

说明

AQS中除了提供锁实现的同步队列之外,还有实现线程间的等待/通知机制的机制,而后者是由AQS中内部提供的ConditionObject类实现,与同步队列相同的是,等待队列也是同同步队列一样的FIFO队列,在其实现通过AQS中提供Node类来完成

ConditionObject

addConditionObject

UnlinkCancelledWaiters

Unlinks cancelled waiter nodes from condition queue. Called only while holding lock. This is called when cancellation occurred during condition wait, and upon insertion of a new waiter when lastWaiter is seen to have been cancelled. This method is needed to avoid garbage retention in the absence of signals. So even though it may require a full traversal, it comes into play only when timeouts or cancellations occur in the absence of signals. It traverses all nodes rather than stopping at a particular target to unlink all pointers to garbage nodes without requiring many re-traversals during cancellation storms.
移除在等待队列中取消等待的节点,即移除因线程中断或进入同步队列中的节点

await


等待方法就是将当前线程构造节点加入等待队列,同时唤醒后继节点,其中不用if来判断是否在同步队列是用来避免过早的通知(即在等待中线程中断),被唤醒之后就会尝试获取同步队列的同步状态

fullRelease


fullRelease的实现就是获取当前节点的状态,尝试释放当前节点的所持有的锁,并且唤醒同步队列的后继节点

Signal




总结一下就是:当前线程获取到锁,并且调用signal操作,同时获取等待队列的first节点,将其加入同步对列,通过LockSupport.unpark唤醒在await中等待的节点线程,之后该节点就可以在同步队列中开始竞争锁(acquireQueue)

参考资料

AQS中关于同步队列参考:https://blog.csdn.net/csw3186016056/article/details/119298043?spm=1001.2014.3001.5501
《并发编程的艺术》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值