【Java併發】AQS

AQS依賴於提供一個原子變量(state)用來表示當前鎖對象的同步狀態,並且提供了三個對state變量原子操作的方法

  • getState()
  • setState()
  • compareAndSetState()
/**
 * Returns the current value of synchronization state.
 * This operation has memory semantics of a {@code volatile} read.
 * @return current state value
 */
protected final int getState() {
    return state;
}

/**
 * Sets the value of synchronization state.
 * This operation has memory semantics of a {@code volatile} write.
 * @param newState the new state value
 */
protected final void setState(int newState) {
    state = newState;
}

/**
 * Atomically sets synchronization state to the given updated
 * value if the current state value equals the expected value.
 * This operation has memory semantics of a {@code volatile} read
 * and write.
 *
 * @param expect the expected value
 * @param update the new value
 * @return {@code true} if successful. False return indicates that the actual
 *         value was not equal to the expected value.
 */
protected final boolean compareAndSetState(int expect, int update) {
    // See below for intrinsics setup to support this
    return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}

AQS包括同步隊列(CLH)、獨佔模式、共享模式。AQS提供了獲取鎖、釋放鎖的頂層方法,其中有些具體的方法需要實現者自己去覆寫鎖的獲取、釋放邏輯。

同步隊列

一個內部Node組成的FIFO雙向隊列。當前線程獲取同步狀態失敗時,同步器會將當前線程以及等待狀態等信息構造爲一個Node並將其加入到同步隊列中,同時阻塞當前線程,當同步狀態釋放時,會把首節點中的線程喚醒,使其再次嘗試獲取同步狀態。

static final class Node {
    /** Marker to indicate a node is waiting in shared mode */
    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 */
    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() throws NullPointerException {
        Node p = prev;
        if (p == null)
            throw new NullPointerException();
        else
            return p;
    }

    Node() {    // Used to establish initial head or SHARED marker
    }

    Node(Thread thread, Node mode) {     // Used by addWaiter
        this.nextWaiter = mode;
        this.thread = thread;
    }

    Node(Thread thread, int waitStatus) { // Used by Condition
        this.waitStatus = waitStatus;
        this.thread = thread;
    }
}

前面還有很大段註釋解釋了Node的用處。

Node其中的屬性值:

1、SHARED和EXCLUSIVE表明了是共享還是獨佔模式

2、waitStatus的幾個狀態值:指示是否被取消、指示繼任節點需要被喚醒、指示是否在等待條件、指示下個共享獲取節點應該無條件傳播

3、pre和next分別連接了前後的同步節點,組成了FIFO雙向隊列

4、存儲對應的線程,一個線程對應一個節點

5、nextWaiter用於等待隊列,只會出現在獨佔模式中

獨佔獲取

獨佔,顧名思義,鎖在同一個時刻只能由一個線程佔有。獨佔鎖的某些具體實現方法以ReentrantLock舉例。

public final void acquire(int arg) {
    if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
        selfInterrupt();//如果線程被打斷喚醒過,在上面的方法中沒有響應,只是做了中斷標記,所以這裏需要補一箇中斷
}

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

獨佔獲取鎖的頂層方法,先嚐試獲取,tryAcquire由各個實現類去覆寫,看下ReentrantLock中的實現。

//ReentrantLock中NonfairSync覆寫的tryAcquire方法

protected final boolean tryAcquire(int acquires) {
    return nonfairTryAcquire(acquires);
}

final boolean nonfairTryAcquire(int acquires) {
    final Thread current = Thread.currentThread();
    int c = getState();//1、獲取當前state
    if (c == 0) {//2、如果爲0,說明當前鎖沒被佔用
        if (compareAndSetState(0, acquires)) {//3、嘗試CAS設置state
            setExclusiveOwnerThread(current);//4、如果成功,說明成功獲取到鎖,將獨佔線程設置爲當前線程
            return true;
        }
    }
    else if (current == getExclusiveOwnerThread()) {//5、如果佔有鎖的是線程自己
        int nextc = c + acquires;//6、增加佔有次數,釋放時需要依次釋放
        if (nextc < 0) // overflow
            throw new Error("Maximum lock count exceeded");//7、重入次數溢出
        setState(nextc);//8、設置state爲新值,因爲此時已佔有鎖,不需要CAS設置
        return true;
    }
    return false;
}

