publicabstractclassAbstractQueuedSynchronizerextendsAbstractOwnableSynchronizerimplementsjava.io.Serializable { ... staticfinalclassNode { /** Marker to indicate a node is waiting in shared mode */ staticfinalNodeSHARED=newNode(); /** Marker to indicate a node is waiting in exclusive mode */ staticfinalNodeEXCLUSIVE=null; /** waitStatus value to indicate thread has cancelled */ staticfinalintCANCELLED=1; /** waitStatus value to indicate successor's thread needs unparking */ staticfinalintSIGNAL= -1; /** waitStatus value to indicate thread is waiting on condition */ staticfinalintCONDITION= -2; /** * waitStatus value to indicate the next acquireShared should * unconditionally propagate */ staticfinalintPROPAGATE= -3; volatileint waitStatus; volatile Node prev; volatile Node next; volatile Thread thread; Node nextWaiter; ... } publicclassConditionObjectimplementsCondition, java.io.Serializable { /** First node of condition queue. */ privatetransient Node firstWaiter; /** Last node of condition queue. */ privatetransient Node lastWaiter; ... } ... }
//Sync实现,检测是否是当前线程获得了锁 protectedfinalbooleanisHeldExclusively() { // While we must in general read state before owner, // we don't need to do so to check if current thread is owner return getExclusiveOwnerThread() == Thread.currentThread(); }