抽象同步队列 AQS三部曲 [ 一 ]

 

AbstractQueuedSynchronizer 抽象同步队列简称 AQS,

它是实现同步器的 基 础组件, 并发包中锁的底层就是使用 AQS 实现的 。

AQS是一个FIFO的双向队列,其内部通过节点head和tail记录队 首和队尾元素,队列元素的类型为 Node。

其中 Node 中的 thread变量用来存放进入 AQS 队列里面的线程: Node 节点内部的 SHARED 用来标记该线程是获取共 享 资源时被阻 塞 挂起后放入 AQS 队列的, EXCLUSIVE 用来标记线程是 获 取独占资源时被挂起后放入 AQS 队列的 ;

waitStatus 记录当前线程等待状态:

CANCELLED (线程被取消了)、

SIGNAL (线程需要被唤醒)、

CONDITION (线程在条件队列里面等待〉、

PROPAGATE (释 放共享资源时需要通知其他节点〕;

prev 记录当前节点的前驱节点, next 记录当前节点的 后继节点 。

 

在独占方式下获取和释放资源使用的 方法为 :

void acquire(int arg)

void acquirelnterruptibly(int arg)

boolean release(int arg)。

 

在共享方式下获取和释放资源的方法为:

void acquireShared(int arg)

void acquireSharedinterruptibly(int arg)

boolean releaseShared(int arg)。

 

 

使用独占方式获取的资源是与具体线程绑定的,就是说如果 一 个 线程获取到了资源, 就会标记是这个线程获取到了,其他线程再尝试操作 state 获取资源时会发现当前该资源 不是自己持有的,就会在获取失败后被阻塞 。

 

队列中的实体 Node : 

 

 Node 这是一个双向队列

 

 

 

 

 

 

 

static final class Node {
        //todo 模式,分为共享与独占


        // todo  共享模式
        /** Marker to indicate a node is waiting in shared mode */
        static final Node SHARED = new Node();

        // todo  独占模式
        /** Marker to indicate a node is waiting in exclusive mode */
        static final Node EXCLUSIVE = null;


        // todo  结点状态
        // todo  CANCELLED,值为1,表示当前的线程被取消
        // todo  SIGNAL,值为-1,表示当前节点的后继节点包含的线程需要运行,也就是unpark
        // todo  CONDITION,值为-2,表示当前节点在等待 condition ,也就是在 condition 队列中
        // todo  PROPAGATE,值为-3,表示当前场景下后续的 acquireShared 能够得以执行
        // todo  值为0,表示当前节点在sync队列中,等待着获取锁


        /** 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).
         */

        // todo  结点状态
        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.
         */

        // todo  前驱结点
        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.
         */
        // todo  后继结点
        volatile Node next;

        /**
         * The thread that enqueued this node.  Initialized on
         * construction and nulled out after use.
         */
        // todo  结点所对应的线程
        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.
         */
        // todo  下一个等待者
        Node nextWaiter;

        /**
         * Returns true if node is waiting in shared mode.
         */
        // todo  结点是否在共享模式下等待
        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
         */

        // todo  获取前驱结点,若前驱结点为空,抛出异常
        final Node predecessor() throws NullPointerException {
            // todo  保存前驱结点
            Node p = prev;
            // todo  前驱结点为空,抛出异常
            if (p == null)
                throw new NullPointerException();
            else  // todo  前驱结点不为空,返回
                return p;
        }

        // todo  无参构造函数
        Node() {    // Used to establish initial head or SHARED marker
        }

        // todo  构造函数
        Node(Thread thread, Node mode) {     // Used by addWaiter
            this.nextWaiter = mode;
            this.thread = thread;
        }

        // todo  构造函数
        Node(Thread thread, int waitStatus) { // Used by Condition
            this.waitStatus = waitStatus;
            this.thread = thread;
        }
    }

 

另外一个 队列 ConditionObject  :

这是一个单向队列

        // todo  condition队列的头结点
        /** First node of condition queue. */
        private transient Node firstWaiter;

        // todo  condition队列的尾结点
        /** Last node of condition queue. */
        private transient Node lastWaiter;

 

 

 

 

 



 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AQS(AbstractQueuedSynchronizer)是Java中实现同步器的框架,它提供了一种基于FIFO队列的阻塞和唤醒机制。AQS的阻塞队列原理是通过CLH(Craig, Landin, and Hagersten)队列来实现的。 CLH队列是一种虚拟的双向链表,它仅存在节点之间的关联关系,而不存在队列的实例。每个请求共享资源的线程都会被封装成一个CLH队列的节点(Node)。当线程请求共享资源时,它会被添加到CLH队列的尾部,并进入阻塞状态。 当共享资源被占用时,其他线程请求该资源的线程会被放入CLH队列的末尾,即排队等待。这种排队等待的方式可以保证请求资源的线程按照FIFO的顺序获得资源,避免了饥饿现象。当资源释放后,AQS会自动唤醒队列中的下一个线程,使其获得资源并继续执行。 需要注意的是,AQS同步队列(Sync queue)是一个双向链表,包括头节点(head)和尾节点(tail),用于后续的调度。而条件队列(Condition queue)是一个单向链表,只有在使用Condition时才会存在,并且可能会有多个条件队列。 总结一下,AQS实现阻塞队列的原理是通过CLH队列来实现的,当共享资源被占用时,请求资源的线程会被添加到CLH队列中排队等待。当资源释放后,AQS会自动唤醒队列中的下一个线程,使其获得资源并继续执行。同步队列用于后续的调度,而条件队列只在使用Condition时才会存在。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值