编程语言
首页 > 编程语言> > 重入锁Condition源码分析

重入锁Condition源码分析

作者:互联网

  首先需要明确的是,Condition只工作在排他锁,一个排他锁可以有多个Condition,不过Condition的代码其实是在AQS里的

public class ConditionObject implements Condition, java.io.Serializable {
        private static final long serialVersionUID = 1173984872572414699L;
        /** First node of condition queue. */
        private transient Node firstWaiter;
        /** Last node of condition queue. */
        private transient Node lastWaiter;

  顺便复习一下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;

        volatile int waitStatus;

        volatile Node prev;

        volatile Node next;

        volatile Thread thread;

        Node nextWaiter;

  上面需要注意的是在同步队列中的时候,是有前驱和后继的prev,next,但是在条件队列中只有nextWaiter,没有前驱,因为没必要

 

标签:重入,Node,waitStatus,indicate,源码,static,final,Condition
来源: https://www.cnblogs.com/juniorMa/p/13986341.html