-
returning on success. Otherwise the thread is queued, possibly
-
repeatedly blocking and unblocking, invoking {@link
-
#tryAcquire} until success. This method can be used
-
to implement method {@link Lock#lock}.
-
@param arg the acquire argument. This value is conveyed to
-
{@link #tryAcquire} but is otherwise uninterpreted and
-
can represent anything you like.
*/
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
//那我们先进入到 tryAcquire(arg)方法,查看获取锁的逻辑,该方法不阻塞。
protected boolean tryAcquire(int arg) { // 说明,该方法在具体的子类中实现。
throw new UnsupportedOperationException();
}
我们一路跟踪进来,发现尝试获取锁的代码在 ReentrantLock内部类 Sync 汇总,Sync 是 NonFairSync 和 FairSync 的父类。
/**
-
Performs non-fair tryLock. tryAcquire is
-
implemented in subclasses, but both need nonfair
-
try for trylock method.
*/
final boolean nonfairTryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) { // @1
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) { // @2
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error(“Maximum lock count exceeded”);
setState(nextc);
return true;
}
return false;
}
该方法,尝试获取锁,如果成功获取锁,则返回true,否则,返回false;
重点关注 代码@1, 再次查看 锁的 state,该字段,表示该锁被占用的次数,如果为0,表示没有线程持有该锁,如果 大于1,表示同一个线程,多次请求锁;也就是可重入锁的实现原理。
代码@2:进一步说明可重入锁的实现机制。再次回到上文提到的 AbstractQueuedSynchronizer的 acquire(arg)方法:
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
}
如果 tryAcquire(arg) 返回 true,则不会执行 acquireQueued,表示成功获取锁,如果 tryAcquire(arg) 返回 false, 说明没有成功获取锁,则加入请求队列中。接着请看 addWaiter (Node.EXCLUSIVE) 方法。
addWaiter 中涉及的逻辑,就是 CLH 思想的实现,故在 AbstractQueuedSynchronizer 中,源码如下:
/**
-
Creates and enqueues node for current thread and given mode.
-
创建并入队一节点,为当前线程和给定的模式, Node.EXCLUSIVE 独占模式
-
@param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
-
@return the new node
*/
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
Node pred = tail;
if (pred != null) { //@1 start
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
} //@1 end
enq(node);
return node;
}
对于上面的代码@1,处说,如果当前该锁的尾部节点不为空时,只需要原子性的将新增节点放入原先的尾部,然后更新锁的 tail 属性即可。如果尾部节点不为空,说明有线程已经在该锁上等待,那如果尾部为空,是什么情况呢?尾部为空,表示没有线程持有锁,为什么该获取锁没有成功呢?我们不妨设想一下,该线程在没有执行到 addWaiter 时,尾部不为空,无法获取锁,当执行到 addWaiter 时,别的线程释放了锁,导致尾部为空,可以重新获取锁了;(其实这个就是并发编程的魅力,与 synchronized 关键字不同的机制);为了解答上述疑问,我们进入到 enq(node) 方法中一探究竟。
/**
-
Inserts node into queue, initializing if necessary. See picture above.
-
@param node the node to insert
-
@return node’s predecessor
*/
private Node enq(final Node node) {
for (;😉 {
Node t = tail;
if (t == null) { // Must initialize @1
if (compareAndSetHead(new Node()))
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
使用自旋来加入,众所周知,CLH算法,需要初始化一个假的 head 节点,也就是 head 节点并不代表一个等待获取锁的对象,AbstractQueuedSynchronzier 选择初始化 head,tail 的时机为第一次产生锁争用的时候。@1处为初始化head,tail,设置成功后,初始化后,再将新添加的节点放入到队列的尾部,然后该方法会返回原先的尾节点。addWaiter方法执行后,继续回到acquire(args)方法处:
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
接下来,查看 acquireQueued 方法,addWaiter 方法返回的是代表当前线程的 Node 节点。
/**
-
Acquires in exclusive uninterruptible mode for thread already in
-
queue. Used by condition wait methods as well as acquire.
-
@param node the node
-
@param arg the acquire argument
-
@return {@code true} if interrupted while waiting
*/
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;😉 {
final Node p = node.predecessor(); // @1
if (p == head && tryAcquire(arg)) { // @2
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt() ) //@3
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
首先@1,获取该节点的 node 的上一个节点。
@2如果node的前节点是head,因为head初始化时,都是假节点,不代表有线程拥有锁,所以,再次尝试获取锁,如果获取锁,则将锁的 head 设置为当前获取锁的线程的 Node,然后返回 false。返回 false, 则代表 if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) 的结果为 false,直接返回,并不需要设置中断标记。如果当前节点不是head的话,则说明该锁被别的线程占用了,那就需要等待其他线程释放该锁,具体,我们看一下shouldParkAfterFailedAcquire,为了更好的理解 shouldParkAfterFailedAcquire, 我们先看一下parkAndCheckInterrupt 方法。
/**
-
Convenience method to park and then check if interrupted
-
阻塞该线程,然等待唤醒后,会返回 当前线程的中断位;
-
@return {@code true} if interrupted
*/
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
/**
-
Checks and updates status for a node that failed to acquire.
-
Returns true if thread should block. This is the main signal
-
control in all acquire loops. Requires that pred == node.prev
-
@param pred node’s predecessor holding status
-
@param node the node
-
@return {@code true} if thread should block
该方法,如果返回true,则代表该线程将被阻塞。
*/
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus; // @1
if (ws == Node.SIGNAL) // @2
/*
-
This node has already set status asking a release
-
to signal it, so it can safely park.
*/
return true;
if (ws > 0) { // @3
/*
-
Predecessor was cancelled. Skip over predecessors and
-
indicate retry.
*/
do { // @4 start
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0); //@4 end
pred.next = node; // @5
} else { // @6
/*
-
waitStatus must be 0 or PROPAGATE. Indicate that we
-
need a signal, but don’t park yet. Caller will need to
-
retry to make sure it cannot acquire before parking.
只有前置节点的状态为 0 或 PROPAGATE,才能进入到该代码块,表明我们需要一个信号,但暂不挂起线程,调用者需要重 试一次,确保它不能获取到锁,从而阻塞该线程。
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
@1 首先获取前置节点的 waitStatus。
@2 如果前置节点的waitStatus = Node.SIGNAL,那么当前节点,直接阻塞,说明状态是一个信号,如果前置节点状态为 Node.SIGNAL,那么后续节点应该阻塞的信号量,为什么这么说,情况代码@6,一个节点,新增的时候,为 0 正常。
@3,ws > 0 ,则代表前置节点已取消。
@4 处的代码,就是当前 Node 的第一个不为取消状态的前置节点,重构 CLH 队列后,返回 false, 再次进入到 acquireQueued 的无限循环中,又继续 acquireQueued 的流程,继续尝试获取锁,获取锁,或者阻塞。
@6,如果前置节点为0或 PROPAGATE(可传播),如果前置节点为0,还没有其他节点通过(prev)来判断该 prev 的后继节点是否需要阻塞过,所以,通过 CAS 设置前置节点为 Node.SIGNAL, 重试获取锁过程,避免不必要的线程阻塞。
至此,获取锁的过程就结束了,为了直观体现上述获取锁的过程,现给出如下流程图:
2、ReentrantLock unlock
==========================
public void unlock() {
sync.release(1);
}
//代码直接进入到AbstractQueuedSynchronzier 的 relase方法。
/**
-
Releases in exclusive mode. Implemented by unblocking one or
-
more threads if {@link #tryRelease} returns true.
-
This method can be used to implement method {@link Lock#unlock}.
-
@param arg the release argument. This value is conveyed to
-
{@link #tryRelease} but is otherwise uninterpreted and
-
can represent anything you like.
-
@return the value returned from {@link #tryRelease}
*/
public final boolean release(int arg) {
if (tryRelease(arg)) { @1
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
直接看代码 tryRelease(arg)方法:tryRelease 方法,是由具体的子类实现的,故将目光转移到 NonFairSync 类的 tryRelease() 方法。
protected final boolean tryRelease(int releases) {
int c = getState() - releases; // @1
if (Thread.currentThread() != getExclusiveOwnerThread()) //@2
throw new IllegalMonitorStateException();
boolean free = false;
if (c == 0) { // @3
free = true;
setExclusiveOwnerThread(null);
}
setState©; //@4
return free;
}
代码@1,首先,计算持有锁的次数=当前被持有锁的次数-减去释放的锁的数量;
代码@2,判断当前锁的持有线程释放与释放锁的线程是否相同,否则,直接抛出运行时异常
代码@3,如果释放锁后,占有次数为0,则代表该锁被释放,设置锁的占有线程为null,
代码@4,设置锁的state,如果返回true,表示锁被释放,如果返回false,表示,锁继续被该线程占有(重入了多次,就需要释放多次)。再次回到release方法,如果tryRelease方法返回true,表示可以释放锁,
public final boolean release(int arg) {
if (tryRelease(arg)) { @1
Node h = head;
if (h != null && h.waitStatus != 0) // @2
unparkSuccessor(h);
return true;
}
return false;
}
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
h = head;
if (h != null && h.waitStatus != 0) // @2
unparkSuccessor(h);
return true;
}
return false;
}
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-OzmaPYFr-1710825679438)]
[外链图片转存中…(img-o5lalSSt-1710825679439)]
[外链图片转存中…(img-EX2HGRSf-1710825679439)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-HnUvblVR-1710825679439)]