ReentrantLock浅析

最近学习一些关于J.U.C的Lock锁的知识,记录一下以免忘记

J.U.C简介

Java.util.concurrent是并发编程中常用的工具类。包含了线程池,阻塞队列,计时器,同步器,并发集合等等。

Lock

Lock是J.U.C的核心组件,J.U.C有很多组件都有使用到Lock,锁最重要的特征就是解决并发时多线程共享数据的安全问题。

ReenTrantLock重入锁使用

ReentrantLock是Lock接口使用最多的实现,ReentrantLock和synchronized一样,支持锁的重入,当前线程t1持有锁,在线程t1内部调用使用同一把锁的另一个方法时,不需要重新获取锁,只需要把锁的state值+1.

public class ReentrantDemo{
	 public synchronized void demo(){
		 System.out.println("begin:demo");
		 demo2();
	 }
	 public void demo2(){
		 System.out.println("begin:demo1");
		 synchronized (this){
		 }
		 }
		 public static void main(String[] args) {
		 ReentrantDemo rd=new ReentrantDemo();
		 new Thread(rd::demo).start();
	 } 
}

ReentranLock同样可以保证多线程共享数据的可见,有序,安全,用法上比起sychronized更灵活

public class LockDemo{
    static  ReentrantLock lock = new ReentrantLock();
    static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        for(int i=0; i<1000; i++){
            new Thread(() -> LockDemo.calc()).start();
        }
        Thread.sleep(3000);
        System.out.println("count : " + count);
    }

    public static void calc() {
        lock.lock();
        try{
            Thread.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        count++;
        lock.unlock();
    }
}

ReentrantLock分析

ReentrantLock的类关系图

ReentrantLock的引用关系

AbstractQueuedSynchronizer

在分析ReentrantLock的锁原理之前,我们需要了解实现锁机制锁依赖的数据结构AbstractQueuedSynchronizer,简称AQS双向链表
###AQS的内部实现
AQS队列是一个FIFIO的双向链表,它的每一个节点是一个Node对象,这种链表的每个Node结构都有两个指针,分别指向后继节点和前驱节点。每个Node中都由线程分装,当线程抢占锁失败后,线程会封装成Node加入到AQS队列中,当持有锁的线程释放锁,会唤醒AQS队列中离头节点最近的下一个Node,当唤醒线程获取锁成功,会将此Node的Thread置为null,并替换原有的头节点,原头结点从AQS队列移除。这些在后续代码分析中再体现。

    static final class Node {
        static final Node SHARED = new Node();
        static final Node EXCLUSIVE = null;

        static final int CANCELLED =  1;  //竞争锁异常设置为CNANCELLED
        
        static final int SIGNAL    = -1; //前驱节点唤醒后继节点waisatus为-1的Node

        static final int CONDITION = -2; //condition相关的线程调用condition.await()在condition队列中的waitstatus

        static final int PROPAGATE = -3;

        volatile int waitStatus; //线程状态标识

        volatile Node prev; //前驱节点

        volatile Node next; //后继节点
        
        volatile Thread thread;

ReentrantLock源码分析

ReentrantLock.lock()

ReentrantLock.lock()非公平锁时序图
在这里插入图片描述
ReentrantLock.lock()获取锁入口

public void lock() {
	sync.lock();
}

Sync实际上是ReentrantLock的抽象的静态内部类,它继承了AQS来实现重入锁逻辑,AQS是一个同步队列,它能够实现线程的阻塞和唤醒,但是不具备业务能力,所以在不同的业务场景会继承AQS来实现对应场景的功能。
在这里插入图片描述
Sync有两个具体实现类也是ReentrantLock的内部类,分别是
-NofairSync:表示可以存在抢占锁的功能,也就是说不管当前队列上是否存在其他
线程等待,新线程都有机会抢占锁
-FairSync:表示所有线程严格按照FIFO来获取锁

NonfairSync.lock
由于非公平锁和公平锁的逻辑基本一致,这里选用非公平锁的lock来分析

static final class NonfairSync extends Sync {
        private static final long serialVersionUID = 7316153563782823691L;

        /**
         * Performs lock.  Try immediate barge, backing up to normal
         * acquire on failure.
         */
        final void lock() {
            if (compareAndSetState(0, 1))
                setExclusiveOwnerThread(Thread.currentThread());
            else
                acquire(1);
        }

        protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
        }
    }
  • 非公平锁执行lock时候,不管AQS队列是否存在其他阻塞线程,首先CAS竞争锁 compareAndSetState(0, 1)
  • CAS成功,表示获取锁成功
  • CAS失败,调用acquire(1) 尝试进入AQS队列
