AQS

  1. AQS 内部维护了一个FIFO双向队列,Node内部类就是队列的节点;(关键: FIFO、双向)
  2. head,此队列头结点
  3. tail,此队列尾节点
  4. state,表示同步状态,共享模式下,一个同步锁可能被多个线程持有,所以用int表示
  5. /**
  6. * Head of the wait queue, lazily initialized. Except for
  7. * initialization, it is modified only via method setHead. Note:
  8. * If head exists, its waitStatus is guaranteed not to be
  9. * CANCELLED.
  10. */
  11. private transient volatile Node head;
  12. /**
  13. * Tail of the wait queue, lazily initialized. Modified only via
  14. * method enq to add new wait node.
  15. */
  16. private transient volatile Node tail;
  17. /**
  18. * The synchronization state.
  19. */
  20. private volatile int state;
  1. /** Marker to indicate a node is waiting in shared mode */
  2. static final Node SHARED = new Node();
  3. /** Marker to indicate a node is waiting in exclusive mode */
  4. static final Node EXCLUSIVE = null;
  5. waitStatus 节点状态
  6. CANCELLED 1 被取消的线程
  7. SIGNAL -1 需要被唤醒的线程
  8. CONDITION -2 表示线程正在等待条件
  9. PROPAGATE -3 表示下一个acquisitionShared 无条件地传播
  10. 0 默认值,无意义
  11. /** waitStatus value to indicate thread has cancelled */
  12. static final int CANCELLED = 1;
  13. /** waitStatus value to indicate successor's thread needs unparking */
  14. static final int SIGNAL = -1;
  15. /** waitStatus value to indicate thread is waiting on condition */
  16. static final int CONDITION = -2;
  17. /**
  18. * waitStatus value to indicate the next acquireShared should
  19. * unconditionally propagate
  20. */
  21. static final int PROPAGATE = -3;
  22. /**
  23. * Status field, taking on only the values:
  24. * SIGNAL: The successor of this node is (or will soon be)
  25. * blocked (via park), so the current node must
  26. * unpark its successor when it releases or
  27. * cancels. To avoid races, acquire methods must
  28. * first indicate they need a signal,
  29. * then retry the atomic acquire, and then,
  30. * on failure, block.
  31. * CANCELLED: This node is cancelled due to timeout or interrupt.
  32. * Nodes never leave this state. In particular,
  33. * a thread with cancelled node never again blocks.
  34. * CONDITION: This node is currently on a condition queue.
  35. * It will not be used as a sync queue node
  36. * until transferred, at which time the status
  37. * will be set to 0. (Use of this value here has
  38. * nothing to do with the other uses of the
  39. * field, but simplifies mechanics.)
  40. * PROPAGATE: A releaseShared should be propagated to other
  41. * nodes. This is set (for head node only) in
  42. * doReleaseShared to ensure propagation
  43. * continues, even if other operations have
  44. * since intervened.
  45. * 0: None of the above
  46. *
  47. * The values are arranged numerically to simplify use.
  48. * Non-negative values mean that a node doesn't need to
  49. * signal. So, most code doesn't need to check for particular
  50. * values, just for sign.
  51. *
  52. * The field is initialized to 0 for normal sync nodes, and
  53. * CONDITION for condition nodes. It is modified using CAS
  54. * (or when possible, unconditional volatile writes).
  55. */
  56. volatile int waitStatus;
  57. /**
  58. * Link to predecessor node that current node/thread relies on
  59. * for checking waitStatus. Assigned during enqueuing, and nulled
  60. * out (for sake of GC) only upon dequeuing. Also, upon
  61. * cancellation of a predecessor, we short-circuit while
  62. * finding a non-cancelled one, which will always exist
  63. * because the head node is never cancelled: A node becomes
  64. * head only as a result of successful acquire. A
  65. * cancelled thread never succeeds in acquiring, and a thread only
  66. * cancels itself, not any other node.
  67. */
  68. volatile Node prev;
  69. /**
  70. * Link to the successor node that the current node/thread
  71. * unparks upon release. Assigned during enqueuing, adjusted
  72. * when bypassing cancelled predecessors, and nulled out (for
  73. * sake of GC) when dequeued. The enq operation does not
  74. * assign next field of a predecessor until after attachment,
  75. * so seeing a null next field does not necessarily mean that
  76. * node is at end of queue. However, if a next field appears
  77. * to be null, we can scan prev's from the tail to
  78. * double-check. The next field of cancelled nodes is set to
  79. * point to the node itself instead of null, to make life
  80. * easier for isOnSyncQueue.
  81. */
  82. volatile Node next;
  83. /**
  84. * The thread that enqueued this node. Initialized on
  85. * construction and nulled out after use.
  86. */
  87. volatile Thread thread;
  88. /**
  89. * Link to next node waiting on condition, or the special
  90. * value SHARED. Because condition queues are accessed only
  91. * when holding in exclusive mode, we just need a simple
  92. * linked queue to hold nodes while they are waiting on
  93. * conditions. They are then transferred to the queue to
  94. * re-acquire. And because conditions can only be exclusive,
  95. * we save a field by using special value to indicate shared
  96. * mode.
  97. */
  98. Node nextWaiter;
  • 独占模式 EXCLUSIVE
  • 共享模式 SHARED

