java并发之AbstractQueuedSynchronizer同步器

43 篇文章 0 订阅

             java并发之AbstractQueuedSynchronizer

一、概述

谈到java的并发编程,就离不开JUC,JUC指的是java.util.concurrent包,里面包含了需要java并发的工具类,而谈到JUC,就不得不谈AbstractQueuedSynchronizer(AQS)!

AQS:抽象的队列式的同步器,AQS定义了一套多线程访问共享资源的同步器框架,许多同步类实现都依赖于它,如常用的ReentrantLock/Semaphore/CountDownLatch...,

二、框架解读

AQS内部维护了一个volatile int state(代表资源)变量和一个变种的CLH(FIFO)队列(多线程竞争阻塞时进入队列),CLH队列一般用于自旋锁。

state代表一种资源,AQS提供了3个操作state的方法

  • getState
  • setState
  • compareAndSetState

AQS定义了两种获取资源的方式:exclusive(独占)与SHARED(共享),独占模式获取资源时(如ReentrantLock),其他threads无法获取成功,共享模式获取资源时(如CountDownLatch),其他threads可能获取成功。

 不同的自定义同步器争用共享资源的方式也不同。自定义同步器在实现时只需要实现共享资源state的获取与释放方式即可,至于具体线程等待队列的维护(如获取资源失败入队/唤醒出队等),AQS已经在顶层实现好了。自定义同步器实现时主要实现以下几种方法:

  • isHeldExclusively():该线程是否正在独占资源。只有用到condition才需要去实现它。
  • tryAcquire(int):独占方式。尝试获取资源,成功则返回true,失败则返回false。
  • tryRelease(int):独占方式。尝试释放资源,成功则返回true,失败则返回false。
  • tryAcquireShared(int):共享方式。尝试获取资源。负数表示失败;0表示成功,但没有剩余可用资源;正数表示成功,且有剩余资源。
  • tryReleaseShared(int):共享方式。尝试释放资源,如果释放后允许唤醒后续等待结点返回true,否则返回false。

  一般来说,自定义同步器要么是独占方法,要么是共享方式,他们也只需实现tryAcquire-tryRelease、tryAcquireShared-tryReleaseShared中的一种即可。但AQS也支持自定义同步器同时实现独占和共享两种方式,如ReentrantReadWriteLock。

下面我们就以ReentrantLock类来解读下AQS的内部逻辑

三、源码理解

先看下内部类Node,线程获取资源阻塞后会被封装成Node类,CLH队列中的node关系如下

           +------+  prev +-----+       +-----+
      head |      | <---- |     | <---- |     |  tail
           +------+       +-----+       +-----+

入队只需要拼接tail,出队需要设置head,prev代表前驱结点,next代表后继节点,初始化队列时,head=tail。当前节点所包含的线程依赖于前驱结点的唤醒。CLH队列需要一个虚拟的head,AQS并没有在构造CLH队列时就初始化head,而是在竞争出现时构造一个node,然后让head、tail指向这个node

Node类内部有个waitStatus变量代表结点的状态,有如下几种取值。

        /** waitStatus value to indicate thread has cancelled */ 线程被取消
        static final int CANCELLED =  1;
        /** waitStatus value to indicate successor's thread needs unparking */ 后继节点需要唤醒
        static final int SIGNAL    = -1;
        /** waitStatus value to indicate thread is waiting on condition */ 后继节点在等待条件
        static final int CONDITION = -2;
        /**
         * waitStatus value to indicate the next acquireShared should
         * unconditionally propagate 
         */
        static final int PROPAGATE = -3;

acquire()方法解读,首先调用tryAcquire方法尝试获取资源,获取资源失败节点入队,在队列中继续尝试获取资源

    /**
        独占模式下获取资源,方法实现中的tryAcquire由子类实现
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
     * by invoking at least once {@link #tryAcquire},
     * 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();
    }
    /**
     * Creates and enqueues node for current thread and given mode.
     *
     * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
     * @return the new node
     * 将当前线程包装成node,并且入队,如果队列为空,初始化一个head,并且head=tail,队列不为空则直接入队
     */
    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) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        enq(node);
        return node;
    }
  /**
     * Inserts node into queue, initializing if necessary. See picture above.
     * @param node the node to insert
     * @return node's predecessor
     * node入队的具体实现
     */
    private Node enq(final Node node) {
        for (;;) {
            Node t = tail; //t指向tail
            if (t == null) { // Must initialize//如果tail为空,则初始化一个node,并且tail=head
                if (compareAndSetHead(new Node()))
                    tail = head;
            } else {
                node.prev = t;//当前节点前驱指向之前的tail,并且设置自己为tail
                if (compareAndSetTail(t, node)) {
                    t.next = node;
                    return t;
                }
            }
        }
    }

前面说过尝试获取资源失败会入队等待重新尝试获取资源


    /**
     * 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();//获取前驱结点
                if (p == head && tryAcquire(arg)) {//如果前驱结点是头部结点,就尝试获取资源,因为头部节点可能会释放资源,然后唤醒后继节点
                    setHead(node);//如果获取资源成功,则将自己置为head
                    p.next = null; // help GC 表示当前线程获取资源成功,则将前驱结点出队
                    failed = false;
                    return interrupted;//返回获取资源过程中是否被中断
                }
                //获取资源失败后是否应该挂起当前线程
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())//当前线程找到安全点后,就可以暂时挂起,等等前驱结点的唤醒
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }
 /**
     * 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
     */
    private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;//前驱结点的waitStatus
        if (ws == Node.SIGNAL)//如果前驱结点waitStatus是SIGNAL,表示当前节点还需要等待唤醒,需要挂起
            /*
             * This node has already set status asking a release
             * to signal it, so it can safely park.
             */
            return true;
        if (ws > 0) {//前驱结点waitStatus为非负数表示前驱节点无需唤醒(可能被取消、等等条件),就继续寻找正常前驱结点挂靠在后边,无需唤醒的结点就断了链接,成为独立的node,将会被GC回收
            /*
             * Predecessor was cancelled. Skip over predecessors and
             * indicate retry.
             */
            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.
             */
            如果前驱结点是正常的,就设置前驱结点waitStatus为signal,表示当前结点需要收到通知
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;
    }
    /**挂起当前线程,唤醒被挂起的线程需要unPark()或者中断
     * Convenience method to park and then check if interrupted
     *
     * @return {@code true} if interrupted
     */
    private final boolean parkAndCheckInterrupt() {
        LockSupport.park(this);
        return Thread.interrupted();
    }