//CAS设置锁代码  java.util.concurrent.locks.AbstractQueuedSynchronizer

protected final boolean compareAndSetState(int expect, int update) {
       // See below for intrinsics setup to support this
       return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
   }

通过CAS乐果锁方式比较替换,如果内存的中state与传入的expect相等,则将state更新成expect,返回true,否则返回false
state是AQS类的一个属性,对于重入锁来说,它代表着重入的次数,

  • state=0时,表示当前锁未被任何线程持有
  • 当state>0,表示已有线程持有当前锁,state代表重入次数

设置state成功后,会设置exclusiveOwnerThread为获取锁的thread

public abstract class AbstractOwnableSynchronizer
    implements java.io.Serializable {
    private transient Thread exclusiveOwnerThread;
    protected final void setExclusiveOwnerThread(Thread thread) {
        exclusiveOwnerThread = thread;
    }
}

加入第一个线程ThreadA进来,此时state=0 exclusiveOwnerThread = null,此时还没有进入AQS,线程设置在AQS的父类AbstractOwnableSynchronizer中,
在这里插入图片描述
第二个线程ThreadB由于线程A将state赋值1,所以B进入acquire(args)

	public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
    
 //在进入AQS队列之前会再次尝试获取锁,因为可能到这一步刚好锁被释放
	 protected final boolean tryAcquire(int acquires) {
          return nonfairTryAcquire(acquires);
       }
       
	final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {
               /如果state=0,没有线程持有锁,直接CAS尝试获取锁
                if (compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current);
                    return true;
                }
            }
            //如果state>0,并且持有锁的线程是当前线程,则重入次数+1
            else if (current == getExclusiveOwnerThread()) {
                int nextc = c + acquires;
                if (nextc < 0) // overflow
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }

tryAcquired做了三件事

  • 如果state=0 会再次尝试CAS获取锁
  • 如果ThreadA调用同一个Lock锁住的另一个方法,触发重入将state+1
  • 如果都不满则走addWaiter将ThreadB加入到AQS链表
private Node addWaiter(Node mode) {
        Node node = new Node(Thread.currentThread(), mode);  //将当前竞锁失败的thread封装成node
        // Try the fast path of enq; backup to full enq on failure
        Node pred = tail;
        //如果AQS队列有存在Node,直接通过CAS把当前Node设置为Tail
        if (pred != null) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        //如果AQS为空,或者设置Tail失败,进入自旋将Node加入到AQS中
        enq(node);
        //将node返回
        return node;
    }

private Node enq(final Node node) {
        for (;;) {
            Node t = tail;
            //如果AQS队列为空,由第一个尝试加入AQS队列的Thread,初始化AQS为一个空的Node(waitStatus=0,thread=null)
            if (t == null) { // Must initialize
                if (compareAndSetHead(new Node()))
                    tail = head;
            } else {
                node.prev = t;
                if (compareAndSetTail(t, node)) {
                    t.next = node;
                    //返回当前线程的上一个Node
                    return t;
                }
            }
        }
    }

