AQS——AbstractQueuedSynchronizer 速查 (jdk 11)

99 阅读11分钟

AbstractQueuedSynchronizer是一个多线程资源管理的框架,ReentrantLock的内部,也是基于AQS进行的实现。

Node

又看到了喜闻乐见的Node了。AQS里面也维护了一个Node用于抽象资源,下面看一下Node里面的属性以及方法

static final class Node {
        /** Marker to indicate a node is waiting in shared mode */
        // 节点正在共享模式下等待标记
        // 这里可以看出,Node默认是共享模式
        static final Node SHARED = new Node();
        /** Marker to indicate a node is waiting in exclusive mode */
        // 节点正在独占模式下等待标记
        static final Node EXCLUSIVE = null;
        /** waitStatus value to indicate thread has cancelled. */
        // waitStatus的一个值,指示线程已取消
        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;

        /**
         * Status field, taking on only the values:
         *   SIGNAL:     The successor of this node is (or will soon be)
         *               blocked (via park), so the current node must
         *               unpark its successor when it releases or
         *               cancels. To avoid races, acquire methods must
         *               first indicate they need a signal,
         *               then retry the atomic acquire, and then,
         *               on failure, block.
         *   CANCELLED:  This node is cancelled due to timeout or interrupt.
         *               Nodes never leave this state. In particular,
         *               a thread with cancelled node never again blocks.
         *   CONDITION:  This node is currently on a condition queue.
         *               It will not be used as a sync queue node
         *               until transferred, at which time the status
         *               will be set to 0. (Use of this value here has
         *               nothing to do with the other uses of the
         *               field, but simplifies mechanics.)
         *   PROPAGATE:  A releaseShared should be propagated to other
         *               nodes. This is set (for head node only) in
         *               doReleaseShared to ensure propagation
         *               continues, even if other operations have
         *               since intervened.
         *   0:          None of the above
         *
         * The values are arranged numerically to simplify use.
         * Non-negative values mean that a node doesn't need to
         * signal. So, most code doesn't need to check for particular
         * values, just for sign.
         *
         * The field is initialized to 0 for normal sync nodes, and
         * CONDITION for condition nodes.  It is modified using CAS
         * (or when possible, unconditional volatile writes).
         */
         // 这个值用户表示等待状态
        volatile int waitStatus;

        /**
         * Link to predecessor node that current node/thread relies on
         * for checking waitStatus. Assigned during enqueuing, and nulled
         * out (for sake of GC) only upon dequeuing.  Also, upon
         * cancellation of a predecessor, we short-circuit while
         * finding a non-cancelled one, which will always exist
         * because the head node is never cancelled: A node becomes
         * head only as a result of successful acquire. A
         * cancelled thread never succeeds in acquiring, and a thread only
         * cancels itself, not any other node.
         */
         // 上一个节点
        volatile Node prev;

        /**
         * Link to the successor node that the current node/thread
         * unparks upon release. Assigned during enqueuing, adjusted
         * when bypassing cancelled predecessors, and nulled out (for
         * sake of GC) when dequeued.  The enq operation does not
         * assign next field of a predecessor until after attachment,
         * so seeing a null next field does not necessarily mean that
         * node is at end of queue. However, if a next field appears
         * to be null, we can scan prev's from the tail to
         * double-check.  The next field of cancelled nodes is set to
         * point to the node itself instead of null, to make life
         * easier for isOnSyncQueue.
         */
         // 下一个节点
        volatile Node next;

        /**
         * The thread that enqueued this node.  Initialized on
         * construction and nulled out after use.
         */
        volatile Thread thread;

        /**
         * Link to next node waiting on condition, or the special
         * value SHARED.  Because condition queues are accessed only
         * when holding in exclusive mode, we just need a simple
         * linked queue to hold nodes while they are waiting on
         * conditions. They are then transferred to the queue to
         * re-acquire. And because conditions can only be exclusive,
         * we save a field by using special value to indicate shared
         * mode.
         */
         // 下一个等待者
        Node nextWaiter;