成功則結束。如果獲取失敗,將線程封裝爲一個Node節點加入到等待隊列中。

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) {//1、如果tail不爲空,嘗試快速插入隊尾
        node.prev = pred;
        if (compareAndSetTail(pred, node)) {//2、CAS插入隊尾成功,則退出
            pred.next = node;
            return node;
        }
    }
    enq(node);//3、等待隊列爲空,或快速入隊失敗,則進行入隊操作,直至成功
    return node;
}

private Node enq(final Node node) {
    for (;;) {
        Node t = tail;
        if (t == null) { //1、如果隊列爲空,初始化隊列
            if (compareAndSetHead(new Node()))//2、CAS設置頭指向一個Node節點
                tail = head;
        } else {
            node.prev = t;
            if (compareAndSetTail(t, node)) {//3、CAS設置node到隊尾,直至成功
                t.next = node;
                return t;
            }
        }
    }
}

將失敗線程的Node節點加入到等待隊列後,開始不斷嘗試獲取鎖

final boolean acquireQueued(final Node node, int arg) {
    boolean failed = true;
    try {
        boolean interrupted = false;
        for (;;) {
            final Node p = node.predecessor();//1、獲得node的前驅節點
            if (p == head && tryAcquire(arg)) {//2、如果前驅節點已經是頭節點,並且本線程獲取鎖成功
                setHead(node);//3、設置自己爲頭節點
                p.next = null; // help GC
                failed = false;
                return interrupted;
            }
            if (shouldParkAfterFailedAcquire(p, node) &&//4、獲取鎖失敗,線程進入等待狀態,設置waitStatus爲-1
                parkAndCheckInterrupt())
                interrupted = true;//5、一旦線程被打斷喚醒過,設置打斷喚醒標記爲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) {
    int ws = pred.waitStatus;
    if (ws == Node.SIGNAL)//1、如果waitStatus已經是-1了,即告訴前驅節點我需要你釋放鎖後通知我,則返回
        /*
         * This node has already set status asking a release
         * to signal it, so it can safely park.
         */
        return true;
    if (ws > 0) {
        /*
         * Predecessor was cancelled. Skip over predecessors and
         * indicate retry.
         */
         
         //2、如果前驅節點waitStatus爲1,即取消獲取鎖,則一直向前找,直到找到非取消狀態的節點,將該節點和當前節點串聯起來,
         //中間的那些取消節點後續會被回收掉
        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.
         */
        compareAndSetWaitStatus(pred, ws, Node.SIGNAL);//3、CAS設置waitStatus爲-1
    }
    return false;
}

private final boolean parkAndCheckInterrupt() {
    LockSupport.park(this);//1、在該鎖上阻塞
    return Thread.interrupted();//2、返回線程是否是被打斷喚醒的,注意此方法會清除中斷標記位
}

//LockSupport.java
public static void park(Object blocker) {
    Thread t = Thread.currentThread();
    setBlocker(t, blocker);//1、設置該線程被鎖阻塞
    UNSAFE.park(false, 0L);//2、線程阻塞,等待被喚醒
    setBlocker(t, null);
}

至此,獨佔鎖的獲取過程已經結束。

獨佔釋放

獨佔釋放的頂層入口爲release方法

public final boolean release(int arg) {
    if (tryRelease(arg)) {//1、嘗試釋放鎖
        Node h = head;
        if (h != null && h.waitStatus != 0)//2、等待隊列不爲空,且後續節點在等待頭節點釋放鎖
            unparkSuccessor(h);
        return true;
    }
    return false;
}

//ReentrantLock覆寫tryRelease
protected final boolean tryRelease(int releases) {
    int c = getState() - releases;//1、獲取當前還需退出鎖的次數(因爲重入鎖可能進入不止一次)
    if (Thread.currentThread() != getExclusiveOwnerThread())//2、當前線程才能釋放自己持有的鎖
        throw new IllegalMonitorStateException();
    boolean free = false;
    if (c == 0) {//3、如果當前需要退出鎖的次數爲0,說明線程已經不再持有鎖,需釋放鎖
        free = true;
        setExclusiveOwnerThread(null);
    }
    setState(c);//4、設置state
    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.
     */
    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) {//1、找到後續節點waitStatus爲-1的節點
        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);//2、喚醒等待鎖釋放的線程
}

在釋放的最後,釋放鎖的線程喚醒在自己後面的下一個等待獲取鎖的線程。等待線程從park中醒來後,開始嘗試獲取鎖。釋放的過程到此結束。

共享獲取

與獨佔模式不同,共享獲取可以允許多個線程持有鎖,所以共享狀態state是可以大於1的。

public final void acquireShared(int arg) {
    if (tryAcquireShared(arg) < 0)
        doAcquireShared(arg);
}

protected int tryAcquireShared(int arg) {
    throw new UnsupportedOperationException();
}

同樣,tryAcquireShared沒有具體實現,留給子類去實現自己的邏輯。共享模式以CountDownLatch舉例。

protected int tryAcquireShared(int acquires) {
    return (getState() == 0) ? 1 : -1;
}

//CountDownLatch用法
CountDownLatch countDownLatch = new CountDownLatch(3);

查看當前的state是否等於0。CountDownLatch在初始化時會將state設置爲傳進來的數值,上例中即state爲3.

private void doAcquireShared(int arg) {
    final Node node = addWaiter(Node.SHARED);//1、封裝線程對象爲Node,不過此時Node的模式爲共享模式
    boolean failed = true;
    try {
        boolean interrupted = false;
        for (;;) {
            final Node p = node.predecessor();//2、獲得前驅節點
            if (p == head) {//3、如果前驅節點已經是頭節點
                int r = tryAcquireShared(arg);//4、嘗試獲取共享鎖
                if (r >= 0) {//5、如果獲取成功
                    setHeadAndPropagate(node, r);//6、將資源傳播給下一個節點
                    p.next = null; // help GC
                    if (interrupted)
                        selfInterrupt();
                    failed = false;
                    return;
                }
            }
            if (shouldParkAfterFailedAcquire(p, node) &&//7、同獨佔模式
                parkAndCheckInterrupt())
                interrupted = true;
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}

private void setHeadAndPropagate(Node node, int propagate) {
    Node h = head; // Record old head for check below
    setHead(node);//1、獲取資源成功後,將自己設置爲頭節點
    /*
     * Try to signal next queued node if:
     *   Propagation was indicated by caller,
     *     or was recorded (as h.waitStatus either before
     *     or after setHead) by a previous operation
     *     (note: this uses sign-check of waitStatus because
     *      PROPAGATE status may transition to SIGNAL.)
     * and
     *   The next node is waiting in shared mode,
     *     or we don't know, because it appears null
     *
     * The conservatism in both of these checks may cause
     * unnecessary wake-ups, but only when there are multiple
     * racing acquires/releases, so most need signals now or soon
     * anyway.
     */
     //2、如果資源還有剩餘或者後續節點是共享節點
    if (propagate > 0 || h == null || h.waitStatus < 0 ||
        (h = head) == null || h.waitStatus < 0) {
        Node s = node.next;
        if (s == null || s.isShared())
            doReleaseShared();//3、喚醒後繼節點
    }
}

共享釋放

共享釋放的頂層方法爲releaseShared。

public final boolean releaseShared(int arg) {
        if (tryReleaseShared(arg)) {
            doReleaseShared();
            return true;
        }
        return false;
    }

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

看下tryReleaseShared在CountDownLatch中的實現。

protected boolean tryReleaseShared(int releases) {
            // Decrement count; signal when transition to zero
            for (;;) {
                int c = getState();
                if (c == 0)
                    return false;
                int nextc = c-1;
                if (compareAndSetState(c, nextc))
                    return nextc == 0;
            }
        }

很好理解,當前線程釋放一個共享資源,由於CountDownLatch是將資源總數減爲0,所以這裏釋放後是將state減1,。如果是Semaphore釋放資源後,state會進行加操作。

private void doReleaseShared() {
        for (;;) {
            Node h = head;
            if (h != null && h != tail) {
                int ws = h.waitStatus;
                if (ws == Node.SIGNAL) {
                    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                        continue;            // loop to recheck cases
                    unparkSuccessor(h);
                }
                else if (ws == 0 &&
                         !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                    continue;                // loop on failed CAS
            }
            if (h == head)                   // loop if head changed
                break;
        }
    }

源碼註釋比較清楚。unparkSuccessor和獨佔一樣,喚醒等待節點。

condition

Condition需要和lock一起使用,在此以ReentrantLock舉例。

        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        
        new Thread(()->{
            lock.lock();
            try {
                log.info("{} 線程獲取到鎖", Thread.currentThread());

                log.info("{} 線程調用condition.await", Thread.currentThread());
                condition.await();
                log.info("{} 線程被signal喚醒", Thread.currentThread());

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                log.info("{} 線程釋放鎖", Thread.currentThread());

                lock.unlock();
            }
        }).start();


        new Thread(()->{
            lock.lock();

            try {
                log.info("{} 線程獲取到鎖", Thread.currentThread());

                log.info("{} 線程調用condition.signal", Thread.currentThread());
                condition.signal();
                log.info("{} 線程調用signal喚醒後", Thread.currentThread());

            } finally {
                log.info("{} 線程釋放鎖", Thread.currentThread());

                lock.unlock();
            }
        }).start();

Condition是通過lock調用newCondition生成的。

public Condition newCondition() {
        return sync.newCondition();
}

final ConditionObject newCondition() {
        return new ConditionObject();
}

就是new一個ConditionObject對象。

public class ConditionObject implements Condition, java.io.Serializable {
        /** First node of condition queue. */
        private transient Node firstWaiter;
        /** Last node of condition queue. */
        private transient Node lastWaiter;

    public final void signal() {
        ...
    }

    public final void await() throws InterruptedException {
        ...
    }

}

ConditionObject位於Aqs內部,使用的時候通過newCondition生成,形成一個等待隊列。所以只有一個同步隊列,但是可以有多個等待隊列

public final void await() throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();//1、生成Condition節點
            int savedState = fullyRelease(node);//2、釋放資源,並返回之前持有資源的數量
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {//3、是否已經在同步隊列中,在第一次進來時,節點在等待隊列,會走到4;如果別的線程調用singal喚醒該線程後,節點會被加到同步隊列中,跳出循環
                LockSupport.park(this);//4、阻塞線程
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
            //5、嘗試獲取wait前需要的資源,即2的返回值
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null) // clean up if cancelled
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
        }

private Node addConditionWaiter() {
            Node t = lastWaiter;
            // If lastWaiter is cancelled, clean out.
            if (t != null && t.waitStatus != Node.CONDITION) {
                unlinkCancelledWaiters();
                t = lastWaiter;
            }
            Node node = new Node(Thread.currentThread(), Node.CONDITION);
            if (t == null)
                firstWaiter = node;
            else
                t.nextWaiter = node;
            lastWaiter = node;
            return node;
        }

final int fullyRelease(Node node) {
        boolean failed = true;
        try {
            int savedState = getState();//1、獲取當前狀態
            if (release(savedState)) {//2、釋放資源,喚醒後繼節點
                failed = false;//3、正常情況是可以成功的,因爲線程此時已經擁有了鎖
                return savedState;
            } else {
                throw new IllegalMonitorStateException();
            }
        } finally {
            if (failed)
                node.waitStatus = Node.CANCELLED;
        }
    }

調用await,生成節點放進等待隊列中,釋放已佔有的資源,等待喚醒。

public final void signal() {
            if (!isHeldExclusively())//1、當前線程必須已經佔有鎖
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                doSignal(first);//2、重置節點狀態,將節點加入同步隊列
}

private void doSignal(Node first) {
            do {
                if ( (firstWaiter = first.nextWaiter) == null)
                    lastWaiter = null;
                first.nextWaiter = null;//節點移出等待隊列
            } while (!transferForSignal(first) &&
                     (first = firstWaiter) != null);
        }

final boolean transferForSignal(Node node) {
        /*
         * If cannot change waitStatus, the node has been cancelled.
         */
        //1、重置節點的狀態爲0
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;

        /*
         * Splice onto queue and try to set waitStatus of predecessor to
         * indicate that thread is (probably) waiting. If cancelled or
         * attempt to set waitStatus fails, wake up to resync (in which
         * case the waitStatus can be transiently and harmlessly wrong).
         */
        Node p = enq(node);//2、將節點加入同步隊列中
        int ws = p.waitStatus;
        //3、如果狀態爲取消或者設置狀態爲SIGNAL失敗,喚醒節點
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        return true;
    }

signal主要將節點狀態由等待重置爲0,並將節點從等待隊列移除,加入同步隊列。喚醒操作是從等待隊列的首節點開始的。

一個示例圖如下

synchronized、wait、notify對應於JUC中就是lock、await、signal,不同於wait、notify只能作用於同一個對象,一個lock可以生成多個Condition,對應於多個等待隊列,使用上更加靈活。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章