现在让我们再回到acquireQueued(),总结下该函数的具体流程:

  1. 结点进入队尾后,检查状态,找到安全休息点;
  2. 调用park()进入waiting状态,等待unpark()或interrupt()唤醒自己;
  3. 被唤醒后,看自己是不是有资格能拿到号。如果拿到,head指向当前结点,并返回从入队到拿到号的整个过程中是否被中断过;如果没拿到,继续流程1。

acquireQueued()分析完之后,我们接下来再回到acquire()!再贴上它的源码吧:

   /**
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
     * by invoking at least once {@link #tryAcquire},
     * 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();
    }

再来总结下它的流程吧:

  1. 调用自定义同步器的tryAcquire()尝试直接去获取资源,如果成功则直接返回;
  2. 没成功,则addWaiter()将该线程加入等待队列的尾部,并标记为独占模式;
  3. acquireQueued()使线程在等待队列中休息,有机会时(轮到自己,会被unpark())会去尝试获取资源。获取到资源后才返回。如果在整个等待过程中被中断过,则返回true,否则返回false。
  4. 如果线程在等待过程中被中断过,它是不响应的。只是获取资源后才再进行自我中断selfInterrupt(),将中断补上。

整个流程如图所示,

说完了acquire()方法,下面来说说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)//如果头节点不为空,并且waitStatus不为空则唤醒后继节点
                unparkSuccessor(h);
            return true;
        }
        return false;
    }
    /**
     * Wakes up node's successor, if one exists.
     *如果后继节点存在,则唤醒
     * @param node the node
     */
    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.
         */
        int ws = node.waitStatus;
        if (ws < 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.
         */
        Node s = node.next;
        if (s == null || s.waitStatus > 0) {//waitStatus大于0,说明节点被cancelled,从tail开始寻找需要唤醒的后继节点
            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);
    }

逻辑并不复杂。它调用tryRelease()来释放资源。有一点需要注意的是,它是根据tryRelease()的返回值来判断该线程是否已经完成释放掉资源了!所以自定义同步器在设计tryRelease()的时候要明确这一点!!

这里的唤醒线程需要结合acquireQueued()方法一起来理解,可以看到这里线程被unParked后就可以继续自旋尝试获取资源。

release()是独占模式下线程释放共享资源的顶层入口。它会释放指定量的资源,如果彻底释放了(即state=0),它会唤醒等待队列里的其他线程来获取资源。

既然源码看完了,我们来看下怎么使用AQS,以ReentrantLock为例子。ReentrantLock是一个可重入锁,ReentrantLock的内部类Sync实现了AQS接口,这是可重入锁的同步控制器,使用AQS的state字段代表锁的持有数。可重入锁有两种模式,公平和非公平模式,默认为非公平模式,先看下fair模式。

 

    /**
     * Creates an instance of {@code ReentrantLock} with the
     * given fairness policy.
     *
     * @param fair {@code true} if this lock should use a fair ordering policy
     */
    构造方法
    public ReentrantLock(boolean fair) {
        sync = fair ? new FairSync() : new NonfairSync();
    }
    /**
     * Sync object for fair locks
     */
    static final class FairSync extends Sync {
        private static final long serialVersionUID = -3000897897090466540L;

        final void lock() {
            acquire(1);
        }

        /**
         * Fair version of tryAcquire.  Don't grant access unless
         * recursive call or no waiters or is first.
         */
        protected final boolean tryAcquire(int acquires) {
            final Thread current = Thread.currentThread();//获取当前线程
            int c = getState();
            if (c == 0) {//state==0表示资源空闲
                if (!hasQueuedPredecessors() &&//是否有前驱结点
                    compareAndSetState(0, acquires)) {//设置资源
                    setExclusiveOwnerThread(current);//设置锁的拥有者为当前线程,重入标志
                    return true;
                }
            }
            else if (current == getExclusiveOwnerThread()) {//如果资源不是空闲的,并且锁的拥有者为当前线程,则表示可重入
                int nextc = c + acquires;
                if (nextc < 0)
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }
    }

tryRelease()

    ReentrantLock的tryRelease方法,重入多少次就释放多少次
  protected final boolean tryRelease(int releases) {
            int c = getState() - releases;
            if (Thread.currentThread() != getExclusiveOwnerThread())
                throw new IllegalMonitorStateException();
            boolean free = false;
            if (c == 0) {//state表示释放完毕,否则锁还是未释放
                free = true;
                setExclusiveOwnerThread(null);
            }
            setState(c);
            return free;
        }

再来看下nonFair模式

    /**
     * Sync object for non-fair locks
    非公平模式下,首先直接参与获取资源而不用排队,这就是非公平的含义
     */
    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);
        }
    }

看懂了AQS的逻辑,再去理解JUC下面的一些包就容易多了

上面的acquire(),release()方法都是独占模式下的资源操作

下面说说共享模式的acquireShared(),releaseShared()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值