在这里插入图片描述

    public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
    //addWaiter加入AQS之后执行acquireQueued
    final boolean acquireQueued(final Node node, int arg) {
        boolean failed = true;
        try {
            //初始化线程中断状态 false,表示未被中断
            boolean interrupted = false;
            for (;;) {
                //加入AQS并不会马上阻塞,此时可能ThreadA释放了,如果node.prev是head,会再次尝试竞争锁
                final Node p = node.predecessor();
                if (p == head && tryAcquire(arg)) {
                    //如果ThreadB执行tryAcquire成功,将原来AQS对应node设置为head并将thread设置为空
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return interrupted;
                }
                //tryAcquire失败,进入挂起线程逻辑
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

   private void setHead(Node node) {
        head = node;
        node.thread = null;
        node.prev = null;
   }
   
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
		//前一个节点的waitStatus
        int ws = pred.waitStatus;
        if (ws == Node.SIGNAL)
            /*
             * This node has already set status asking a release
             * to signal it, so it can safely park.
             */
            //如果上一个节点是SIGNAL(-1)返回true,parkAndCheckInterrupt()挂起线程
            return true;
        if (ws > 0) {
            /*
             * Predecessor was cancelled. Skip over predecessors and
             * indicate retry.
             */
             //waitStatus>0只有CANCALLED(1)在竞争锁发生异常是设置为CANCELLED; 删除前驱节点中的CANCELLED,直到往前找到最近不为CANCELLED的一个节点,设置为node的前驱节点
            do {
                node.prev = pred = pred.prev;
            } while (pred.waitStatus > 0);
            pred.next = node;
        } else {
            /*
             * 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.
             */
             //如果ws是CONDITION(ws=-2)或者初始的ws=0设置pred前驱节点状态为SIGNAL(-1)
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;

//如果shouldParkAfterFailedAcquire()方法返回true执行parkAndCheckInterrupt
private final boolean parkAndCheckInterrupt() {
		//调用park方法挂起当前线程
        LockSupport.park(this);
        return Thread.interrupted();
    }

如果ThreadA在加入AQS之后,调用acquireQueued释放了锁
在这里插入图片描述
如果ThreadA没有释放锁,
在这里插入图片描述

ReentrantLock.unlock()

 public void unlock() {
 		//每次调用unlock只能释放一次重入次数
        sync.release(1);
 }
 
public final boolean release(int arg) {
    if (tryRelease(arg)) {
    	//ThreadA释放锁成功
    	//获取AQS头结点
        Node h = head;
        if (h != null && h.waitStatus != 0)
            unparkSuccessor(h);
        return true;
    }
    return false;
}

protected final boolean tryRelease(int releases) {
    int c = getState() - releases;
    if (Thread.currentThread() != getExclusiveOwnerThread())
    	//调用release的不是当前线程,抛出IllegalMonitorStateException
        throw new IllegalMonitorStateException();
    boolean free = false;
    //由于只有当前线程才能调用tryRelease,所以不需要CAS操作
    //如果state在变为0时, setExclusiveOwnerThread(null);返回true
    if (c == 0) {
        free = true;
        setExclusiveOwnerThread(null);
    }
    //更新重入状态值 返回false
    setState(c);
    return free;
}

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.
     */
     //获取head头结点的waitStatus状态
    int ws = node.waitStatus;
    if (ws < 0)
    	//如果是SIGNAL(-1)或者CONDITION(-2)设置为初始状态0
        compareAndSetWaitStatus(node, ws, 0);

    /*
     * Thread to unpark is held in successor, which is normally
     * just the next node.  But if cancelled or apparently null,
     * traverse backwards from tail to find the actual
     * non-cancelled successor.
     */
     //尝试唤醒head节点的下一个节点 ThreadB
    Node s = node.next;
    //如果下一个节点为null或者waitStatus是CANCELLED(-1),从tail开始向前遍历,查询最近的waitStatus<0的节点
    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;
    }
    if (s != null)
        LockSupport.unpark(s.thread);
}

//为什么从tail开始遍历回顾enq方法
private Node enq(final Node node) {
    for (;;) {
        Node t = tail;
        if (t == null) { // Must initialize
            if (compareAndSetHead(new Node()))
                tail = head;
        } else {
            node.prev = t;
            if (compareAndSetTail(t, node)) {
                t.next = node;
                return t;
            }
        }
    }
}
//node.prev关系在CAS操作之前建立,如果compareAndSetTail(t, node)成功
//但是t.next()还没有执行,导致node为tail的节点被忽略

unparkSuccessor(Node node)最终会执行LockSupport.unpark(s.thread);唤醒离tail节点的最近一个waitStatus<=0节点

ThreadB从被挂起的位置继续执行

final boolean acquireQueued(final Node node, int arg) {
   boolean failed = true;
    try {
        boolean interrupted = false;
        for (;;) {
        	//ThreadB被ThreadA唤醒后再次自旋
            final Node p = node.predecessor();
            //此时ThreadB满足条件并且tryAcquire(arg)成功
            if (p == head && tryAcquire(arg)) {
                setHead(node);
                p.next = null; // help GC
                failed = false;
                //返回interrupted状态
                return interrupted;
            }
            if (shouldParkAfterFailedAcquire(p, node) &&
                parkAndCheckInterrupt())
                //parkAndCheckInterrupt返回true说明被中断过设置interrupted为true交给外面一层响应中断
                interrupted = true;
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}

private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
	//被唤醒后首先获取中断标识,是否在被阻塞过程中其他线程调用threadb.interrupted()方法
    return Thread.interrupted();
}

//acquire响应acquireQueued的结果 执行selfInterrupt方法
public final void acquire(int arg) {
   if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
        selfInterrupt();
}

static void selfInterrupt() {
  //将当前线程的状态设置为中断
 Thread.currentThread().interrupt();
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值