获取锁的两种模式

  1. 尝试获取锁,但实际是否获得锁并不重要。获取到锁直接返回true;没有获取到锁直接返回false。

不会阻塞

  1. 没有获得锁的话就会一直阻塞,直到获得锁

这两种模式在AQS中都有实现,分别是 tryAcquire()acquire()

tryAcquire()

tryAcquire()用于尝试获取锁,获取结果会立即返回。
AQS中的tryAcquire()方法并没有提供具体的实现,要求AQS的继承者必须自己实现此方法,例如ReentryLock、CountDownLacth,都自己实现了tryAcquire()方法。如果没有实现电话,会抛出UnsupportedOperationException() 异常

  1. /**
  2. * Attempts to acquire in exclusive mode. This method should query
  3. * if the state of the object permits it to be acquired in the
  4. * exclusive mode, and if so to acquire it.
  5. *
  6. * <p>This method is always invoked by the thread performing
  7. * acquire. If this method reports failure, the acquire method
  8. * may queue the thread, if it is not already queued, until it is
  9. * signalled by a release from some other thread. This can be used
  10. * to implement method {@link Lock#tryLock()}.
  11. *
  12. * <p>The default
  13. * implementation throws {@link UnsupportedOperationException}.
  14. *
  15. * @param arg the acquire argument. This value is always the one
  16. * passed to an acquire method, or is the value saved on entry
  17. * to a condition wait. The value is otherwise uninterpreted
  18. * and can represent anything you like.
  19. * @return {@code true} if successful. Upon success, this object has
  20. * been acquired.
  21. * @throws IllegalMonitorStateException if acquiring would place this
  22. * synchronizer in an illegal state. This exception must be
  23. * thrown in a consistent fashion for synchronization to work
  24. * correctly.
  25. * @throws UnsupportedOperationException if exclusive mode is not supported
  26. */
  27. protected boolean tryAcquire(int arg) {
  28. throw new UnsupportedOperationException();
  29. }

acquire()

用于在独占模式下获取锁,直到成功
执行步骤:

  1. 执行tryAcquire(),如果获取到锁(返回ture),直接执行selfInterrupt()
  2. tryAcquire()没有获取到锁(返回false),执行 acquireQueued(addWaiter(Node.EXCLUSIVE), arg),