        /**
         * Returns true if node is waiting in shared mode.
         */
        final boolean isShared() {
            return nextWaiter == SHARED;
        }

        /**
         * Returns previous node, or throws NullPointerException if null.
         * Use when predecessor cannot be null.  The null check could
         * be elided, but is present to help the VM.
         *
         * @return the predecessor of this node
         */
         // 返回上一个节点
        final Node predecessor() {
            Node p = prev;
            if (p == null)
                throw new NullPointerException();
            else
                return p;
        }

        /** Establishes initial head or SHARED marker. */
        Node() {}

        /** Constructor used by addWaiter. */
        Node(Node nextWaiter) {
            this.nextWaiter = nextWaiter;
            THREAD.set(this, Thread.currentThread());
        }

        /** Constructor used by addConditionWaiter. */
        Node(int waitStatus) {
            WAITSTATUS.set(this, waitStatus);
            THREAD.set(this, Thread.currentThread());
        }

        /** CASes waitStatus field. */
        final boolean compareAndSetWaitStatus(int expect, int update) {
            return WAITSTATUS.compareAndSet(this, expect, update);
        }

        /** CASes next field. */
        final boolean compareAndSetNext(Node expect, Node update) {
            return NEXT.compareAndSet(this, expect, update);
        }

        final void setPrevRelaxed(Node p) {
            PREV.set(this, p);
        }

        // VarHandle mechanics
        private static final VarHandle NEXT;
        private static final VarHandle PREV;
        private static final VarHandle THREAD;
        private static final VarHandle WAITSTATUS;
        static {
            try {
                // MethodHandles.lookup() 提供了一种动态的方式来获取和操作方法句柄,同时确保了安全性和权限。
                MethodHandles.Lookup l = MethodHandles.lookup();
                NEXT = l.findVarHandle(Node.class, "next", Node.class);
                PREV = l.findVarHandle(Node.class, "prev", Node.class);
                THREAD = l.findVarHandle(Node.class, "thread", Thread.class);
                WAITSTATUS = l.findVarHandle(Node.class, "waitStatus", int.class);
            } catch (ReflectiveOperationException e) {
                throw new ExceptionInInitializerError(e);
            }
        }
    }

从Node的属性可以看到,node是一个存放着Thread的,且维护着节点状态的双向链表

VarHandle (jdk9 以后支持)

VarHandle 提供了一组方法,允许你以原子或非原子的方式读取、写入、更新变量的值,还支持其他操作,如比较并交换。它的目标是提供更细粒度的控制,同时确保线程安全性和可见性。

acquire() 方法

上面介绍完对应的Node属性,下面要找一个入口,看看是怎么实现加锁操作的,那么怎么找这个入口呢?

java中ReentrantLocklock()方法,调用的是内部维护的一个Sync类,这个类就是继承了AQS,那我们可以通过它,看一下lock()加锁操作是调用的哪个方法。(其实跟着注释一点一点的看也是可以的,所以接口注释还是很重要的)

abstract static class Sync extends AbstractQueuedSynchronizer
    /**
     * Acquires the lock.
     *
     * <p>Acquires the lock if it is not held by another thread and returns
     * immediately, setting the lock hold count to one.
     *
     * <p>If the current thread already holds the lock then the hold
     * count is incremented by one and the method returns immediately.
     *
     * <p>If the lock is held by another thread then the
     * current thread becomes disabled for thread scheduling
     * purposes and lies dormant until the lock has been acquired,
     * at which time the lock hold count is set to one.
     */
    public void lock() {
        sync.acquire(1);
    }

那么很明显了,入口就是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();
    }

