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
《并发编程的艺术》