节点入队

  1. addWaiter(Node.EXCLUSIVE)
    1. 将当前线程封装成Node,并加入等待队列
  2. acquireQueued(node,arg)
  3. 如果获取锁失败,会执行 if 的代码 selfInterrupt(),将当前线程中断
  1. /**
  2. * Acquires in exclusive mode, ignoring interrupts. Implemented
  3. * by invoking at least once {@link #tryAcquire},
  4. * returning on success. Otherwise the thread is queued, possibly
  5. * repeatedly blocking and unblocking, invoking {@link
  6. * #tryAcquire} until success. This method can be used
  7. * to implement method {@link Lock#lock}.
  8. *
  9. * @param arg the acquire argument. This value is conveyed to
  10. * {@link #tryAcquire} but is otherwise uninterpreted and
  11. * can represent anything you like.
  12. */
  13. public final void acquire(int arg) {
  14. if (
  15. !tryAcquire(arg) &&
  16. acquireQueued(addWaiter(Node.EXCLUSIVE), arg)
  17. )
  18. selfInterrupt();
  19. }
  1. 1. 将当前线程,封装成node
  2. 2. 如果尾节点不为空,则用cas将当前节点设为尾节点
  3. 3. 如果cas失败,则进入完整入队方法 enq()
  4. /**
  5. * Creates and enqueues node for current thread and given mode.
  6. *
  7. * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
  8. * @return the new node
  9. */
  10. private Node addWaiter(Node mode) {
  11. Node node = new Node(Thread.currentThread(), mode);
  12. // Try the fast path of enq; backup to full enq on failure
  13. // 先尝试快速入队,失败的话就调用完整入队方法
  14. Node pred = tail;
  15. if (pred != null) {
  16. node.prev = pred;
  17. if (compareAndSetTail(pred, node)) {
  18. pred.next = node;
  19. return node;
  20. }
  21. }
  22. enq(node);
  23. return node;
  24. }
  1. 入队方法,将节点node加入队列
  2. 1. 如果尾节点为空(说明队列还未初始化),则用cas初始化一个队列
  3. 2. 如果尾节点不为空,则用cas将当前节点设为尾节点
  4. /**
  5. * Inserts node into queue, initializing if necessary. See picture above.
  6. * @param node the node to insert
  7. * @return node's predecessor
  8. */
  9. private Node enq(final Node node) {
  10. for (;;) { // 自旋操作
  11. Node t = tail;
  12. if (t == null) { // Must initialize
  13. if (compareAndSetHead(new Node()))
  14. tail = head;
  15. } else {
  16. node.prev = t;
  17. if (compareAndSetTail(t, node)) {
  18. t.next = node;
  19. return t;
  20. }
  21. }
  22. }
  23. }
  1. 配合release方法,对线程进行挂起和响应。
  2. 对已经在队列中的线程以不间断的方式进行获取。被条件等待方法和获取方法所调用。
  3. 1. 判断
  4. cancelAcquire(node)
  5. /**
  6. * Acquires in exclusive uninterruptible mode for thread already in
  7. * queue. Used by condition wait methods as well as acquire.
  8. *
  9. * @param node the node
  10. * @param arg the acquire argument
  11. * @return {@code true} if interrupted while waiting
  12. */
  13. final boolean acquireQueued(final Node node, int arg) {
  14. boolean failed = true;
  15. try {
  16. boolean interrupted = false;
  17. for (;;) {
  18. // 判断当前节点的前一节点是否是头结点
  19. final Node p = node.predecessor();
  20. if (p == head && tryAcquire(arg)) {
  21. setHead(node);
  22. p.next = null; // help GC
  23. failed = false;
  24. return interrupted;
  25. }
  26. // 判断当前线程是否需要被挂起,如果成功挂起,则interrupted置为true
  27. if (shouldParkAfterFailedAcquire(p, node) &&
  28. parkAndCheckInterrupt())
  29. interrupted = true;
  30. }
  31. } finally {
  32. if (failed)
  33. cancelAcquire(node);
  34. }
  35. }
  1. 判断当前节点的线程是否需要挂起
  2. /**
  3. * Checks and updates status for a node that failed to acquire.
  4. * Returns true if thread should block. This is the main signal
  5. * control in all acquire loops. Requires that pred == node.prev.
  6. *
  7. * @param pred node's predecessor holding status
  8. * @param node the node
  9. * @return {@code true} if thread should block
  10. */
  11. private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
  12. int ws = pred.waitStatus;
  13. if (ws == Node.SIGNAL)
  14. /*
  15. * This node has already set status asking a release
  16. * to signal it, so it can safely park.
  17. */
  18. // 前置节点状态为signal,前置节点也在等待拿锁,那当前节点就可以挂起等待。
  19. return true;
  20. if (ws > 0) {
  21. /*
  22. * Predecessor was cancelled. Skip over predecessors and
  23. * indicate retry.
  24. */
  25. // 前置节点状态为cancel,则删除前面所有waitStatus为CANCEL的节点
  26. do {
  27. node.prev = pred = pred.prev;
  28. } while (pred.waitStatus > 0);
  29. pred.next = node;
  30. } else {
  31. /*
  32. * waitStatus must be 0 or PROPAGATE. Indicate that we
  33. * need a signal, but don't park yet. Caller will need to
  34. * retry to make sure it cannot acquire before parking.
  35. */
  36. // 其他状态,将前置节点waitStatus置为SIGNAL
  37. compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
  38. }
  39. return false;
  40. }