调用了四个方法,tryAcquire(),addWaiter(),acquireQueued(),selfInterrupt() 先简单介绍一下流程:

  • tryAcquire()方法,尝试获取一下锁,获取成功了直接不执行&&后面的语句
  • tryAcquire()获取锁失败,先调用addWaiter(Node.EXCLUSIVE)方法
    • 这里的Node.EXCLUSIVE表示,当前的lock是独占模式
    • 在addWaiter()方法里,有一个自旋,CAS放入Head和Tail,因为可能会有多个线程同时进来addWaiter()方法这个时候,如果不加乐观锁, 会导致node节点丢失,比如两个线程同时进来,这个时候应该要生成两个node,如果只加了if条件,没有加乐观锁,去添加tail节点,那么tail会被后 进来的node覆盖掉
  • addWaiter() 方法执行成功以后,会返回含有当前线程的node,并且已经初始化好了Head和tail这两个头尾节点
  • 调用acquireQueued()方法,这个方法会决定当前的线程是不是需要挂起等待
    • 判断当前节点的前一个节点,是不是head,是的话尝试去获取锁,拿到了锁以后,把head设置为当前节点,旧的head释放掉,返回false,告诉上级 方法, 当前线程没有中断
    • 如果当前节点不是头结点,或者没有获取到锁,则会判断当前线程需不需要被挂起为watting状态
      • 这时候会取当前node的pred(前一个节点)的waitStatus(默认是0),
      • 如果pred的waitStatus = Node.SIGNAL(-1),直接返回true
      • 如果pred的waitStatus > 0,说明pred节点已经被打上了中断标记,这时候直接遍历前面的node,找出waitStatus>0的出队 直到waitStatus<=0为止,把它的next设置为当前node,node的prev设置为找到的节点,饭后返回false
      • 如果pred的waitStatus为其他状态,则修改为 Node.SIGNAL,然后返回false
    • 当acquireQueued返回true的时候,就会执行selfInterrupt()方法
    • selfInterrupt方法会把当前线程中断

tryAcquire() 尝试获取

protected boolean tryAcquire(int arg) {
        throw new UnsupportedOperationException();
    }

在AbstractQueuedSynchronizer中,tryAcquire()是个抽象方法,如果你不重写它,那调用它就会报错

这里可以看下ReentrantLock的实现,ReentrantLock有两个实现,一个公平锁,一个非公平锁

// 公平锁实现
protected final boolean tryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            // state表示锁被同一个线程获取的次数
            int c = getState();
            if (c == 0) {
                // 判断有无在等待的队列,有返回true,没有返回false
                if (!hasQueuedPredecessors() &&
                    // 前面没有队列,则修改内部STATE的状态值 一般是 0->1
                    // 其实这里就是通过原子的方式修改state = 1
                    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");
                // 设置state
                setState(nextc);
                return true;
            }
            return false;
        }
// 非公平锁实现
final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            // 没有加锁
            if (c == 0) {
                // 调用父类compareAndSetState方法,原子修改state状态
                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");
                // state= c + acquires (acquires一般为1)
                setState(nextc);
                return true;
            }
            return false;
        }

FairSync的hasQueuedPredecessors()方法

public final boolean hasQueuedPredecessors() {
        Node h, s;
        if ((h = head) != null) {
            if ((s = h.next) == null || s.waitStatus > 0) {
                s = null; // traverse in case of concurrent cancellation
                for (Node p = tail; p != h && p != null; p = p.prev) {
                    if (p.waitStatus <= 0)
                        s = p;
                }
            }
            if (s != null && s.thread != Thread.currentThread())
                return true;
        }
        return false;
    }

这个方法是看头部有没有在等待的Node,有的话返回true,没有的话,返回false

addWaiter()

private Node addWaiter(Node mode) {
        Node node = new Node(mode);
        // 串一下这里的操作
        // 首先取尾部的node出来
        // 判断尾部是不是空的,是空的,就调用initializeSyncQueue初始化一下队列,创建一个空的node到头尾
        // 如果尾部有值,把维护的上一个节点换成oldTail
        // 然后把尾部的值设置成当新的node
        // 把oldTail.next 设置为新的node
        // 把新的node返回出去
        for (;;) {
            Node oldTail = tail;
            if (oldTail != null) {
                node.setPrevRelaxed(oldTail);
                if (compareAndSetTail(oldTail, node)) {
                    oldTail.next = node;
                    return node;
                }
            } else {
                initializeSyncQueue();
            }
        }
    }

acquireQueued

