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 ...
随机推荐
- ElasticSearch-hard插件及IK分词器安装
ElasticSearch-hard插件及IK分词器安装 编辑 通过上一篇学习,我们学会了ElasticSearch的安装及访问到了如下页面: 编辑 ElasticSearch-head插件安 ...
- the request was rejected because no multipart boundary was found
报错: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.t ...
- 【YashanDB知识库】yasql登录报错:YAS-00413
[问题分类]错误码处理 [关键字]yasql,00413 [问题描述]使用工具设置不同并发迁移数据的过程中,导致yasql登录报错:YAS-00413 [问题原因分析]工具使用与数据库使用资源超过了操 ...
- C++ 性能反向优化——用哈希表unordered_map消除if else导致性能降低。
从代码整洁的角度考虑,对于不同的值将调用相同参数的不同函数,我们通常可以通过建立从值到对应函数指针的哈希表,从而将if else消除.但实际可能使性能更低,以下是测试例子. 原因在于,if else分 ...
- .NET 8 + Vue/UniApp 高性能前后端分离框架
前言 作为一名开发者,我们知道能够简化开发流程.提升工作效率的工具是至关重要的. 推荐一款前后端分离框架 Admin.NET(ZRAdmin),它不仅可以满足项目开发的需求,还应用了一些新的特性,如R ...
- HBase集群安装及集成
Hbase集群搭建 注意:在安装hbase或者hadoop的时候,要注意hadoop和hbase的对应关系.如果版本不对应可能造成系统的不稳定和一些其他的问题.在hbase的lib目录下可以看到had ...
- manim边学边做--形状匹配
manim中有几个特殊的用于形状匹配的对象,它们的作用是标记和注释已有的对象,本身一般不单独使用. 形状匹配对象一共有4种: BackgroundRectangle:为已有的对象提供一个矩形的背景 C ...
- 对3D图像进行裁剪
在对医学图像进行深度学习的过程中,我们会遇到图片过大,导致train的过程中网络会瘫痪,所以我们会考虑到对图像进行分割.比如一张155x240x240的图像,我们可以将他分割成一系列128x128x1 ...
- CSharp的@microsoft/signalr实时通信教程 - 前端 vue
1. 安装@microsoft/signalr pnpm install @microsoft/signalr --save signalr 是微软对 websocket技术的封装,优化了操作 :1. ...
- HN CSP-J 2023 奇人鉴赏
其中有 4 位同学提到了IOI 一位同学提到了 fk,但是并没有 Fk CCF 共有52个 CCF,其中HN-J00157同学复制了很多遍题目一位同学用了ccf当 struct 名字,并且写出了人名函 ...