将当前节点线程挂起
    /**
     * Convenience method to park and then check if interrupted
     *
     * @return {@code true} if interrupted
     */
    private final boolean parkAndCheckInterrupt() {
        LockSupport.park(this);
        return Thread.interrupted();
    }

只有Head的后一个节点有资格拿锁,且需要保证同时有且只有这一个节点在拿锁。
AQS源码解析 - 图1
被挂起的线程何时被唤醒呢?

释放锁

tryRelease()由继承子类自行定义逻辑

    protected boolean tryRelease(int arg) {
        throw new UnsupportedOperationException();
    }
1. 调用tryRealease()方法尝试释放锁
2. 如果tryRealease()成功,返回true,则唤醒队列的下一节点
    /**
     * 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;
    }
node 为头结点(也就是刚刚执行结束的线程)
1. 将node的waitStatus 通过cas置为0
2. 寻找node的继任者,通常及时node的next节点,但有时也不是,这是就要查找
3. 查找逻辑:
    从队列尾节点开始向前寻找 最靠近node的waitStatus<=0 的节点


    /**
     * 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) {
            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);
    }

!为什么寻找successor时要从后往前查找?

首先要明确,从后往前寻找时,并不是找到第一个符合s == null || s.waitStatus > 0就行,而是一直找到离node最近的那一个。所以就有了这个疑问,既然要找的是离node最近的一个,为什么不直接从node开始向后寻找呢?
原因在入队方法enq()逻辑里
重点关注入队语句,将一个节点node入队,即放在尾节点Tail后,有两个步骤:

// 
tail.next = node;

node.prev = tail;

从逻辑上讲,单线程环境下,这两个步骤谁先谁后无所谓。而多线程下,就有问题了(因为这两个操作不是原子操作),回到AQS的入队操作:

if (compareAndSetTail(t, node)) {
    t.next = node;
    return t;
}

上面逻辑中,compareAndSetTail(t, node),利用cas将node的prev指针设置为tail,成功后再执行if括号里的内容;但是括号里的操作不是同步操作。这个时刻,node的prev指针已经指向了tail,但是tail.next
指针还没建立。若此时其他线程调用了unpark操作,那tail的next指针就无法成功建立。
所以从头往后就无法完整遍历队列,从后往前就可以

    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;
                }
            }
        }
    }

Head头结点

头结点是个虚节点,不存储实际线程;一个线程的节点被设为head节点,意外该线程节点完成,出队。

    private void setHead(Node node) {
        head = node;
        node.thread = null;
        node.prev = null;
    }