重入鎖Condition原始碼分析
阿新 • • 發佈:2020-11-17
首先需要明確的是,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,沒有前驅,因為沒必要