前言
在上一篇文章分析独占锁的获取过程中,我们分析了独占锁的获取过程。相对于获取过程,独占锁的释放过程则相对简单了很多。
Lock lock = new ReentrantLock();
...
lock.lock();
try {
// 搞事情
} finally {
lock.unlock();
}
对于ReentrantLock的使用,一定要注意将unlock操作写在finally块里,以保证锁的释放。这种手法我们已经在doAcquireInterruptibly里见过,在此函数里,即使抛出中断异常也会在异常抛给上一层调用函数之前,先去执行完cancelAcquire后,再将异常抛给上一层函数。
同样的,本文将以ReentrantLock的视角分析独占锁的释放过程。
release
//ReentrantLock
public void unlock() {
sync.release(1);
}
//AbstractQueuedSynchronizer
public final boolean release(int arg) {
//尝试释放锁
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
//唤醒下一个节点
unparkSuccessor(h);
return true;
}
return false;
}
- 尝试释放锁tryRelease,这是子类的实现。
- h !=
null标准地防止空指针,不过这种情况肯定也有,比如从头到尾都只有一个线程在使用锁,那么队列也不会初始化,head肯定为null。 - 使用h.waitStatus != 0作为唤醒head后继的判断标准,当队列只有一个dummy
node时,它的状态为0,也就不会执行unparkSuccessor(h)了。当head的状态为SIGNAL时,说明head后继已经设置了闹钟,会执行unparkSuccessor(h)。
tryRelease
protected final boolean tryRelease(int releases) {
int c = getState() - releases;
//当前线程不为独占线程则抛出异常
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
boolean free = false;
1、c 为0 时,将独占锁释放,设为null,不为0是说明是重入锁,多次上锁
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
- if (Thread.currentThread() !=getExclusiveOwnerThread())有了这个判断,没有持有锁的线程调用了ReentrantLock.unlock()就会抛出异常。只有持有锁的线程,才会继续往下执行。
- 该函数执行完毕,同步器的状态会变成c。如果c不为0,说明重入的锁还没有返回足够多的次数,返回false;如果c为0,说明独占锁已经被释放,设置ExclusiveOwnerThread成员为null,并返回true
unparkSuccessor
private void unparkSuccessor(Node node) {
/*
* If status is negative (i.e., possibly needing signal) try
* to clear in anticipation of signalling. It is OK if this
* fails or if status is changed by waiting thread.
*/
1、获取node的waitStatus
int ws = node.waitStatus;
2、小于0在独占锁中则为SIGNAL,设置为0
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
/*
* head后继一般能直接通过next找到,但只有prev是肯定有效的。
* 所以遇到next为null,肯定需要从队尾的prev往前找。
* 遇到next的状态为取消,也需要从队尾的prev往前找。
*/
3、从tail往前遍历,直到找到最接近node的可用节点
Node s = node.next;
if (s == null || s.waitStatus > 0) {
s = null;
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
4、唤醒node后面的第一个可用节点
if (s != null)
LockSupport.unpark(s.thread);
}
-
首先会尝试设置状态从小于0变成0。一般可以这样认为,如果head的状态为0,代表head后继线程即将被唤醒,或者已经被唤醒。
-
如果遇到s == null,说明我们遇到一种中间状态,next指针还没有指好。如果遇到s.waitStatus >0,说明head后继刚取消了。这两种情况,都需要从队尾的prev往前找。关于prev的有效性,已经在上一篇博客讲过了。
-
注意循环条件t != null && t !=node,它会从队尾一直往前找,直到t是null或t已经到达了node。一般情况下,不会出现t !=null,所以,这样循环肯定能找到node之后第一个不是取消状态的节点。
线程在哪被唤醒
从哪被锁就从哪唤醒,所以需要找到在哪执行的lock方法。也就是parkAndCheckInterrupt方法中。
final boolean acquireQueued(final Node node, long arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt()) //我们当初阻塞在这里了!!!
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
- head后继当初是阻塞在了parkAndCheckInterrupt()这里,根据它的返回值,我们将设置interrupted变量,以在返回用户代码之前,补上中断状态。
- 被唤醒的线程将开始下一次循环,最重要的,会再一次执行p == head &&
tryAcquire(arg),考虑本文的流程且没有别的线程竞争的话,此处的tryAcquire必定能成功。
参考链接:https://blog.csdn.net/anlian523/article/details/106515311