final boolean acquireQueued(final Node node, int arg) {
        boolean interrupted = false;
        try {
            for (;;) {
                // 获取上一个节点
                final Node p = node.predecessor();
                // 如果是头节点 且能获取到同步状态
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    return interrupted;
                }
                // 获取同步状态失败后,判断当前节点需不需要挂起   
                if (shouldParkAfterFailedAcquire(p, node))
                    // 这里是一个或等操作
                    // 先进行或操作,再赋值 false | true = true
                    // booleanA | booleanB ,B为false,则A不变
                    interrupted |= parkAndCheckInterrupt();
            }
        } catch (Throwable t) {
            cancelAcquire(node);
            if (interrupted)
                selfInterrupt();
            throw t;
        }
    }  

shouldParkAfterFailedAcquire()

private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;
        // 前一个节点的状态,如果是SIGNAL(-1)说明上一个节点已经设置了释放状态了,当前节点可以安全挂起(park)
        if (ws == Node.SIGNAL)
            /*
             * This node has already set status asking a release
             * to signal it, so it can safely park.
             */
            return true;
        // wa>0 对应的就是cancel(1)状态,说明前一个节点被中断了
        // 这时需要向前遍历,把cancel状态的node出队,找到waitStatus<=0的节点为止
        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 {
            /*
             * 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的,则把preNode的waitStatus改为Node.SIGNAL()
            pred.compareAndSetWaitStatus(ws, Node.SIGNAL);
        }
        return false;
    }

shouldParkAfterFailedAcquire()方法是用于在尝试获取同步状态失败后,判断当前线程是否应该挂起(park) 其实这里对当前节点的关注不大,主要操作的是pred,前一个节点 pred节点,如果是Node.SIGNAL状态,就返回true,说明可以被安全挂起 如果前一个节点被中断了,就向前遍历,把cancel的节点全部出队,然后返回false,说明不能安全挂起 如果前一个节点是其他状态,则把前一个节点的waitStates设置为挂起,然后返回false

parkAndCheckInterrupt()

    /**
     * Convenience method to park and then check if interrupted.
     *
     * @return {@code true} if interrupted
     */
    private final boolean parkAndCheckInterrupt() {
        // 前面都是在维护node的队列queue和状态
        // 执行到这里,才是真的挂起了线程
        // LockSupport.park(this),挂起当前线程,进入waiting状态
        LockSupport.park(this);
        // Thread.interrupted():返回当前线程的中断状态,并清除中断状态。
        // 如果线程在挂起的过程中被中断过,那么 Thread.interrupted() 返回 true,否则返回 false
        return Thread.interrupted();
    }

执行到parkAndCheckInterrupt()方法的时候,才是真正的挂起了线程,把线程的状态改为waiting状态,然后返回线程是否中断。

release() 释放锁

    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() state为0的时候,才算是释放了锁

@ReservedStackAccess
        protected final boolean tryRelease(int releases) {
            // 获取state-releases 一般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);
            }
            // state设置为0
            setState(c);
            return free;
        }

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.
         */
        int ws = node.waitStatus;
        // 把小于的状态设置为0
        if (ws < 0)
            node.compareAndSetWaitStatus(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;
        // 如果下个节点为空或者 s.waitStatus > 0 (cancel)则从尾部向前遍历,获取最前的一个node<=0的node
        if (s == null || s.waitStatus > 0) {
            s = null;
            for (Node p = tail; p != node && p != null; p = p.prev)
                if (p.waitStatus <= 0)
                    s = p;
        }
        if (s != null)
            // 唤醒线程parkAndCheckInterrupt()中阻塞的线程会被唤醒
            LockSupport.unpark(s.thread);
    }

tips

& 和 && 非短路与和短路与操作,短路与是,第一个值为false时,不会执行第二个值的操作,非短路则会执行 所以 String a = null时, a != null && !a.equals(""),这个操作不会空指针异常,a != null & !a.equals("") 则会

ReentrantLock是独占锁,使用了AQS中的独占锁部分,如果是共享锁,则有ReentrantReadWriteLock,读写锁