AbstractQueuedSynchronizer源码详细分析【Condition条件】

AQS是JUC锁框架中最重要的类,通过它来实现独占锁和共享锁的。

本章是对AbstractQueuedSynchronizer源码的完全解析,分为四个部分介绍:

  1. CLH队列即同步队列:储存着所有等待锁的线程
  2. 独占锁
  3. 共享锁
  4. Condition条件

注: 还有一个AbstractQueuedLongSynchronizer类,它与AQS功能和实现几乎一样,唯一不同的是AQLS中代表锁被获取次数的成员变量state类型是long长整类型,而AQS中该成员变量是int类型。

一. CLH 队列

二. 独占锁

三. 共享锁

四. Condition条件

Condition是为了实现线程之间相互等待的问题。注意Condition对象只能在独占锁中才能使用。

考虑一下情况,有两个线程,生产者线程,消费者线程。当消费者线程消费东西时,发现没有东西,这时它就要等待,让生产者线程生产东西后,在通知它消费。
因为操作的是同一个资源,所以要加锁,防止多线程冲突。而锁在同一时间只能有一个线程持有,所以消费者在让线程等待前,必须释放锁,且唤醒另一个等待锁的线程。
那么在AQS中Condition条件又是如何实现的呢?

  1. 首先内部存在一个Condition队列,存储着所有在此Condition条件等待的线程。
  2. await系列方法:让当前持有锁的线程释放锁,并唤醒一个在CLH队列上等待锁的线程,再为当前线程创建一个node节点,插入到Condition队列(注意不是插入到CLH队列中)
  3. signal系列方法:其实这里没有唤醒任何线程,而是将Condition队列上的等待节点插入到CLH队列中,所以当持有锁的线程执行完毕释放锁时,就会唤醒CLH队列中的一个线程,这个时候才会唤醒线程。

4.1 await系列方法

4.1.1 await方法

        //让当前线程阻塞,并且释放锁。如果线程有中断请求,则抛出异常
        public final void await() throws InterruptedException {
            // 如果当前线程中断标志位是true,就抛出InterruptedException异常
            if (Thread.interrupted())
                throw new InterruptedException();
            // 为当前线程新建node节点,并且加入condition队列中
            Node node = addConditionWaiter();
            // 释放当前线程锁占有的锁,并且唤醒CLH队列一个等待线程
            int savedState = fullyRelease(node);
            int interruptMode = 0;
            // 如果当前node节点不在CLH队列中
            while (!isOnSyncQueue(node)) {
                // 阻塞当前线程
                LockSupport.park(this);
                // 如果当前线程产生中断请求,就跳出循环
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
             // 如果节点node已经在同步队列中了,获取同步锁,只有得到锁才能继续执行,否则线程继续阻塞等待
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            // 清除Condition队列中状态不是Node.CONDITION的节点
            if (node.nextWaiter != null) 
                unlinkCancelledWaiters();
            // 是否要抛出异常,或者发出中断请求
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
        }

方法流程: 

  1. addConditionWaiter方法:为当前线程创建新的Node节点,并且将这个节点插入到Condition队列中了
  2. fullyRelease方法:释放当前线程占有的锁,并唤醒CLH队列一个等待线程
  3. isOnSyncQueue 方法:如果返回false,表示节点node不在CLH队列中,即没有调用过 signal系列方法,所以调用LockSupport.park(this)方法阻塞当前线程。
  4. 如果跳出while循环,表示节点node已经在CLH队列中,那么调用acquireQueued方法去获取锁。
  5. 清除Condition队列中状态不是Node.CONDITION的节点

4.1.2 addConditionWaiter方法

        //为当前线程创建新的Node节点,并且将这个节点插入到Condition队列中了
        private Node addConditionWaiter() {
            Node t = lastWaiter;
            // 如果Condition队列尾节点的状态不是Node.CONDITION
            if (t != null && t.waitStatus != Node.CONDITION) {
                // 清除Condition队列中,状态不是Node.CONDITION的节点,
                // 并且可能会重新设置firstWaiter和lastWaiter
                unlinkCancelledWaiters();
                // 重新将Condition队列尾赋值给t
                t = lastWaiter;
            }
            // 为当前线程创建一个状态为Node.CONDITION的节点
            Node node = new Node(Thread.currentThread(), Node.CONDITION);
            // 如果t为null,表示Condition队列为空,将node节点赋值给链表头
            if (t == null)
                firstWaiter = node;
            else
                // 将新节点node插入到Condition队列尾
                t.nextWaiter = node;
            // 将新节点node设置为新的Condition队列尾
            lastWaiter = node;
            return node;
        }

4.1.3 fullyRelease方法

释放当前线程占有的锁,并唤醒CLH队列一个等待线程

    // 释放当前线程占有的锁,并唤醒CLH队列一个等待线程
    final int fullyRelease(Node node) {
        boolean failed = true;
        try {
            int savedState = getState();
            // 释放当前线程锁占有的锁
            if (release(savedState)) {
                failed = false;
                return savedState;
            } else {
                throw new IllegalMonitorStateException();
            }
        } finally {
            if (failed)
                node.waitStatus = Node.CANCELLED;
        }
    }

4.1.4 isOnSyncQueue

节点node是不是在CLH队列中

    final boolean isOnSyncQueue(Node node) {
        // 如果node的状态是Node.CONDITION,或者node没有前一个节点prev,
        // 那么返回false,节点node不在同步队列中
        if (node.waitStatus == Node.CONDITION || node.prev == null)
            return false;
        // 如果node有下一个节点next,那么它一定在同步队列中
        if (node.next != null) 
            return true;
        // 从同步队列中查找节点node
        return findNodeFromTail(node);
    }


    // 在同步队列中从后向前查找节点node,如果找到返回true,否则返回false
    private boolean findNodeFromTail(Node node) {
        Node t = tail;
        for (;;) {
            if (t == node)
                return true;
            if (t == null)
                return false;
            t = t.prev;
        }
    }

4.1.5 acquireQueued方法

获取独占锁,这个在独占锁章节已经说过

4.1.6 unlinkCancelledWaiters 方法

清除Condition队列中状态不是Node.CONDITION的节点

        // 清楚condition队列中不是condition状态的节点
        private void unlinkCancelledWaiters() {
            Node t = firstWaiter;
            Node trail = null;
            while (t != null) {
                Node next = t.nextWaiter;
                if (t.waitStatus != Node.CONDITION) {
                    t.nextWaiter = null;
                    if (trail == null)
                        firstWaiter = next;
                    else
                        trail.nextWaiter = next;
                    if (next == null)
                        lastWaiter = trail;
                }
                else
                    trail = t;
                t = next;
            }
        }

4.1.7 reportInterruptAfterWait方法

        /**
         * 如果interruptMode是THROW_IE,就抛出InterruptedException异常
         * 如果interruptMode是REINTERRUPT,则当前线程再发出中断请求
         * 否则就什么都不做
         */
        private void reportInterruptAfterWait(int interruptMode)
            throws InterruptedException {
            if (interruptMode == THROW_IE)
                throw new InterruptedException();
            else if (interruptMode == REINTERRUPT)
                selfInterrupt();
        }

4.2 signal系列方法

4.2.1 signal方法

        // 如果condition队列不为空,则讲condition队列插入到CLH队列头中
        public final void signal() {
            // condtion只针对独占锁,如果不是独占锁就抛出异常
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                // 将Condition队列中的first节点插入到CLH队列中
                doSignal(first);
        }

如果condition队列不为空,就调用doSignal方法将condition队列头节点插入到CLH队列中。

        // 将condition队列插入到CLH队列中
        private void doSignal(Node first) {
            do {
                // 原先的Condition队列头节点取消,所以重新赋值Condition队列头节点
                // 如果新的Condition队列头节点为null,表示Condition队列为空了
                // ,所以也要设置Condition队列尾lastWaiter为null
                if ( (firstWaiter = first.nextWaiter) == null)
                    lastWaiter = null;
                // 取消first节点nextWaiter引用 节点独立出来了
                first.nextWaiter = null;
            } while (!transferForSignal(first) &&
                     (first = firstWaiter) != null);
        }

为什么使用while循环,因为只有是Node.CONDITION状态的节点才能插入CLH队列,如果不是这个状态,那么循环Condition队列下一个节点。

4.2.3 transferForSignal方法

    // 返回true表示节点node插入到同步队列中,返回false表示节点node没有插入到同步队列中
    final boolean transferForSignal(Node node) {
        // 如果节点node的状态不是Node.CONDITION,或者更新状态失败,
        // 说明该node节点已经插入到同步队列中,所以直接返回false
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;
        // 将节点插入到尾结点
        Node p = enq(node);
        int ws = p.waitStatus;
        // 如果前一个节点是已取消状态,或者不能将它设置成Node.SIGNAL状态。
        // 就说明节点p之后也不会发起唤醒下一个node节点线程的操作,
        // 所以这里直接调用 LockSupport.unpark(node.thread)方法,唤醒节点node所在线程
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        return true;
    }
  1. 状态不是 Node.CONDITION的节点,是不能从Condition队列中插入到CLH队列中。直接返回false
  2. 调用enq方法,将节点node插入到同步队列中,p是原先同步队列尾节点,也是node节点的前一个节点
  3. 如果前一个节点是已取消状态,或者不能将它设置成Node.SIGNAL状态。那么就要LockSupport.unpark(node.thread)方法唤醒node节点所在线程。

4.2.4 signalAll 方法

        public final void signalAll() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                doSignalAll(first);
        }

