/**
* The synchronization state.
*/privatevolatileint state;/**
* Returns the current value of synchronization state.
* This operation has memory semantics of a {@code volatile} read.
* @return current state value
*/protectedfinalintgetState(){return state;}/**
* Sets the value of synchronization state.
* This operation has memory semantics of a {@code volatile} write.
* @param newState the new state value
*/protectedfinalvoidsetState(int newState){
state = newState;}/**
* Atomically sets synchronization state to the given updated
* value if the current state value equals the expected value.
* This operation has memory semantics of a {@code volatile} read
* and write.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that the actual
* value was not equal to the expected value.
*/// 核心方法,通过cas去改变State的值!protectedfinalbooleancompareAndSetState(int expect,int update){// See below for intrinsics setup to support thisreturn unsafe.compareAndSwapInt(this, stateOffset, expect, update);}
AQS的资源共享方式
/** Marker to indicate a node is waiting in shared mode */staticfinalNode SHARED =newNode();/** Marker to indicate a node is waiting in exclusive mode */staticfinalNode EXCLUSIVE =null;
Exclusive-独占,只有一个线程能执行,如ReentrantLock。
Share-共享,多个线程可以同时执行,如Semaphore/CountDownLatch。
AQS的节点状态
staticfinalint CANCELLED =1;staticfinalint SIGNAL =-1;staticfinalint CONDITION =-2;staticfinalint PROPAGATE =-3;