AbstractQueuedSynchronizer源码解析之ReentrantLock(二)
上篇文章分析了ReentrantLock的lock,tryLock,unlock方法,继续分析剩下的方法,首先开始lockInterruptibly,先看其API说明:
lockInterruptiblypublic void lockInterruptibly()
throws Acquires the lock unless the current thread is .
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
- The lock is acquired by the current thread; or
- Some other thread the current thread.
文档说明,只要线程没有处于interrupted状态,就尝试去获取锁,直到获取到锁,或者被interrupted。下面看其源码实现:
public void lockInterruptibly() throws InterruptedException {
sync.acquireInterruptibly(1);
}
acquireInterruptibly方法在AbstractQueuedSynchronizer中:
/**
* Acquires in exclusive mode, aborting if interrupted. Implemented by first
* checking interrupt status, then invoking at least once
* {@link #tryAcquire}, returning on success. Otherwise the thread is
* queued, possibly repeatedly blocking and unblocking, invoking
* {@link #tryAcquire} until success or the thread is interrupted. This
* method can be used to implement method {@link Lock#lockInterruptibly}.
*
* @param arg
* the acquire argument. This value is conveyed to
* {@link #tryAcquire} but is otherwise uninterpreted and can
* represent anything you like.
* @throws InterruptedException
* if the current thread is interrupted
*/
public final void acquireInterruptibly(int arg) throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (!tryAcquire(arg))
doAcquireInterruptibly(arg);
}
首先检查interrupted状态,调用tryAcquire方法,失败则:
/**
* Acquires in exclusive interruptible mode.
*
* @param arg
* the acquire argument
*/
private void doAcquireInterruptibly(int arg) throws InterruptedException {
final Node node = addWaiter(Node.EXCLUSIVE);
try {
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
return;
}
if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt())
break;
}
} catch (RuntimeException ex) {
cancelAcquire(node);
throw ex;
}
// Arrive here only if interrupted
cancelAcquire(node);
throw new InterruptedException();
}
这段代码比较眼熟,这也是个阻塞方法,阻塞至node前继是head且exclusively owned synchronizer则返回,如果p is blocked and interrupted,这时线程处于interrupted状态,所以中止.
最后看一下ReentrantLock的newCondition方法,看这个方法之前先看看Doug Lea的论文《The java.util.concurrent Synchronizer Framework》,上面描述如下:
A ConditionObject uses the same internal queue nodes as synchronizers, but maintains them on a separate condition queue.The signal operation is implemented as a queue transfer from the condition queue to the lock queue, without necessarily waking up the signalled
thread before it has re-acquired its lock.The basic await operation is:
create and add new node to condition queue;
release lock;
block until node is on lock queue;
re-acquire lock;
And the signal operation is:
transfer the first node from condition queue to lock queue;
Because these operations are performed only when the lock is held, they can use sequential linked queue operations (using a nextWaiter field in nodes) to maintain the condition queue.
The transfer operation simply unlinks the first node from the condition queue.queue, and then uses CLH insertion to attach it to the lock queue.
继续看代码:
public Condition newCondition() {
return sync.newCondition();
}
newCondition在Sync类中:
final ConditionObject newCondition() {
return new ConditionObject();
}
直接返回一个ConditionObject 对象,ConditionObject 类是AbstractQueuedSynchronizer.java文件中。
来看看其声明的变量:
private static final long serialVersionUID = 1173984872572414699L;
/** First node of condition queue. */
private transient Node firstWaiter;
/** Last node of condition queue. */
private transient Node lastWaiter;
声明两个节点,通过后面的方法知道,这是一个单向链表。继续看其await方法:
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null)
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
检查状态后会调用addConditionWaiter方法:
/**
* Adds a new waiter to wait queue.
*
* @return its new wait node
*/
private Node addConditionWaiter() {
Node t = lastWaiter;
// If lastWaiter is cancelled, clean out.
if (t != null && t.waitStatus != Node.CONDITION) {
unlinkCancelledWaiters();
t = lastWaiter;
}
Node node = new Node(Thread.currentThread(), Node.CONDITION);
if (t == null)
firstWaiter = node;
else
t.nextWaiter = node;
lastWaiter = node;
return node;
}
继续看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.
*/
private void unlinkCancelledWaiters() {
Node t = firstWaiter;
Node trail = null;
while (t != null) {
Node next = t.nextWaiter;
if (t.waitStatus != Node.CONDITION) {
t.nextWaiter = null; //next=null
if (trail == null)
firstWaiter = next;
else
trail.nextWaiter = next;//tail.nextWaiter=null
if (next == null)
lastWaiter = trail;
} else
trail = t;
t = next;
}
}
遍历链表,如果某结点状态不为Node.CONDITION,则移除当前结点及后面的全部结点
addConditionWaiter方法首先会检查lastWaiter是否已经cancelled,是则清除链中cancelled的结点,否则添加当前结点为lastWaiter。
继续看fullyRelease方法:
/**
* Invokes release with current state value; returns saved state. Cancels
* node and throws exception on failure.
*
* @param node
* the condition node for this wait
* @return previous sync state
*/
final int fullyRelease(Node node) {
try {
int savedState = getState();
if (release(savedState))
return savedState;
} catch (RuntimeException ex) {
node.waitStatus = Node.CANCELLED;//如果发生异常则将当前结点为置于CANCELLED状态
throw ex;
}
// reach here if release fails
node.waitStatus = Node.CANCELLED;
throw new IllegalMonitorStateException();
}
调用release方法交出synchronized排它持有权并unblock sync queue下一个结点,最后返回savedState 值。
继续看下面的方法:
/**
* Returns true if a node, always one that was initially placed on a
* condition queue, is now waiting to reacquire on sync queue.
*
* @param node
* the node
* @return true if is reacquiring
*/
final boolean isOnSyncQueue(Node node) {
if (node.waitStatus == Node.CONDITION || node.prev == null)
return false; //node在condition queue中
if (node.next != null) // If has successor, it must be on queue
return true; //node有前继结点也有后续结点,那么它在sync queue上,但node结点不是tail
/*
* node.prev can be non-null, but not yet on queue because the CAS to
* place it on queue can fail. So we have to traverse from tail to make
* sure it actually made it. It will always be near the tail in calls to
* this method, and unless the CAS failed (which is unlikely), it will
* be there, so we hardly ever traverse much.
*/
return findNodeFromTail(node);
}
/**
* Returns true if node is on sync queue by searching backwards from tail.
* Called only when needed by isOnSyncQueue.
*
* @return true if present
*/
private boolean findNodeFromTail(Node node) {
Node t = tail;
for (;;) {
if (t == node)
return true;
if (t == null)
return false;
t = t.prev;//从tail结点开始,逆序遍历查找node结点,成功则返回true
}
}
await会在while循环中判断:
/**
* Checks for interrupt, returning THROW_IE if interrupted before
* signalled, REINTERRUPT if after signalled, or 0 if not interrupted.
*/
private int checkInterruptWhileWaiting(Node node) {
return (Thread.interrupted()) ? ((transferAfterCancelledWait(node)) ? THROW_IE : REINTERRUPT) : 0;
}
/**
* Transfers node, if necessary, to sync queue after a cancelled wait.
* Returns true if thread was cancelled before being signalled.
*
* @param current
* the waiting thread
* @param node
* its node
* @return true if cancelled before the node was signalled.
*/
final boolean transferAfterCancelledWait(Node node) {
if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
enq(node);//如果node状态为CONDITION,插入了sync queue尾部
return true;
}
/*
* If we lost out to a signal(), then we can't proceed until it finishes
* its enq(). Cancelling during an incomplete transfer is both rare and
* transient, so just spin.
*/
while (!isOnSyncQueue(node))//如果node不在sync queue中,那么让其他线程执行
Thread.yield();
return false;
}
如果当前node状态为Node.CONDITION,则会把该node添加到sync queue;如果当前线程interrupted,返回THROW_IE
现在回顾一下await方法所进行的操作:
1.首先调用addConditionWaiter方法将当前线程以Node.CONDITION模式添加到condition queue中,如果队列lastWaiter处于cancelled状态,遍历队列移除cancelled状态的结点
2.调用fullyRelease方法交出synchronizer排它持有权并unpark sync queue的head后续结点,并返回之前的state值
3.循环判断当前node是否在sync queue上,不在则park当前线程,并检测当前线程是否处于interrupted状态,是则中断循环
4.调用acquireQueued阻塞方法,如果当前线程在signalled之前interrupted,设置interruptMode
5.如果node nextWaiter不为空,则移除condition queue上cancelled的结点。
6.最后检测interruptMode,抛出异常或interrupt自身
继续看其signal方法:
/**
* Moves the longest-waiting thread, if one exists, from the wait queue
* for this condition to the wait queue for the owning lock.
*
* @throws IllegalMonitorStateException
* if {@link #isHeldExclusively} returns {@code false}
*/
public final void signal() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);//取出condition queue第一个结点
}
检测当前线程是否exclusively owned synchronizer,调用doSignal方法:
/**
* Removes and transfers nodes until hit non-cancelled one or null.
* Split out from signal in part to encourage compilers to inline the
* case of no waiters.
*
* @param first
* (non-null) the first node on condition queue
*/
private void doSignal(Node first) {
do {
if ((firstWaiter = first.nextWaiter) == null)
lastWaiter = null;
first.nextWaiter = null;
} while (!transferForSignal(first) && (first = firstWaiter) != null);
//注意这个循环条件,每次都将firstWaiter赋给first,这样保证操作的始终是队列中第一个结点即等待时间最长的线程
}
遍历condition queue,如果存在空结点,则null lastWaiter,
/**
* Transfers a node from a condition queue onto sync queue. Returns true if
* successful.
*
* @param node
* the node
* @return true if successfully transferred (else the node was cancelled
* before signal).
*/
final boolean transferForSignal(Node node) {
/*
* If cannot change waitStatus, the node has been cancelled.
*/
if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
return false;//如果不是CONDITION状态,不会transfer node到sync queue上
/*
* Splice onto queue and try to set waitStatus of predecessor to
* indicate that thread is (probably) waiting. If cancelled or attempt
* to set waitStatus fails, wake up to resync (in which case the
* waitStatus can be transiently and harmlessly wrong).
*/
Node p = enq(node);
int c = p.waitStatus;
if (c > 0 || !compareAndSetWaitStatus(p, c, Node.SIGNAL))
LockSupport.unpark(node.thread);
return true;
}
如果当前node状态为Node.CONDITION,则将其插入到sync queue中并返回状态,如果状态值为cancelled,或者状态有变化,则unpark node所含的线程。
现在回顾一下singal方法所进行的操作:
1.检测
2.循环,反复调用transferForSignal操作condition queue中第一个结点,直到成功。
最后再看看signalAll方法的源码:
/**
* Moves all threads from the wait queue for this condition to the wait
* queue for the owning lock.
*
* @throws IllegalMonitorStateException
* if {@link #isHeldExclusively} returns {@code false}
*/
public final void signalAll() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignalAll(first);
}
/**
* Removes and transfers all nodes.
*
* @param first
* (non-null) the first node on condition queue
*/
private void doSignalAll(Node first) {
lastWaiter = firstWaiter = null;
do {
Node next = first.nextWaiter;
first.nextWaiter = null;
transferForSignal(first);
first = next;
} while (first != null);
}
遍历调用transferForSignal unpark结点所含的线程。
ReentrantLock的源码分析就到这里了,接下来想继续看看Semaphore,Latches等源码。
AbstractQueuedSynchronizer源码解析之ReentrantLock(二)的更多相关文章
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- AbstractQueuedSynchronizer源码解析
1.简介 AbstractQueuedSynchronizer队列同步器,用来实现锁或者其他同步组件的基础框架 AbstractQueuedSynchronizer使用int类型的volatile变量 ...
- [spring源码] 小白级别的源码解析ioc(二)
之前一篇,整体描述了一下 Spring的整体概况和 jar包的介绍. 现在开始进入具体的源码解析,从本篇开始,先介绍spring的ioc容器.之前也看过一些介绍spring源码的, 有的是只讲整体的接 ...
- JDK8源码解析 -- HashMap(二)
在上一篇JDK8源码解析 -- HashMap(一)的博客中关于HashMap的重要知识点已经讲了差不多了,还有一些内容我会在今天这篇博客中说说,同时我也会把一些我不懂的问题抛出来,希望看到我这篇博客 ...
- MyBatis源码解析(十二)——binding绑定模块之MapperRegisty
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6758456.html 1.回顾 之前解析了解析模块parsing,其实所谓的解析模块就是为 ...
- mybatis源码-解析配置文件(二)之解析的流程
目录 1. 简介 2. 配置文件解析流程分析 2.1 调用 2.2 解析的目的 2.3 XML 解析流程 2.3.1 build(parser) 2.3.2 new XMLConfigBuilder( ...
- Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?
不知道一些同学有没有这种疑问,为什么Mybtis中要配置dataSource,Spring的事务中也要配置dataSource?那么Mybatis和Spring事务中用的Connection是同一个吗 ...
- bitcoin 源码解析 - 交易 Transaction(二) - 原理篇
这篇文章我断断续续写了呃···· 应该快三个星期了? 所以前后的风格可能差别相当大.真是十分的怠惰啊··· 最近实在是不够努力.用python重写bitcoin的项目也卡在网络编程部分(这方面真是我的 ...
- AbstractQueuedSynchronizer源码分析(ReentrantLock锁的实现)
1. 前言 Java中好多地方用到AbstractQueuedSynchronizer(PS:简称AQS),比如ReentrantLock.线程池,这部分在面试的时候也经常被问到,今天以Reentr ...
- Redis源码解析:21sentinel(二)定期发送消息、检测主观下线
六:定时发送消息 哨兵每隔一段时间,会向其所监控的所有实例发送一些命令,用于获取这些实例的状态.这些命令包括:"PING"."INFO"和"PUBLI ...
随机推荐
- Python 串口
ref: https://www.cnblogs.com/zhicungaoyuan-mingzhi/p/12303229.html
- 2023.7.2-3-4Mssql xp_cmdshell提权
1.概念 Mssql和SQL sever的一个产品的不同名称.都属于微软公司旗下.而上述Mssql xp_cmdshell提权也属于数据库提权的一种. 主要依赖于sql server自带的存储过程. ...
- 苹果(ios)打包证书下载
这里,首先需要明确的是,苹果打包证书不能共用,因此证书下载是只能下载自己的证书,不是去下载别人的证书. 那么自己的证书又是如何生成的呢?去什么地方下载呢?第一次开发ios的同学们,肯定会问这个问题. ...
- VS code常用插件安装【持续更新】
Auto Close Tag 自动添加HTML/XML关闭标签.例如,在输入<div>时,输入完最后一个尖括号>时,会自动添加对应的闭合标签</div> Auto Ren ...
- Angular Material 18+ 高级教程 – Material Tooltip
前言 一个常见的 Tooltip 使用场景是 当有 ellipsis 时,hover 显示全文. Tooltip 算是一种 Popover,我们之前有讲过,要搞 Popover 可以使用底层的 CDK ...
- Angular 18+ 高级教程 – 目录
请按顺序阅读 关于本教程 初识 Angular Get Started Angular Compiler (AKA ngc) Quick View Dependency Injection 依赖注入 ...
- ASP.NET Core – HttpClient
前言 以前写过的文章 Asp.net core 学习笔记 ( HttpClient ). 其实 HttpClient 内容是挺多的, 但是我自己用到的很少. 所以这篇记入一下自己用到的就好了. 参考 ...
- find基础命令与提权教程
关于我 博客主页:https://mp.weixin.qq.com/mp/homepage?__biz=Mzg2Nzk0NjA4Mg==&hid=2&sn=54cc29945318b7 ...
- [30] CSP 加赛 1
A.小W与伙伴招募 考虑贪心,可以发现,每一天只需要优先选择价值低的即可 这种贪心思路有一个错误的扩展,就是先把 \(m\) 天的货一次性补齐再一次性买,这样做的问题在于有可能买到次日的货,而这样做是 ...
- PHP面试,拼团
如何设计数据库模型来支持拼团功能? 答案:拼团功能涉及到多个用户参与同一团的情况,可以设计以下表结构: Product 表: 存储商品信息,包括商品ID.名称.价格等字段. Group 表: 存储拼团 ...