4.2.5 doSignalAll方法

        /**
         * 将condition队列中所有的节点都插入到同步队列中
         * @param first condition队列头节点
         */
        private void doSignalAll(Node first) {
            // 表示将condition队列设置为空
            lastWaiter = firstWaiter = null;
            do {
                // 得到condition队列的下一个节点
                Node next = first.nextWaiter;
                first.nextWaiter = null;
                // 将节点first插入到同步队列中
                transferForSignal(first);
                first = next;
            // 循环遍历condition队列中所有的节点
            } while (first != null);
        }

循环遍历整个condition队列,调用transferForSignal方法,将节点插入到CLH队列中。

4.3 小结

Condition只能使用在独占锁中。它内部有一个Condition队列记录所有在Condition条件等待的线程(即就是调用await系列方法后等待的线程).
await系列方法:会让当前线程释放持有的锁,并唤醒在CLH队列上的一个等待锁的线程,再将当前线程插入到Condition队列中(注意不是CLH队列)
signal系列方法:并不是唤醒线程,而是将Condition队列中的节点插入到CLH队列中。

 

附录

     package java.util.concurrent.locks;

import sun.misc.Unsafe;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {

    private static final long serialVersionUID = 7373984972572414691L;


    protected AbstractQueuedSynchronizer() { }

    static final class Node {
        // 共享模式的标记
        static final Node SHARED = new Node();
        // 独占模式的标记
        static final Node EXCLUSIVE = null;

        // waitStatus变量的值,标志着线程被取消
        static final int CANCELLED =  1;
        // waitStatus变量的值,标志着后继线程(即队列中此节点之后的节点)需要被阻塞.(用于独占锁)
        static final int SIGNAL    = -1;
        // waitStatus变量的值,标志着线程在Condition条件上等待阻塞.(用于Condition的await等待)
        static final int CONDITION = -2;
        // waitStatus变量的值,标志着下一个acquireShared方法线程应该被允许。(用于共享锁)
        static final int PROPAGATE = -3;

        // 标记着当前节点的状态,默认状态是0, 小于0的状态都是有特殊作用,大于0的状态表示已取消
        volatile int waitStatus;

        // prev和next实现一个双向链表
        volatile Node prev;
        volatile Node next;

        // 该节点拥有的线程
        volatile Thread thread;

        // 可能有两种作用:1. 表示下一个在Condition条件上等待的节点
        // 2. 表示是共享模式或者独占模式,注意第一种情况节点一定是共享模式
        Node nextWaiter;

        // 是不是共享模式
        final boolean isShared() {
            return nextWaiter == SHARED;
        }

        // 返回前一个节点prev,如果为null,则抛出NullPointerException异常
        final Node predecessor() throws NullPointerException {
            Node p = prev;
            if (p == null)
                throw new NullPointerException();
            else
                return p;
        }

        // 用于创建链表头head,或者共享模式SHARED
        Node() {
        }

        // 使用在addWaiter方法中
        Node(Thread thread, Node mode) {
            this.nextWaiter = mode;
            this.thread = thread;
        }

        // 使用在Condition条件中
        Node(Thread thread, int waitStatus) {
            this.waitStatus = waitStatus;
            this.thread = thread;
        }
    }

    // CLH队列头
    private transient volatile Node head;

    // CLH队列尾
    private transient volatile Node tail;

    // 用来记录当前锁被获取的次数,当state==0,表示还没有被任何线程获取
    private volatile int state;

    protected final int getState() {
        return state;
    }

    protected final void setState(int newState) {
        state = newState;
    }

    // 采用CAS函数。比较并交换函数,它是原子操作函数;即,通过CAS操作的数据都是以原子方式进行的
    protected final boolean compareAndSetState(int expect, int update) {
        return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
    }

    // Queuing utilities

    static final long spinForTimeoutThreshold = 1000L;

    // 向队列尾插入新节点,如果队列没有初始化,就先初始化。返回原先的队列尾节点
    private Node enq(final Node node) {
        for (;;) {
            Node t = tail;
            // t为null,表示队列为空,先初始化队列
            if (t == null) {
                // 采用CAS函数即原子操作方式,设置队列头head值。
                // 如果成功,再将head值赋值给链表尾tail。如果失败,表示head值已经被其他线程,那么就进入循环下一次
                if (compareAndSetHead(new Node()))
                    tail = head;
            } else {
                // 新添加的node节点的前一个节点prev指向原来的队列尾tail
                node.prev = t;
                // 采用CAS函数即原子操作方式,设置新队列尾tail值。
                if (compareAndSetTail(t, node)) {
                    // 设置老的队列尾tail的下一个节点next指向新添加的节点node
                    t.next = node;
                    return t;
                }
            }
        }
    }

    // 通过给定的模式mode(独占或者共享)为当前线程创建新节点,并插入队列中
    private Node addWaiter(Node mode) {
        // 为当前线程创建新的节点
        Node node = new Node(Thread.currentThread(), mode);
        Node pred = tail;
        // 如果队列已经创建,就将新节点插入队列尾。
        if (pred != null) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        // 如果队列没有创建,通过enq方法创建队列,并插入新的节点。
        enq(node);
        return node;
    }

    // 重新设置队列头head,它只在acquire系列的方法中调用
    private void setHead(Node node) {
        head = node;
        // 线程也没有意义了,因为该线程已经获取到锁了
        node.thread = null;
        // 前一个节点已经没有意义了
        node.prev = null;
    }

    // 唤醒node节点的下一个非取消状态的节点所在线程(即waitStatus<=0)
    private void unparkSuccessor(Node node) {
        // 获取node节点的状态
        int ws = node.waitStatus;
        // 如果小于0,就将状态重新设置为0,表示这个node节点已经完成了
        if (ws < 0)
            compareAndSetWaitStatus(node, ws, 0);

        // 下一个节点
        Node s = node.next;
        // 如果下一个节点为null,或者状态是已取消,那么就要寻找下一个非取消状态的节点
        if (s == null || s.waitStatus > 0) {
            // 先将s设置为null,s不是非取消状态的节点
            s = null;
            // 从队列尾向前遍历,直到遍历到node节点
            for (Node t = tail; t != null && t != node; t = t.prev)
                // 因为是从后向前遍历,所以不断覆盖找到的值,这样才能得到node节点后下一个非取消状态的节点
                if (t.waitStatus <= 0)
                    s = t;
        }
        // 如果s不为null,表示存在非取消状态的节点。那么调用LockSupport.unpark方法,唤醒这个节点的线程
        if (s != null)
            LockSupport.unpark(s.thread);
    }

    // 会唤醒等待共享锁的线程
    private void doReleaseShared() {
        for (;;) {
            // 将同步队列头赋值给节点h
            Node h = head;
            // 如果节点h不为null,且不等于同步队列尾
            if (h != null && h != tail) {
                // 得到节点h的状态
                int ws = h.waitStatus;
                // 如果状态是Node.SIGNAL,就要唤醒节点h后继节点的线程
                if (ws == Node.SIGNAL) {
                    // 将节点h的状态设置成0,如果设置失败,就继续循环,再试一次。
                    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                        continue;            // loop to recheck cases
                    // 唤醒节点h后继节点的线程
                    unparkSuccessor(h);
                }
                // 如果节点h的状态是0,就设置ws的状态是PROPAGATE。
                else if (ws == 0 &&
                         !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                    continue;                // loop on failed CAS
            }
            // 如果同步队列头head节点发生改变,继续循环,
            // 如果没有改变,就跳出循环
            if (h == head)
                break;
        }
    }

    // 重新设置CLH队列头,如果CLH队列头的下一个节点为null或者共享模式,
    // 那么就要唤醒共享锁上等待的线程
    private void setHeadAndPropagate(Node node, int propagate) {
        Node h = head;
        // 设置新的同步队列头head
        setHead(node);
        // 如果propagate大于0,
        if (propagate > 0 || h == null || h.waitStatus < 0 ||
            (h = head) == null || h.waitStatus < 0) {
            // 获取新的CLH队列头的下一个节点s
            Node s = node.next;
            // 如果节点s是空或者共享模式节点,那么就要唤醒共享锁上等待的线程
            if (s == null || s.isShared())
                doReleaseShared();
        }
    }

    // 则将node节点的状态设置成CANCELLED,表示node节点所在线程已取消,不需要唤醒了。
    private void cancelAcquire(Node node) {
        // 如果node为null,就直接返回
        if (node == null)
            return;

        //
        node.thread = null;

        // 跳过那些已取消的节点,在队列中找到在node节点前面的第一次状态不是已取消的节点
        Node pred = node.prev;
        while (pred.waitStatus > 0)
            node.prev = pred = pred.prev;

        // 记录pred原来的下一个节点,用于CAS函数更新时使用
        Node predNext = pred.next;

        // Can use unconditional write instead of CAS here.
        // After this atomic step, other Nodes can skip past us.
        // Before, we are free of interference from other threads.
        // 将node节点状态设置为已取消Node.CANCELLED;
        node.waitStatus = Node.CANCELLED;

        // 如果node节点是队列尾节点,那么就将pred节点设置为新的队列尾节点
        if (node == tail && compareAndSetTail(node, pred)) {
            // 并且设置pred节点的下一个节点next为null
            compareAndSetNext(pred, predNext, null);
        } else {
            // If successor needs signal, try to set pred's next-link
            // so it will get one. Otherwise wake it up to propagate.
            int ws;
            if (pred != head &&
                ((ws = pred.waitStatus) == Node.SIGNAL ||
                 (ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) &&
                pred.thread != null) {
                Node next = node.next;
                if (next != null && next.waitStatus <= 0)
                    compareAndSetNext(pred, predNext, next);
            } else {
                unparkSuccessor(node);
            }

            node.next = node; // help GC
        }
    }

    /**
     * 根据前一个节点pred的状态,来判断当前线程是否应该被阻塞
     * @param pred : node节点的前一个节点
     * @param node
     * @return 返回true 表示当前线程应该被阻塞,之后应该会调用parkAndCheckInterrupt方法来阻塞当前线程
     */
    private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;
        if (ws == Node.SIGNAL)
            // 如果前一个pred的状态是Node.SIGNAL,那么直接返回true,当前线程应该被阻塞
            return true;
        if (ws > 0) {
            // 如果前一个节点状态是Node.CANCELLED(大于0就是CANCELLED),
            // 表示前一个节点所在线程已经被唤醒了,要从CLH队列中移除CANCELLED的节点。
            // 所以从pred节点一直向前查找直到找到不是CANCELLED状态的节点,并把它赋值给node.prev,
            // 表示node节点的前一个节点已经改变。
            do {
                node.prev = pred = pred.prev;
            } while (pred.waitStatus > 0);
            pred.next = node;
        } else {
            // 此时前一个节点pred的状态只能是0或者PROPAGATE,不可能是CONDITION状态
            // CONDITION(这个是特殊状态,只在condition列表中节点中存在,CLH队列中不存在这个状态的节点)
            // 将前一个节点pred的状态设置成Node.SIGNAL,这样在下一次循环时,就是直接阻塞当前线程
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;
    }

    /**
     * 当前线程发出中断通知
     */
    static void selfInterrupt() {
        Thread.currentThread().interrupt();
    }

    /**
     * 阻塞当前线程,线程被唤醒后返回当前线程中断状态
     */
    private final boolean parkAndCheckInterrupt() {
        // 通过LockSupport.park方法,阻塞当前线程
        LockSupport.park(this);
        // 当前线程被唤醒后,返回当前线程中断状态
        return Thread.interrupted();
    }

    /**
     * 想要获取锁的 acquire系列方法,都会这个方法来获取锁
     * 循环通过tryAcquire方法不断去获取锁,如果没有获取成功,就有可能调用parkAndCheckInterrupt方法,让当前线程阻塞
     * @param node 想要获取锁的节点
     * @param arg
     * @return 返回true,表示在线程等待的过程中,线程被中断了
     */
    final boolean acquireQueued(final Node node, int arg) {
        boolean failed = true;
        try {
            // 表示线程在等待过程中,是否被中断了
            boolean interrupted = false;
            // 通过死循环,直到node节点的线程获取到锁,才返回
            for (;;) {
                // 获取node的前一个节点
                final Node p = node.predecessor();
                // 如果前一个节点是队列头head,并且尝试获取锁成功
                // 那么当前线程就不需要阻塞等待,继续执行
                if (p == head && tryAcquire(arg)) {
                    // 将节点node设置为新的队列头
                    setHead(node);
                    // help GC
                    p.next = null;
                    // 不需要调用cancelAcquire方法
                    failed = false;
                    return interrupted;
                }
                // 当p节点的状态是Node.SIGNAL时,就会调用parkAndCheckInterrupt方法,阻塞node线程
                // node线程被阻塞,有两种方式唤醒,
                // 1.是在unparkSuccessor(Node node)方法,会唤醒被阻塞的node线程,返回false
                // 2.node线程被调用了interrupt方法,线程被唤醒,返回true
                // 在这里只是简单地将interrupted = true,没有跳出for的死循环,继续尝试获取锁
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            // failed为true,表示发生异常,
            // 则将node节点的状态设置成CANCELLED,表示node节点所在线程已取消,不需要唤醒了。
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     * 这个方法与acquireQueued(final Node node, int arg)方法流程几乎一样
     * 只不过当parkAndCheckInterrupt返回true时,直接抛出异常。
     * @param arg
     * @throws InterruptedException
     */
    private void doAcquireInterruptibly(int arg)
        throws InterruptedException {
        // 为当前线程创建节点node,并插入到队列中
        final Node node = addWaiter(Node.EXCLUSIVE);
        boolean failed = true;
        try {
            // 通过死循环,直到node节点的线程获取到锁,或者当前线程有中断请求会抛出中断异常
            for (;;) {
                final Node p = node.predecessor();
                // 如果前一个节点是队列头head,并且尝试获取锁成功
                // 将该节点node设置成队列头head
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return;
                }
                // 当p节点的状态是Node.SIGNAL时,就会调用parkAndCheckInterrupt方法,阻塞node线程
                // node线程被阻塞,有两种方式唤醒,
                // 1.是在unparkSuccessor(Node node)方法,会唤醒被阻塞的node线程,返回false
                // 2.node线程被调用了interrupt方法,线程被唤醒,返回true
                // 在这里如果parkAndCheckInterrupt返回true,就会抛出InterruptedException异常
                // 跳出死循环,方法返回
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }


    /**
     * 尝试在一定的时间nanosTimeout内获取锁,超时了就返回false
     *
     */
    private boolean doAcquireNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (nanosTimeout <= 0L)
            return false;
        // 计算截止时间
        final long deadline = System.nanoTime() + nanosTimeout;
        // 为当前线程创建节点node,并插入到队列中
        final Node node = addWaiter(Node.EXCLUSIVE);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                // 如果前一个节点是队列头head,并且尝试获取锁成功
                // 将该节点node设置成队列头head
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return true;
                }
                // 计算剩余时间
                nanosTimeout = deadline - System.nanoTime();
                // 剩余时间小于等于0,就直接返回false,获取锁失败
                if (nanosTimeout <= 0L)
                    return false;
                // 当p节点的状态是Node.SIGNAL时,调用LockSupport.parkNanos阻塞当前线程
                if (shouldParkAfterFailedAcquire(p, node) &&
                    nanosTimeout > spinForTimeoutThreshold)
                    // 当前线程阻塞nanosTimeout时间
                    LockSupport.parkNanos(this, nanosTimeout);
                // 如果当前线程中断标志位是true,抛出InterruptedException异常
                if (Thread.interrupted())
                    throw new InterruptedException();
            }
        } finally {
            // failed为true,表示发生异常,
            // 则将node节点的状态设置成CANCELLED,表示node节点所在线程已取消,不需要唤醒了
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     * 获取共享锁,获取失败,则会阻塞当前线程,直到获取共享锁返回
     * @param arg the acquire argument
     */
    private void doAcquireShared(int arg) {
        // 为当前线程创建共享锁节点node
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                final Node p = node.predecessor();
                // 如果节点node前一个节点是同步队列头节点。就会调用tryAcquireShared方法尝试获取共享锁
                if (p == head) {
                    int r = tryAcquireShared(arg);
                    // 如果返回值大于0,表示获取共享锁成功
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        if (interrupted)
                            selfInterrupt();
                        failed = false;
                        return;
                    }
                }
                // 如果节点p的状态是Node.SIGNAL,就是调用parkAndCheckInterrupt方法阻塞当前线程
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            // failed为true,表示发生异常,
            // 则将node节点的状态设置成CANCELLED,表示node节点所在线程已取消,不需要唤醒了
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     * Acquires in shared interruptible mode.
     * @param arg the acquire argument
     */
    private void doAcquireSharedInterruptibly(int arg)
        throws InterruptedException {
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    int r = tryAcquireShared(arg);
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return;
                    }
                }
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            // failed为true,表示发生异常,
            // 则将node节点的状态设置成CANCELLED,表示node节点所在线程已取消,不需要唤醒了
            if (failed)
                cancelAcquire(node);
        }
    }


    private boolean doAcquireSharedNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (nanosTimeout <= 0L)
            return false;
        final long deadline = System.nanoTime() + nanosTimeout;
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    int r = tryAcquireShared(arg);
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return true;
                    }
                }
                nanosTimeout = deadline - System.nanoTime();
                if (nanosTimeout <= 0L)
                    return false;
                if (shouldParkAfterFailedAcquire(p, node) &&
                    nanosTimeout > spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if (Thread.interrupted())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    // Main exported methods

    // 尝试去获取独占锁,立即返回。如果返回true表示获取锁成功。
    protected boolean tryAcquire(int arg) {
        throw new UnsupportedOperationException();
    }

    // 尝试去释放当前线程持有的独占锁,立即返回。如果返回true表示释放锁成功
    protected boolean tryRelease(int arg) {
        throw new UnsupportedOperationException();
    }

    // 尝试去获取共享锁,立即返回。返回值大于等于0,表示获取共享锁成功
    protected int tryAcquireShared(int arg) {
        throw new UnsupportedOperationException();
    }

    // 尝试去释放共享锁
    protected boolean tryReleaseShared(int arg) {
        throw new UnsupportedOperationException();
    }

    protected boolean isHeldExclusively() {
        throw new UnsupportedOperationException();
    }

    /**
     * 获取独占锁。如果没有获取到,线程就会阻塞等待,直到获取锁。不会响应中断异常
     * @param arg
     */
    public final void acquire(int arg) {
        // 1. 先调用tryAcquire方法,尝试获取独占锁,返回true,表示获取到锁,不需要执行acquireQueued方法。
        // 2. 调用acquireQueued方法,先调用addWaiter方法为当前线程创建一个节点node,并插入队列中,
        // 然后调用acquireQueued方法去获取锁,如果不成功,就会让当前线程阻塞,当锁释放时才会被唤醒。
        // acquireQueued方法返回值表示在线程等待过程中,是否有另一个线程调用该线程的interrupt方法,发起中断。
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }

//    public final void acquire(int arg) {
//        // 1.先调用tryAcquire方法,尝试获取独占锁,返回true则直接返回
//        if (tryAcquire(arg)) return;
//        // 2. 调用addWaiter方法为当前线程创建一个节点node,并插入队列中
//        Node node = addWaiter(Node.EXCLUSIVE);
//        // 调用acquireQueued方法去获取锁,
//        // acquireQueued方法返回值表示在线程等待过程中,是否有另一个线程调用该线程的interrupt方法,发起中断。
//        boolean interrupted = acquireQueued(node, arg);
//        // 如果interrupted为true,则当前线程要发起中断请求
//        if (interrupted) {
//            selfInterrupt();
//        }
//    }

    /**
     * 获取独占锁。如果没有获取到,线程就会阻塞等待,直到获取锁。如果有中断请求就会产生中断异常
     * @param arg
     */
    public final void acquireInterruptibly(int arg)
            throws InterruptedException {
        // 如果当前线程中断标志位是true,那么直接抛出中断异常
        if (Thread.interrupted())
            throw new InterruptedException();
        // 先调用tryAcquire方法,尝试获取独占锁,返回true,表示获取到锁,不需要执行doAcquireInterruptibly方法。
        if (!tryAcquire(arg))
            doAcquireInterruptibly(arg);
    }

    /**
     * 尝试在一定时间内获取独占锁。如果有中断请求就会产生中断异常。
     * 返回true,表示获取锁成功
     */
    public final boolean tryAcquireNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        // 如果当前线程中断标志位是true,那么直接抛出中断异常
        if (Thread.interrupted())
            throw new InterruptedException();
        // 先调用tryAcquire方法,尝试获取独占锁,返回true,表示获取到锁。
        // 否则调用doAcquireNanos方法获取锁
        return tryAcquire(arg) ||
            doAcquireNanos(arg, nanosTimeout);
    }

    // 在独占锁模式下,释放锁的操作
    public final boolean release(int arg) {
        // 调用tryRelease方法,尝试去释放锁,由子类具体实现
        if (tryRelease(arg)) {
            Node h = head;
            // 如果队列头节点的状态不是0,那么队列中就可能存在需要唤醒的等待节点。
            // 还记得我们在acquireQueued(final Node node, int arg)获取锁的方法中,如果节点node没有获取到锁,
            // 那么我们会将节点node的前一个节点状态设置为Node.SIGNAL,然后调用parkAndCheckInterrupt方法
            // 将节点node所在线程阻塞。
            // 在这里就是通过unparkSuccessor方法,进而调用LockSupport.unpark(s.thread)方法,唤醒被阻塞的线程
            if (h != null && h.waitStatus != 0)
                unparkSuccessor(h);
            return true;
        }
        return false;
    }

    // 获取共享锁
    public final void acquireShared(int arg) {
        // 尝试去获取共享锁,如果返回值小于0表示获取共享锁失败
        if (tryAcquireShared(arg) < 0)
            // 调用doAcquireShared方法去获取共享锁
            doAcquireShared(arg);
    }

    public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (tryAcquireShared(arg) < 0)
            doAcquireSharedInterruptibly(arg);
    }

    public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        return tryAcquireShared(arg) >= 0 ||
            doAcquireSharedNanos(arg, nanosTimeout);
    }

    // 释放共享锁
    public final boolean releaseShared(int arg) {
        // 尝试释放共享锁
        if (tryReleaseShared(arg)) {
            // 唤醒等待共享锁的线程
            doReleaseShared();
            return true;
        }
        return false;
    }

    // Queue inspection methods
    public final boolean hasQueuedThreads() {
        return head != tail;
    }
    public final boolean hasContended() {
        return head != null;
    }

    public final Thread getFirstQueuedThread() {
        // handle only fast path, else relay
        return (head == tail) ? null : fullGetFirstQueuedThread();
    }

    /**
     * Version of getFirstQueuedThread called when fastpath fails
     */
    private Thread fullGetFirstQueuedThread() {
        Node h, s;
        Thread st;
        if (((h = head) != null && (s = h.next) != null &&
             s.prev == head && (st = s.thread) != null) ||
            ((h = head) != null && (s = h.next) != null &&
             s.prev == head && (st = s.thread) != null))
            return st;

        Node t = tail;
        Thread firstThread = null;
        while (t != null && t != head) {
            Thread tt = t.thread;
            if (tt != null)
                firstThread = tt;
            t = t.prev;
        }
        return firstThread;
    }

    public final boolean isQueued(Thread thread) {
        if (thread == null)
            throw new NullPointerException();
        for (Node p = tail; p != null; p = p.prev)
            if (p.thread == thread)
                return true;
        return false;
    }

    final boolean apparentlyFirstQueuedIsExclusive() {
        Node h, s;
        return (h = head) != null &&
            (s = h.next)  != null &&
            !s.isShared()         &&
            s.thread != null;
    }

    // 返回false表示队列为null,或者当前线程节点在是队列头的下一个节点。
    // 返回true表示有一个线程节点在当前线程节点之前
    public final boolean hasQueuedPredecessors() {
        Node t = tail;
        Node h = head;
        Node s;
        return h != t &&
            ((s = h.next) == null || s.thread != Thread.currentThread());
    }


    // Instrumentation and monitoring methods

    public final int getQueueLength() {
        int n = 0;
        for (Node p = tail; p != null; p = p.prev) {
            if (p.thread != null)
                ++n;
        }
        return n;
    }

    public final Collection<Thread> getQueuedThreads() {
        ArrayList<Thread> list = new ArrayList<Thread>();
        for (Node p = tail; p != null; p = p.prev) {
            Thread t = p.thread;
            if (t != null)
                list.add(t);
        }
        return list;
    }

    public final Collection<Thread> getExclusiveQueuedThreads() {
        ArrayList<Thread> list = new ArrayList<Thread>();
        for (Node p = tail; p != null; p = p.prev) {
            if (!p.isShared()) {
                Thread t = p.thread;
                if (t != null)
                    list.add(t);
            }
        }
        return list;
    }

    public final Collection<Thread> getSharedQueuedThreads() {
        ArrayList<Thread> list = new ArrayList<Thread>();
        for (Node p = tail; p != null; p = p.prev) {
            if (p.isShared()) {
                Thread t = p.thread;
                if (t != null)
                    list.add(t);
            }
        }
        return list;
    }

    public String toString() {
        int s = getState();
        String q  = hasQueuedThreads() ? "non" : "";
        return super.toString() +
            "[State = " + s + ", " + q + "empty queue]";
    }


    // Internal support methods for Conditions

    // 节点node是不是在CLH队列中
    final boolean isOnSyncQueue(Node node) {
        // 如果node的状态是Node.CONDITION,或者node没有前一个节点prev,
        // 那么返回false,节点node不在同步队列中
        if (node.waitStatus == Node.CONDITION || node.prev == null)
            return false;
        // 如果node有下一个节点next,那么它一定在同步队列中
        if (node.next != null) // If has successor, it must be on queue
            return true;
        // 从同步队列中查找节点node
        return findNodeFromTail(node);
    }

    // 在同步队列中从后向前查找节点node,如果找到返回true,否则返回false
    private boolean findNodeFromTail(Node node) {
        Node t = tail;
        for (;;) {
            if (t == node)
                return true;
            if (t == null)
                return false;
            t = t.prev;
        }
    }

    // 返回true表示节点node插入到同步队列中,返回false表示节点node没有插入到同步队列中
    final boolean transferForSignal(Node node) {
        // 如果节点node的状态不是Node.CONDITION,或者更新状态失败,
        // 说明该node节点已经插入到同步队列中,所以直接返回false
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;

        // 将节点node插入到同步队列中,p是原先同步队列尾节点,也是node节点的前一个节点
        Node p = enq(node);
        int ws = p.waitStatus;
        // 如果前一个节点是已取消状态,或者不能将它设置成Node.SIGNAL状态。
        // 就说明节点p之后也不会发起唤醒下一个node节点线程的操作,
        // 所以这里直接调用 LockSupport.unpark(node.thread)方法,唤醒节点node所在线程
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        return true;
    }

    // 等待节点node存放到同步队列中,如果是当前线程插入的,返回true,如果是另一个线程插入的,返回false。
    final boolean transferAfterCancelledWait(Node node) {
        // 当节点node状态还是Node.CONDITION,改变它的状态是0,然后插入到同步队列中。
        if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
            enq(node);
            return true;
        }
        // 如果节点node不在同步队列中,当前线程让出执行权
        while (!isOnSyncQueue(node))
            Thread.yield();
        return false;
    }

    /**
     * 释放当前线程占有的锁,并唤醒CLH队列一个等待线程
     * 如果失败就抛出异常,设置node节点的状态是Node.CANCELLED
     * @return
     */
    final int fullyRelease(Node node) {
        boolean failed = true;
        try {
            int savedState = getState();
            // 释放当前线程占有的锁
            if (release(savedState)) {
                failed = false;
                return savedState;
            } else {
                throw new IllegalMonitorStateException();
            }
        } finally {
            if (failed)
                node.waitStatus = Node.CANCELLED;
        }
    }

    // Instrumentation methods for conditions

    public final boolean owns(ConditionObject condition) {
        return condition.isOwnedBy(this);
    }

    public final boolean hasWaiters(ConditionObject condition) {
        if (!owns(condition))
            throw new IllegalArgumentException("Not owner");
        return condition.hasWaiters();
    }

    public final int getWaitQueueLength(ConditionObject condition) {
        if (!owns(condition))
            throw new IllegalArgumentException("Not owner");
        return condition.getWaitQueueLength();
    }

    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
        if (!owns(condition))
            throw new IllegalArgumentException("Not owner");
        return condition.getWaitingThreads();
    }

    public class ConditionObject implements Condition, java.io.Serializable {
        private static final long serialVersionUID = 1173984872572414699L;
        /** Condition队列头. */
        private transient Node firstWaiter;
        /** Condition队列尾. */
        private transient Node lastWaiter;

        /**
         * Creates a new {@code ConditionObject} instance.
         */
        public ConditionObject() { }

        // Internal methods

        /**
         * 为当前线程创建新的Node节点,并且将这个节点插入到Condition队列中了
         * @return its new wait node
         */
        private Node addConditionWaiter() {
            Node t = lastWaiter;
            // 如果Condition队列尾节点的状态不是Node.CONDITION
            if (t != null && t.waitStatus != Node.CONDITION) {
                // 清除Condition队列中,状态不是Node.CONDITION的节点,
                // 并且可能会重新设置firstWaiter和lastWaiter
                unlinkCancelledWaiters();
                // 重新将Condition队列尾赋值给t
                t = lastWaiter;
            }
            // 为当前线程创建一个状态为Node.CONDITION的节点
            Node node = new Node(Thread.currentThread(), Node.CONDITION);
            // 如果t为null,表示Condition队列为空,将node节点赋值给链表头
            if (t == null)
                firstWaiter = node;
            else
                // 将新节点node插入到Condition队列尾
                t.nextWaiter = node;
            // 将新节点node设置为新的Condition队列尾
            lastWaiter = node;
            return node;
        }

        // 将Condition队列中的first节点插入到CLH队列中
        private void doSignal(Node first) {
            do {
                // 原先的Condition队列头节点取消,所以重新赋值Condition队列头节点
                // 如果新的Condition队列头节点为null,表示Condition队列为空了
                // ,所以也要设置Condition队列尾lastWaiter为null
                if ( (firstWaiter = first.nextWaiter) == null)
                    lastWaiter = null;
                // 取消first节点nextWaiter引用
                first.nextWaiter = null;
            } while (!transferForSignal(first) &&
                     (first = firstWaiter) != null);
        }

        /**
         * 将condition队列中所有的节点都插入到同步队列中
         * @param first condition队列头节点
         */
        private void doSignalAll(Node first) {
            // 表示将condition队列设置为空
            lastWaiter = firstWaiter = null;
            do {
                // 得到condition队列的下一个节点
                Node next = first.nextWaiter;
                first.nextWaiter = null;
                // 将节点first插入到同步队列中
                transferForSignal(first);
                first = next;
                // 循环遍历condition队列中所有的节点
            } while (first != null);
        }

        // 清除Condition队列中状态不是Node.CONDITION的节点
        private void unlinkCancelledWaiters() {
            // condition队列头赋值给t
            Node t = firstWaiter;
            // 这个trail节点,只是起辅助作用
            Node trail = null;
            while (t != null) {
                //得到下一个节点next。当节点是condition时候,nextWaiter表示condition队列的下一个节点
                Node next = t.nextWaiter;
                // 如果节点t的状态不是CONDITION,那么该节点就要从condition队列中移除
                if (t.waitStatus != Node.CONDITION) {
                    // 将节点t的nextWaiter设置为null
                    t.nextWaiter = null;
                    // 如果trail为null,表示原先的condition队列头节点实效,需要设置新的condition队列头
                    if (trail == null)
                        firstWaiter = next;
                    else
                        // 将节点t从condition队列中移除,因为改变了引用的指向,从condition队列中已经找不到节点t了
                        trail.nextWaiter = next;
                    // 如果next为null,表示原先的condition队列尾节点也实效,重新设置队列尾节点
                    if (next == null)
                        lastWaiter = trail;
                }
                else
                    // 遍历到的有效节点
                    trail = t;
                // 将next赋值给t,遍历完整个condition队列
                t = next;
            }
        }

        // public methods

        // 如果condition队列不为空,将condition队列头节点插入到同步队列中
        public final void signal() {
            // 如果当前线程不是独占锁线程,就抛出IllegalMonitorStateException异常
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();

            // 将Condition队列头赋值给节点first
            Node first = firstWaiter;
            if (first != null)
                //  将Condition队列中的first节点插入到CLH队列中
                doSignal(first);
        }

        // 将condition队列中所有的节点都插入到同步队列中
        public final void signalAll() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                doSignalAll(first);
        }

        // 让当前持有锁的线程阻塞等待,并释放锁。如果线程等待期间发出中断请求,不会产生中断异常
        public final void awaitUninterruptibly() {
            // 为当前线程创建新的Node节点,并且将这个节点插入到Condition队列中了
            Node node = addConditionWaiter();
            // 释放当前线程占有的锁,并唤醒其他线程
            int savedState = fullyRelease(node);
            boolean interrupted = false;
            // 如果节点node不在同步队列中(注意不是Condition队列),就阻塞当前线程
            while (!isOnSyncQueue(node)) {
                // 阻塞当前线程
                LockSupport.park(this);
                if (Thread.interrupted())
                    interrupted = true;
            }
            // 如果节点node已经在同步队列中了,获取同步锁,只有得到锁才能继续执行,否则线程继续阻塞等待
            if (acquireQueued(node, savedState) || interrupted)
                selfInterrupt();
        }

        /** Mode meaning to reinterrupt on exit from wait */
        private static final int REINTERRUPT =  1;
        /** Mode meaning to throw InterruptedException on exit from wait */
        private static final int THROW_IE    = -1;

        /**
         * 如果线程没有发起了中断请求,返回0.
         * 如果线程发起了中断请求,且中断请求在signalled(即调用signal或signalAll)之前返回THROW_IE
         * 中断请求在signalled之后返回REINTERRUPT
         */
        private int checkInterruptWhileWaiting(Node node) {
            return Thread.interrupted() ?
                (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
                0;
        }

        /**
         * 如果interruptMode是THROW_IE,就抛出InterruptedException异常
         * 如果interruptMode是REINTERRUPT,则当前线程再发出中断请求
         * 否则就什么都不做
         */
        private void reportInterruptAfterWait(int interruptMode)
            throws InterruptedException {
            if (interruptMode == THROW_IE)
                throw new InterruptedException();
            else if (interruptMode == REINTERRUPT)
                selfInterrupt();
        }

        /**
         * 让当前持有锁的线程阻塞等待,并释放锁。如果有中断请求,则抛出InterruptedException异常
         * @throws InterruptedException
         */
        public final void await() throws InterruptedException {
            // 如果当前线程中断标志位是true,就抛出InterruptedException异常
            if (Thread.interrupted())
                throw new InterruptedException();
            // 为当前线程创建新的Node节点,并且将这个节点插入到Condition队列中了
            Node node = addConditionWaiter();
            // 释放当前线程占有的锁,并唤醒CLH队列一个等待线程
            int savedState = fullyRelease(node);
            int interruptMode = 0;
            // 如果节点node不在同步队列中(注意不是Condition队列)
            while (!isOnSyncQueue(node)) {
                // 阻塞当前线程,那么怎么唤醒这个线程呢?
                // 首先我们必须调用signal或者signalAll将这个节点node加入到同步队列。
                // 只有这样unparkSuccessor(Node node)方法,才有可能唤醒被阻塞的线程
                LockSupport.park(this);
                // 如果当前线程产生中断请求,就跳出循环
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
            // 如果节点node已经在同步队列中了,获取同步锁,只有得到锁才能继续执行,否则线程继续阻塞等待
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            // 清除Condition队列中状态不是Node.CONDITION的节点
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            // 是否要抛出异常,或者发出中断请求
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
        }

        public final long awaitNanos(long nanosTimeout)
                throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            final long deadline = System.nanoTime() + nanosTimeout;
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                if (nanosTimeout <= 0L) {
                    transferAfterCancelledWait(node);
                    break;
                }
                if (nanosTimeout >= spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
                nanosTimeout = deadline - System.nanoTime();
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
            return deadline - System.nanoTime();
        }

        public final boolean awaitUntil(Date deadline)
                throws InterruptedException {
            long abstime = deadline.getTime();
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            boolean timedout = false;
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                if (System.currentTimeMillis() > abstime) {
                    timedout = transferAfterCancelledWait(node);
                    break;
                }
                LockSupport.parkUntil(this, abstime);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
            return !timedout;
        }

        public final boolean await(long time, TimeUnit unit)
                throws InterruptedException {
            long nanosTimeout = unit.toNanos(time);
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            final long deadline = System.nanoTime() + nanosTimeout;
            boolean timedout = false;
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                if (nanosTimeout <= 0L) {
                    timedout = transferAfterCancelledWait(node);
                    break;
                }
                if (nanosTimeout >= spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
                nanosTimeout = deadline - System.nanoTime();
            }
            // 获取
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            // 清除Condition队列中状态不是Node.CONDITION的节点
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            // 是否要抛出异常,或者发出中断请求
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
            return !timedout;
        }

        /**
         * 返回true:表示这个condition对象是由sync对象创建的。
         */
        final boolean isOwnedBy(AbstractQueuedSynchronizer sync) {
            return sync == AbstractQueuedSynchronizer.this;
        }

        /**
         * 这个condition对象上是否有等待线程,即condition队列不为空,且有一个节点的状态是 Node.CONDITION
         */
        protected final boolean hasWaiters() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            // 遍历condition队列
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
                if (w.waitStatus == Node.CONDITION)
                    return true;
            }
            return false;
        }

        /**
         * 返回condition对象上等待线程的个数,即遍历condition队列,计算节点的状态是Node.CONDITION的个数
         */
        protected final int getWaitQueueLength() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            int n = 0;
            // 遍历condition队列
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
                // 计算节点的状态是Node.CONDITION的个数
                if (w.waitStatus == Node.CONDITION)
                    ++n;
            }
            return n;
        }

        /**
         * 返回condition对象上等待线程的集合。
         */
        protected final Collection<Thread> getWaitingThreads() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            ArrayList<Thread> list = new ArrayList<Thread>();
            // 遍历condition队列
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
                // 当节点的状态是Node.CONDITION,将节点的线程添加到list集合中
                if (w.waitStatus == Node.CONDITION) {
                    Thread t = w.thread;
                    if (t != null)
                        list.add(t);
                }
            }
            return list;
        }
    }

    // 支持compareAndSet操作
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long stateOffset;
    private static final long headOffset;
    private static final long tailOffset;
    private static final long waitStatusOffset;
    private static final long nextOffset;

    static {
        try {
            stateOffset = unsafe.objectFieldOffset
                (AbstractQueuedSynchronizer.class.getDeclaredField("state"));
            headOffset = unsafe.objectFieldOffset
                (AbstractQueuedSynchronizer.class.getDeclaredField("head"));
            tailOffset = unsafe.objectFieldOffset
                (AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
            waitStatusOffset = unsafe.objectFieldOffset
                (Node.class.getDeclaredField("waitStatus"));
            nextOffset = unsafe.objectFieldOffset
                (Node.class.getDeclaredField("next"));

        } catch (Exception ex) { throw new Error(ex); }
    }

    /**
     * 通过CAS函数设置head值,仅仅在enq方法中调用
     */
    private final boolean compareAndSetHead(Node update) {
        return unsafe.compareAndSwapObject(this, headOffset, null, update);
    }

    /**
     * 通过CAS函数设置tail值,仅仅在enq方法中调用
     */
    private final boolean compareAndSetTail(Node expect, Node update) {
        return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
    }

    /**
     * 通过CAS函数设置node对象的waitStatus值
     */
    private static final boolean compareAndSetWaitStatus(Node node,
                                                         int expect,
                                                         int update) {
        return unsafe.compareAndSwapInt(node, waitStatusOffset,
                                        expect, update);
    }

    /**
     * 通过CAS函数设置node对象的next值
     */
    private static final boolean compareAndSetNext(Node node,
                                                   Node expect,
                                                   Node update) {
        return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
    }
}


 

