本文需要你有 AQS/CAS 知识,如不了解AQS,请跳转链接了解一下:AQS 原理分析
附:可重入锁 ReentrantLock 的使用示例,请参考:可重入锁 ReentrantLock
ReentrantLock 实现方式
有两种实现方式:公平锁 和 非公平锁
公平锁:表示线程获取锁的顺序是按照线程加锁的顺序分配的,即先来先得的 FIFO 先进先出的顺序;
非公平锁:就是一个获取锁的抢占机制,是随机获得锁的,synchronized 就是非公平锁的最好体现;
字面理解:公平锁:站成一列排队;
非公平锁:即站成一行,N多个线程同时争抢。没有顺序可言,也就没有公平可言了。
使用实例
/**
* TODO 可重入锁Demo
*
* @author liuzebiao
* @Date 2019-12-25 15:11
*/
public class ReentrantLockDemo {
Lock lock = new ReentrantLock();//默认为非公平锁,构造器无参/传false, 公平锁需要传 true
/**
* 吃饭
*/
public void eat(){
lock.lock();//通过lock()方法获得锁
try {
System.out.println(Thread.currentThread().getName()+":开始吃饭");
//进入另一个加锁方法
sleep();
} finally {
lock.unlock();
}
}
/**
* 睡觉
*/
public void sleep(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+":开始睡觉");
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
ReentrantLockDemo demo = new ReentrantLockDemo();
new Thread(()->{
demo.eat();
},"Mary").start();
new Thread(()->{
demo.eat();
},"Lucy").start();
}
}
//测试结果:
//Mary:开始吃饭
//Mary:开始睡觉
//Lucy:开始吃饭
//Lucy:开始睡觉
源码分析
0.源码调用时序图
1.公平锁 Vs 非公平锁
//new ReentrantLock(),默认无参为非公平锁
public ReentrantLock() {
sync = new NonfairSync();
}
//[无参]或者[false]为非公平锁;[true]为公平锁
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
2.我们从获得锁方法 lock() 开始分析
跳转分析,调用 sync.lock() 方法有两个实现。具体调用哪个实现方法,取决于你使用的是公平锁还是非公平锁。本文以非公平锁为例来进行分析,此处选择 NonfairSync 类中的 lock() 方法分析。【FairSync 中 lock()方法类似 NonfairSync,文末介绍】
3.NonfairSync 类中 lock()方法获得锁
/**
* Sync object for non-fair locks
*/
static final class NonfairSync extends ReentrantLock.Sync {
private static final long serialVersionUID = 7316153563782823691L;
/**
* Performs lock. Try immediate barge, backing up to normal
* acquire on failure.
*/
final void lock() {
/*
通过CAS算法去改变state的值
state=0表示无锁状态,state>0表示有锁状态
*/
if (compareAndSetState(0, 1))
//exclusiveOwnerThread属性是AQS从父类AbstractOwnableSynchronizer中继承的属性,改属性用来保存当前占用锁的线程,设置成一个独占锁
setExclusiveOwnerThread(Thread.currentThread());
else
//调用AQS中的acquire()方法,尝试去获得锁
acquire(1);
}
}
通过 compareAndSetState() 方法来修改 state 的值。
当锁空闲时,设置 exclusiveOwnerThread 属性来保存当前占用锁的线程;当锁被占用时,执行acquire(1),调用AQS类中的 acquire() 来尝试去获得锁。以下都是对 acquire() 这个方法的详细分析
4.acquire() 方法分析
public final void acquire(int arg) {
//调用tryAcquire()方法获得锁,成功则直接返回
//如果没有获得锁,则会继续调用 accquireQueued() 和 addWaiter() 方法
//addWaiter()方法,是加入到等待队列的一个方法。即:把当前线程封装成Node,并添加到队列的尾部
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(AbstractQueuedSynchronizer.Node.EXCLUSIVE), arg))
selfInterrupt();
}
1.tryAcquire()方法分析
此时调用到 NonfairSync 类中的 tryAcquire() 方法
/**
* Sync object for non-fair locks
*/
static final class NonfairSync extends ReentrantLock.Sync {
//省略部分代码
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
}
此时调用到 Sync 内部类中的 nonfairTryAcquire() 方法
/**
* 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();
//获取当前线程的状态 state,默认情况下是 0 表示无锁状态
int c = getState();
//如果当前状态 == 0,为无锁状态
if (c == 0) {
//通过CAS来改变state 的值,如果更新成功,表示获取锁成功
//compareAndSetState(),这个操作外部方法lock()就调用过一次,这里再做只是为了再尝试一次,尽量以最简单的方式获取锁。
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}//如果当前线程等于获取锁的线程,表示重入,直接累加重入次数
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
//如果 state 不为0,且当前线程不是 owner,则返回false
return false;
}
2.addWaiter(Node mode)方法分析
/**
* Creates and enqueues node for current thread and given mode.
* 为当前线程创建一个队列,并未当前节点提供 mode (mode即当前节点的状态)
* @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
* @return the new node
*/
private Node addWaiter(Node mode) {
//将当前线程封装成一个 Node 节点,mode是独占锁(Node.EXCLUSIVE)
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
//尝试快速入队
//tail是AQS的中表示同步队列队尾的属性,刚开始为null,所以进行enq(node)方法
Node pred = tail;
if (pred != null) {
node.prev = pred;
// 防止有其他线程修改tail,使用CAS进行修改,保证线程安全
if (compareAndSetTail(pred, node)) {
//如果设置tail成功,旧的tail的next指针将指向新的tail,成为双向链表
pred.next = node;
return node;
}
}
//队列为null时,进行enq(node)方法,将当前节点加入到队列中。enq()方法,最终返回的是头结点(为什么是头结点,看enq()方法)
enq(node);
return node;
}
调用 enq() 方法
/**
* 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(;;)而不用while(true)?是因为for循环生成的执行指令比while少,不占用寄存器
for (;;) {
//第一次循环时,head, tail都为null
Node t = tail;
//如果tail为null则说明队列首次使用,需要进行初始化
if (t == null) { // Must initialize
//设置头节点,如果失败则存在竞争,留至下一轮循环(for(;;)死循环)
if (compareAndSetHead(new Node()))
//用CAS的方式创建一个空的Node作为头结点,因为此时队列中只一个头结点,所以tail也指向head
tail = head;
//---->到此处:第一次循环执行结束
} else {
//进行第二次循环时,此时tail不为null,则进入else部分
//将当前线程的Node结点的prev指向tail,然后使用CAS将tail指向Node
//这部分代码和addWaiter代码一样,将当前节点添加到队列
node.prev = t;
if (compareAndSetTail(t, node)) {
//t此时指向tail,所以可以CAS成功,将tail重新指向Node。
//此时t为更新前的tail的值,即指向空的头结点,t.next=node,就将头结点的后续结点指向Node,返回头结点
//此处:将两个Node节点组成链表
t.next = node;
return t;
}
}
}
}
3.acquireQueued()方法分析
addWaiter()方法,返回了插入的节点,作为 acquireQueued() 方法的入参,这个方法主要用于争抢锁。
/**
* 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 (;;) {
//获取prev节点,若为null即刻抛出NullPointException
final Node p = node.predecessor();
//如果前驱为head才有资格进行锁的抢夺
if (p == head && tryAcquire(arg)) {
//获取锁成功后就不需要再进行同步操作了,获取锁成功的线程作为新的head节点
setHead(node);
//凡是head节点,head.thread与head.prev永远为null, 但是head.next不为null
p.next = null; // help GC
failed = false; //获取锁成功
return interrupted;
}
//如果获取锁失败,则根据节点的waitStatus决定是否需要挂起线程
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
//若前面为true,则执行挂起,待下次唤醒的时候检测中断的标志
interrupted = true;
}
} finally {
if (failed)
//如果抛出异常则取消锁的获取,进行出队(sync queue)操作
cancelAcquire(node);
}
}
//原 head 节点释放锁以后,则会从当前队列中移除,原 head 节点的 next 节点会成为新的 head节点。
调用AQS 中的 shouldParkAfterFailedAcquire() 方法,决定获取锁失败后的操作。
从上面的分析可以看出,只有队列的第二个节点可以有机会争抢锁,如果成功获取锁,则此节点晋升为头节点。对于第三个及以后的节点,if (p == head)条件不成立,首先进行shouldParkAfterFailedAcquire(p, node)操作。
shouldParkAfterFailedAcquire() 方法是判断一个争抢锁的线程是否应该被阻塞。
实现过程:主要通过Node节点的state状态,来修改链表中Node节点之间的串联性关系。它首先判断一个节点的前置节点的状态是否为Node.SIGNAL,如果是,则说明此节点已经将状态设置完成。如果锁释放,则应当通知它,它可以安全的阻塞了,返回true。
/**
* 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.
* 此方法:主要通过Node节点的state状态,来修改链表中Node节点之间的串联性关系问题
*
* 获取锁失败后,检查并修改该线程的状态。如果当前线程需要阻塞,则返回true
*
* @param pred node's predecessor holding status
* @param node the node
* @return {@code true} if thread should block
*/
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
//前继节点的状态
int ws = pred.waitStatus;
//如果是SIGNAL状态,意味着当前线程需要被unpark唤醒
if (ws == Node.SIGNAL)
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
return true;
/*
* 如果前节点的状态大于0,即为CANCELLED状态时,则会从前节点开始逐步循环找到一个没有
* 被CANCELLED节点设置为当前节点的前节点,返回false。在下次循环执行
* shouldParkAfterFailedAcquire时,返回true。
* 这个操作实际是把队列中CANCELLED节点剔除掉。
*/
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
//如果前继节点是'取消'状态,则设置'当前节点'的'当前前继节点'为'原前继节点'的前继节点。
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {//如果前继节点为“0”或者“共享锁”状态,则设置前继节点为SIGNAL状态
/*
* 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.
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
调用AQS 中的 parkAndCheckInterrupt() 方法
如果 shouldParkAfterFailedAcquire() 方法返回了 true,acquireQueued会继续执行 parkAndCheckInterrupt()方法,它是通过LockSupport.park(this)将当前线程挂起到 WATING 状态,它需要等待一个中断、unpark方法来唤醒它,通过这样一种 FIFO 的机制的等待,来实现了Lock的操作。
/**
* Convenience method to park and then check if interrupted
*
* @return {@code true} if interrupted
*/
private final boolean parkAndCheckInterrupt() {
//LockSupport提供park()和unpark()方法,实现阻塞线程和解除线程阻塞的功能
LockSupport.park(this);
return Thread.interrupted();
}
4.NonfairSync 类中 unlock() 方法释放锁
/**
* Attempts to release this lock.
* 尝试去释放锁
*
* <p>If the current thread is the holder of this lock then the hold
* count is decremented. If the hold count is now zero then the lock
* is released. If the current thread is not the holder of this
* lock then {@link IllegalMonitorStateException} is thrown.
*
* @throws IllegalMonitorStateException if the current thread does not
* hold this lock
*/
public void unlock() {
sync.release(1);
}
然后调用AQS 中的 release() 方法
/**
* 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)) {
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
最后调用 Sync 内部类中的 tryRelease() 方法
tryRelease()方法,可以认为就是一个设置锁状态的操作,而且是将状态减掉传入的参数值(参数是1),如果结果状态为0,就将独占锁的 Owner 设置为 null,以使其它的线程有机会进行执行。
在独占锁中,加锁的时候状态会增加1(当然可以自己修改这个值),在解锁的时候减掉1,同一个锁,在可以重入后,可能会被叠加为2、3、4这些值,只有 unlock() 的次数与 lock() 的次数对应才会将 Owner 线程设置为空,而且也只有这种情况下才会返回true。(即:某个锁重入了5次,必须释放5次,释放锁才算完成)
protected final boolean tryRelease(int releases) {
//这里是将锁的数量减1
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread())
//如果释放的线程和获取锁的线程不是同一个,抛出非法监视器状态异常
throw new IllegalMonitorStateException();
boolean free = false;
//由于重入的关系,不是每次释放锁c都等于0,
//直到最后一次释放锁时,才会把当前线程释放
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
5.lock与unlock过程总结
在获取同步状态时,AQS同步器维护了一个同步队列,获取状态失败的线程都会被加入到队列中并在队列中进行自旋;移出队列(或停止自旋)的条件是前驱节点为头节点且成功获取了同步状态。在释放同步状态时,同步器调用tryRelease(int arg)方法释放同步状态,然后唤醒头节点的后继节点。
附:FairSync 中 lock()方法
公平锁,则省去了修改state状态等判断锁、阻塞等方法,直接使用 acquire() 方法获得锁即可。
调用 acquire() 方法,同非公平锁一模一样。
/**
* 公平锁
*/
final void lock() {
acquire(1);
}
对比非公平锁
/**
* 非公平锁
*/
final void lock() {
/*
通过CAS算法去改变state的值
state=0表示无锁状态,state>0表示有锁状态
*/
if (compareAndSetState(0, 1))
//exclusiveOwnerThread属性是AQS从父类AbstractOwnableSynchronizer中继承的属性,改属性用来保存当前占用锁的线程,设置成一个独占锁
setExclusiveOwnerThread(Thread.currentThread());
else
//调用AQS中的acquire()方法,尝试去获得锁
acquire(1);
}
可重入锁 ReentrantLock 原理分析,介绍到此为止
如果本文对你有所帮助,那就给我点个赞呗 ^_^
End