对很少部分做整理,自己梳理一遍代码思路更加清晰,膜拜原作者!侵删!

本文参考:https://www.jianshu.com/p/0da2939391cf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: u-boot是一个开源的引导加载程序,用于嵌入式系统的启动。它通常嵌入在芯片的ROM或闪存中,是系统的第一个执行程序,负责初始化硬件、加载操作系统和其他应用程序。 u-boot的源码是以C语言编写的,具有高度可移植性。它提供了一系列的驱动程序和命令行工具,可以在开发板上进行硬件初始化和测试。 源码的结构分为几个重要的部分:启动代码、中断向量表、初始化代码以及其他功能模块。启动代码是u-boot执行的入口点,在这个阶段,它会初始化一些必要的硬件设备,例如串口、存储器等,同时也会设置中断向量表。 中断向量表是一个由硬件中断信号触发的函数指针数组,u-boot将中断信号与相应的函数关联起来,以便在发生中断时进行相应的处理。 初始化代码是u-boot执行的核心部分,它会通过配置文件或环境变量来读取系统设置,并进行相应的初始化。例如,它会加载并运行操作系统内核镜像,设置内存映射表,配置设备和网络接口等。 此外,u-boot还提供了一些功能模块,例如命令行解析器、文件系统支持、网络协议栈等。这些功能模块可以通过命令行进行操作,以便用户对嵌入式系统进行配置、调试和测试。 对于研究和分析u-boot源码,可以从以下几个方面入手: 1. 启动流程:了解u-boot是如何从ROM或闪存中加载到内存并执行的,包括启动代码和中断向量表的设置。 2. 硬件初始化:了解u-boot是如何初始化硬件设备的,包括串口、存储器、网络接口等。 3. 配置文件和环境变量:了解u-boot是如何通过配置文件或环境变量来读取系统设置的,以及如何进行相应的初始化。 4. 功能模块:了解u-boot提供的功能模块,例如命令行解析器、文件系统支持、网络协议栈等,以及它们的实现方式和使用方法。 通过对u-boot源码详细分析,可以深入了解嵌入式系统的引导过程、硬件初始化和驱动程序的编写,从而提高嵌入式系统的开发和调试能力。 ### 回答2: Uboot是一种开源的引导加载程序,用于嵌入式系统的启动。它是一个简单而灵活的软件,可以在各种硬件平台上使用,并提供了许多功能和驱动程序。 首先,Uboot的主要功能是加载和运行操作系统。它通过读取存储介质上的引导扇区,将操作系统加载到内存中并启动。此外,Uboot还提供了命令行界面,用户可以在启动过程中进行配置和控制。 Uboot的源代码由若干模块组成,包括引导代码、设备驱动程序、命令行解析器等。其中,引导代码是最关键的部分,负责在硬件启动时初始化系统和设备,并在引导过程中进行加载和启动操作系统。设备驱动程序用于访问硬件设备,例如存储介质、串口等。命令行解析器则负责解析用户输入的命令,并执行相应的操作。 在Uboot的源代码中,可以找到各种初始化和设置函数,以及与硬件平台相关的代码。这些代码通常是与硬件设备的寄存器交互,进行硬件初始化和配置。此外,还有一些与引导过程和加载操作系统相关的代码,用于读取、解析和加载引导扇区以及操作系统镜像。 总的来说,Uboot的源码详细分析涉及到引导代码、设备驱动程序和命令行解析器等多个模块。在分析过程中,需要理解硬件平台的相关知识和操作系统的启动流程,并深入了解Uboot的代码实现和功能。只有这样,才能对Uboot的源码有一个全面的理解,并能根据需求进行相应的修改和定制。 ### 回答3: U-Boot是一款开源的引导加载程序,用于嵌入式系统中启动操作系统。它是最常用的引导加载程序之一,具有广泛的应用。下面,我将对U-Boot源码进行详细分析。 U-Boot源码位于一个git仓库中,可以通过clone仓库获取源码源码的结构清晰,主要分为三个部分:板级支持包(board support package,BSP),引导和命令。 BSP包含了与硬件相关的代码和配置文件,用于支持不同的硬件平台。其中,包括设备初始化、设备驱动程序和硬件设置等。这些代码主要包括处理器启动代码、时钟初始化、内存初始化以及设备和外设的配置等。 引导部分是U-Boot的核心,其中包括引导过程的各个阶段。首先,它加载引导扇区和主引导程序,其中包括引导加载器。引导加载器根据设备的启动模式选择适当的引导方式。然后,它会加载内核映像和根文件系统,并将控制权转移到内核。 最后,命令部分包含了一系列的命令,用于与用户进行交互。这些命令可以用于启动操作系统、进行系统设置和调试等。U-Boot提供了丰富的命令集,包括boot、setenv、saveenv、printenv等等。 在分析U-Boot源码时,需要了解硬件平台的特性和配置文件。可以根据目标硬件平台的手册和数据手册,对源码进行逐步分析和调试。在分析过程中,可以使用调试工具进行跟踪、断点和单步调试,以便更好地理解源码的执行过程。 总的来说,U-Boot源码详细分析需要涉及到硬件平台的特性和配置文件,并对引导加载过程和命令解析进行深入研究。通过对U-Boot源码的理解和分析,可以为嵌入式系统的启动和操作提供更好的支持和定制化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值