Java并发编程的艺术锁核心类AQS详解
带着BAT大厂的面试问题去理解
请带着这些问题继续后文,会很大程度上帮助你更好地理解相关知识点。什么是AQS?为什么它是核心?AQS的核心思想是什么?它是怎么实现的?底层数据结构等AQS有哪些核心的方法?AQS定义什么样的资源获取方式?AQS定义了两种资源获取方式:独占(只有一个线程能访问执行,又根据是否按队列的顺序分为公平锁和非公平锁,如ReentrantLock)和共享(多个线程可同时访问执行,如Semaphore、CountDownLatch、CyclicBarrier)。ReentrantReadWriteLock可以看成是组合式,允许多个线程同时对某一资源进行读。AQS底层使用了什么样的设计模式?模板AQS的应用示例?AbstractQueuedSynchronizer简介
AQS是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Semaphore,其他的诸如ReentrantReadWriteLock,SynchronousQueue,FutureTask等等皆是基于AQS的。当然,我们自己也能利用AQS非常轻松容易地构造出符合我们自己需求的同步器。AQS核心思想
AQS核心思想是,如果被请求的共享资源空闲,则将当前请求资源的线程设置为有效的工作线程,并且将共享资源设置为锁定状态。如果被请求的共享资源被占用,那么就需要一套线程阻塞等待以及被唤醒时锁分配的机制,这个机制AQS是用CLH队列锁实现的,即将暂时获取不到锁的线程加入到队列中。
CLH(Craig,Landin,andHagersten)队列是一个虚拟的双向队列(虚拟的双向队列即不存在队列实例,仅存在结点之间的关联关系)。AQS是将每条请求共享资源的线程封装成一个CLH锁队列的一个结点(Node)来实现锁的分配。
AQS使用一个int成员变量来表示同步状态,通过内置的FIFO队列来完成获取资源线程的排队工作。AQS使用CAS对该同步状态进行原子操作实现对其值的修改。privatevolatileintstate;共享变量,使用volatile修饰保证线程可见性
状态信息通过procted类型的getState,setState,compareAndSetState进行操作返回同步状态的当前值protectedfinalintgetState(){returnstate;}设置同步状态的值protectedfinalvoidsetState(intnewState){statenewState;}原子地(CAS操作)将同步状态值设置为给定值update如果当前同步状态的值等于expect(期望值)protectedfinalbooleancompareAndSetState(intexpect,intupdate){returnunsafe。compareAndSwapInt(this,stateOffset,expect,update);}AQS对资源的共享方式
AQS定义两种资源共享方式Exclusive(独占):只有一个线程能执行,如ReentrantLock。又可分为公平锁和非公平锁:公平锁:按照线程在队列中的排队顺序,先到者先拿到锁非公平锁:当线程要获取锁时,无视队列顺序直接去抢锁,谁抢到就是谁的Share(共享):多个线程可同时执行,如SemaphoreCountDownLatch。Semaphore、CountDownLatCh、CyclicBarrier、ReadWriteLock我们都会在后面讲到。
ReentrantReadWriteLock可以看成是组合式,因为ReentrantReadWriteLock也就是读写锁允许多个线程同时对某一资源进行读。
不同的自定义同步器争用共享资源的方式也不同。自定义同步器在实现时只需要实现共享资源state的获取与释放方式即可,至于具体线程等待队列的维护(如获取资源失败入队唤醒出队等),AQS已经在上层已经帮我们实现好了。AQS底层使用了模板方法模式
同步器的设计是基于模板方法模式的,如果需要自定义同步器一般的方式是这样(模板方法模式很经典的一个应用):
使用者继承AbstractQueuedSynchronizer并重写指定的方法。(这些重写方法很简单,无非是对于共享资源state的获取和释放)将AQS组合在自定义同步组件的实现中,并调用其模板方法,而这些模板方法会调用使用者重写的方法。
这和我们以往通过实现接口的方式有很大区别,模板方法模式请参看:设计模式行为型模板方法(TemplateMethod)详解
AQS使用了模板方法模式,自定义同步器时需要重写下面几个AQS提供的模板方法:isHeldExclusively()该线程是否正在独占资源。只有用到condition才需要去实现它。tryAcquire(int)独占方式。尝试获取资源,成功则返回true,失败则返回false。tryRelease(int)独占方式。尝试释放资源,成功则返回true,失败则返回false。tryAcquireShared(int)共享方式。尝试获取资源。负数表示失败;0表示成功,但没有剩余可用资源;正数表示成功,且有剩余资源。tryReleaseShared(int)共享方式。尝试释放资源,成功则返回true,失败则返回false。
默认情况下,每个方法都抛出UnsupportedOperationException。这些方法的实现必须是内部线程安全的,并且通常应该简短而不是阻塞。AQS类中的其他方法都是final,所以无法被其他类使用,只有这几个方法可以被其他类使用。
以ReentrantLock为例,state初始化为0,表示未锁定状态。A线程lock()时,会调用tryAcquire()独占该锁并将state1。此后,其他线程再tryAcquire()时就会失败,直到A线程unlock()到state0(即释放锁)为止,其它线程才有机会获取该锁。当然,释放锁之前,A线程自己是可以重复获取此锁的(state会累加),这就是可重入的概念。但要注意,获取多少次就要释放多么次,这样才能保证state是能回到零态的。AbstractQueuedSynchronizer数据结构
AbstractQueuedSynchronizer类底层的数据结构是使用CLH(Craig,Landin,andHagersten)队列是一个虚拟的双向队列(虚拟的双向队列即不存在队列实例,仅存在结点之间的关联关系)。AQS是将每条请求共享资源的线程封装成一个CLH锁队列的一个结点(Node)来实现锁的分配。其中Syncqueue,即同步队列,是双向链表,包括head结点和tail结点,head结点主要用作后续的调度。而Conditionqueue不是必须的,其是一个单向链表,只有当使用Condition时,才会存在此单向链表。并且可能会有多个Conditionqueue。
AbstractQueuedSynchronizer源码分析类的继承关系
AbstractQueuedSynchronizer继承自AbstractOwnableSynchronizer抽象类,并且实现了Serializable接口,可以进行序列化。publicabstractclassAbstractQueuedSynchronizerextendsAbstractOwnableSynchronizerimplementsjava。io。Serializable
其中AbstractOwnableSynchronizer抽象类的源码如下:publicabstractclassAbstractOwnableSynchronizerimplementsjava。io。Serializable{版本序列号privatestaticfinallongserialVersionUID3737899427754241961L;构造方法protectedAbstractOwnableSynchronizer(){}独占模式下的线程privatetransientThreadexclusiveOwnerThread;设置独占线程protectedfinalvoidsetExclusiveOwnerThread(Threadthread){exclusiveOwnerThreadthread;}获取独占线程protectedfinalThreadgetExclusiveOwnerThread(){returnexclusiveOwnerThread;}}
AbstractOwnableSynchronizer抽象类中,可以设置独占资源线程和获取独占资源线程。分别为setExclusiveOwnerThread与getExclusiveOwnerThread方法,这两个方法会被子类调用。
AbstractQueuedSynchronizer类有两个内部类,分别为Node类与ConditionObject类。下面分别做介绍。类的内部类Node类staticfinalclassNode{模式,分为共享与独占共享模式staticfinalNodeSHAREDnewNode();独占模式staticfinalNodeEXCLUSIVEnull;结点状态CANCELLED,值为1,表示当前的线程被取消SIGNAL,值为1,表示当前节点的后继节点包含的线程需要运行,也就是unparkCONDITION,值为2,表示当前节点在等待condition,也就是在condition队列中PROPAGATE,值为3,表示当前场景下后续的acquireShared能够得以执行值为0,表示当前节点在sync队列中,等待着获取锁staticfinalintCANCELLED1;staticfinalintSIGNAL1;staticfinalintCONDITION2;staticfinalintPROPAGATE3;结点状态volatileintwaitStatus;前驱结点volatileNodeprev;后继结点volatileNodenext;结点所对应的线程volatileThreadthread;下一个等待者NodenextWaiter;结点是否在共享模式下等待finalbooleanisShared(){returnnextWaiterSHARED;}获取前驱结点,若前驱结点为空,抛出异常finalNodepredecessor()throwsNullPointerException{保存前驱结点Nodepprev;if(pnull)前驱结点为空,抛出异常thrownewNullPointerException();else前驱结点不为空,返回returnp;}无参构造方法Node(){UsedtoestablishinitialheadorSHAREDmarker}构造方法Node(Threadthread,Nodemode){UsedbyaddWaiterthis。nextWaitermode;this。threadthread;}构造方法Node(Threadthread,intwaitStatus){UsedbyConditionthis。waitStatuswaitStatus;this。threadthread;}}
每个线程被阻塞的线程都会被封装成一个Node结点,放入队列。每个节点包含了一个Thread类型的引用,并且每个节点都存在一个状态,具体状态如下。CANCELLED,值为1,表示当前的线程被取消。SIGNAL,值为1,表示当前节点的后继节点包含的线程需要运行,需要进行unpark操作。CONDITION,值为2,表示当前节点在等待condition,也就是在conditionqueue中。PROPAGATE,值为3,表示当前场景下后续的acquireShared能够得以执行。值为0,表示当前节点在syncqueue中,等待着获取锁。类的内部类ConditionObject类
这个类有点长,耐心看下:内部类publicclassConditionObjectimplementsCondition,java。io。Serializable{版本号privatestaticfinallongserialVersionUID1173984872572414699L;Firstnodeofconditionqueue。condition队列的头节点privatetransientNodefirstWaiter;Lastnodeofconditionqueue。condition队列的尾结点privatetransientNodelastWaiter;Createsanew{codeConditionObject}instance。构造方法publicConditionObject(){}InternalmethodsAddsanewwaitertowaitqueue。returnitsnewwaitnode添加新的waiter到wait队列privateNodeaddConditionWaiter(){保存尾结点NodetlastWaiter;IflastWaiteriscancelled,cleanout。if(t!nullt。waitStatus!Node。CONDITION){尾结点不为空,并且尾结点的状态不为CONDITION清除状态为CONDITION的结点unlinkCancelledWaiters();将最后一个结点重新赋值给ttlastWaiter;}新建一个结点NodenodenewNode(Thread。currentThread(),Node。CONDITION);if(tnull)尾结点为空设置condition队列的头节点firstWaiternode;else尾结点不为空设置为节点的nextWaiter域为node结点t。nextWaiternode;更新condition队列的尾结点lastWaiternode;returnnode;}Removesandtransfersnodesuntilhitnoncancelledoneornull。Splitoutfromsignalinparttoencouragecompilerstoinlinethecaseofnowaiters。paramfirst(nonnull)thefirstnodeonconditionqueueprivatevoiddoSignal(Nodefirst){循环do{if((firstWaiterfirst。nextWaiter)null)该节点的nextWaiter为空设置尾结点为空lastWaiternull;设置first结点的nextWaiter域first。nextWaiternull;}while(!transferForSignal(first)(firstfirstWaiter)!null);将结点从condition队列转移到sync队列失败并且condition队列中的头节点不为空,一直循环}Removesandtransfersallnodes。paramfirst(nonnull)thefirstnodeonconditionqueueprivatevoiddoSignalAll(Nodefirst){condition队列的头节点尾结点都设置为空lastWaiterfirstWaiternull;循环do{获取first结点的nextWaiter域结点Nodenextfirst。nextWaiter;设置first结点的nextWaiter域为空first。nextWaiternull;将first结点从condition队列转移到sync队列transferForSignal(first);重新设置firstfirstnext;}while(first!null);}Unlinkscancelledwaiternodesfromconditionqueue。Calledonlywhileholdinglock。Thisiscalledwhencancellationoccurredduringconditionwait,anduponinsertionofanewwaiterwhenlastWaiterisseentohavebeencancelled。Thismethodisneededtoavoidgarbageretentionintheabsenceofsignals。Soeventhoughitmayrequireafulltraversal,itcomesintoplayonlywhentimeoutsorcancellationsoccurintheabsenceofsignals。Ittraversesallnodesratherthanstoppingataparticulartargettounlinkallpointerstogarbagenodeswithoutrequiringmanyretraversalsduringcancellationstorms。从condition队列中清除状态为CANCEL的结点privatevoidunlinkCancelledWaiters(){保存condition队列头节点NodetfirstWaiter;Nodetrailnull;while(t!null){t不为空下一个结点Nodenextt。nextWaiter;if(t。waitStatus!Node。CONDITION){t结点的状态不为CONDTION状态设置t节点的nextWaiter域为空t。nextWaiternull;if(trailnull)trail为空重新设置condition队列的头节点firstWaiternext;elsetrail不为空设置trail结点的nextWaiter域为next结点trail。nextWaiternext;if(nextnull)next结点为空设置condition队列的尾结点lastWaitertrail;}elset结点的状态为CONDTION状态设置trail结点trailt;设置t结点tnext;}}publicmethodsMovesthelongestwaitingthread,ifoneexists,fromthewaitqueueforthisconditiontothewaitqueuefortheowninglock。throwsIllegalMonitorStateExceptionif{linkisHeldExclusively}returns{codefalse}唤醒一个等待线程。如果所有的线程都在等待此条件,则选择其中的一个唤醒。在从await返回之前,该线程必须重新获取锁。publicfinalvoidsignal(){if(!isHeldExclusively())不被当前线程独占,抛出异常thrownewIllegalMonitorStateException();保存condition队列头节点NodefirstfirstWaiter;if(first!null)头节点不为空唤醒一个等待线程doSignal(first);}Movesallthreadsfromthewaitqueueforthisconditiontothewaitqueuefortheowninglock。throwsIllegalMonitorStateExceptionif{linkisHeldExclusively}returns{codefalse}唤醒所有等待线程。如果所有的线程都在等待此条件,则唤醒所有线程。在从await返回之前,每个线程都必须重新获取锁。publicfinalvoidsignalAll(){if(!isHeldExclusively())不被当前线程独占,抛出异常thrownewIllegalMonitorStateException();保存condition队列头节点NodefirstfirstWaiter;if(first!null)头节点不为空唤醒所有等待线程doSignalAll(first);}Implementsuninterruptibleconditionwait。olliSavelockstatereturnedby{linkgetState}。liInvoke{linkrelease}withsavedstateasargument,throwingIllegalMonitorStateExceptionifitfails。liBlockuntilsignalled。liReacquirebyinvokingspecializedversionof{linkacquire}withsavedstateasargument。ol等待,当前线程在接到信号之前一直处于等待状态,不响应中断publicfinalvoidawaitUninterruptibly(){添加一个结点到等待队列NodenodeaddConditionWaiter();获取释放的状态intsavedStatefullyRelease(node);booleaninterruptedfalse;while(!isOnSyncQueue(node)){阻塞当前线程LockSupport。park(this);if(Thread。interrupted())当前线程被中断设置interrupted状态interruptedtrue;}if(acquireQueued(node,savedState)interrupted)selfInterrupt();}Forinterruptiblewaits,weneedtotrackwhethertothrowInterruptedException,ifinterruptedwhileblockedoncondition,versusreinterruptcurrentthread,ifinterruptedwhileblockedwaitingtoreacquire。ModemeaningtoreinterruptonexitfromwaitprivatestaticfinalintREINTERRUPT1;ModemeaningtothrowInterruptedExceptiononexitfromwaitprivatestaticfinalintTHROWIE1;Checksforinterrupt,returningTHROWIEifinterruptedbeforesignalled,REINTERRUPTifaftersignalled,or0ifnotinterrupted。privateintcheckInterruptWhileWaiting(Nodenode){returnThread。interrupted()?(transferAfterCancelledWait(node)?THROWIE:REINTERRUPT):0;}ThrowsInterruptedException,reinterruptscurrentthread,ordoesnothing,dependingonmode。privatevoidreportInterruptAfterWait(intinterruptMode)throwsInterruptedException{if(interruptModeTHROWIE)thrownewInterruptedException();elseif(interruptModeREINTERRUPT)selfInterrupt();}Implementsinterruptibleconditionwait。olliIfcurrentthreadisinterrupted,throwInterruptedException。liSavelockstatereturnedby{linkgetState}。liInvoke{linkrelease}withsavedstateasargument,throwingIllegalMonitorStateExceptionifitfails。liBlockuntilsignalledorinterrupted。liReacquirebyinvokingspecializedversionof{linkacquire}withsavedstateasargument。liIfinterruptedwhileblockedinstep4,throwInterruptedException。ol等待,当前线程在接到信号或被中断之前一直处于等待状态publicfinalvoidawait()throwsInterruptedException{if(Thread。interrupted())当前线程被中断,抛出异常thrownewInterruptedException();在wait队列上添加一个结点NodenodeaddConditionWaiter();intsavedStatefullyRelease(node);intinterruptMode0;while(!isOnSyncQueue(node)){阻塞当前线程LockSupport。park(this);if((interruptModecheckInterruptWhileWaiting(node))!0)检查结点等待时的中断类型break;}if(acquireQueued(node,savedState)interruptMode!THROWIE)interruptModeREINTERRUPT;if(node。nextWaiter!null)cleanupifcancelledunlinkCancelledWaiters();if(interruptMode!0)reportInterruptAfterWait(interruptMode);}Implementstimedconditionwait。olliIfcurrentthreadisinterrupted,throwInterruptedException。liSavelockstatereturnedby{linkgetState}。liInvoke{linkrelease}withsavedstateasargument,throwingIllegalMonitorStateExceptionifitfails。liBlockuntilsignalled,interrupted,ortimedout。liReacquirebyinvokingspecializedversionof{linkacquire}withsavedstateasargument。liIfinterruptedwhileblockedinstep4,throwInterruptedException。ol等待,当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态publicfinallongawaitNanos(longnanosTimeout)throwsInterruptedException{if(Thread。interrupted())thrownewInterruptedException();NodenodeaddConditionWaiter();intsavedStatefullyRelease(node);finallongdeadlineSystem。nanoTime()nanosTimeout;intinterruptMode0;while(!isOnSyncQueue(node)){if(nanosTimeout0L){transferAfterCancelledWait(node);break;}if(nanosTimeoutspinForTimeoutThreshold)LockSupport。parkNanos(this,nanosTimeout);if((interruptModecheckInterruptWhileWaiting(node))!0)break;nanosTimeoutdeadlineSystem。nanoTime();}if(acquireQueued(node,savedState)interruptMode!THROWIE)interruptModeREINTERRUPT;if(node。nextWaiter!null)unlinkCancelledWaiters();if(interruptMode!0)reportInterruptAfterWait(interruptMode);returndeadlineSystem。nanoTime();}Implementsabsolutetimedconditionwait。olliIfcurrentthreadisinterrupted,throwInterruptedException。liSavelockstatereturnedby{linkgetState}。liInvoke{linkrelease}withsavedstateasargument,throwingIllegalMonitorStateExceptionifitfails。liBlockuntilsignalled,interrupted,ortimedout。liReacquirebyinvokingspecializedversionof{linkacquire}withsavedstateasargument。liIfinterruptedwhileblockedinstep4,throwInterruptedException。liIftimedoutwhileblockedinstep4,returnfalse,elsetrue。ol等待,当前线程在接到信号、被中断或到达指定最后期限之前一直处于等待状态publicfinalbooleanawaitUntil(Datedeadline)throwsInterruptedException{longabstimedeadline。getTime();if(Thread。interrupted())thrownewInterruptedException();NodenodeaddConditionWaiter();intsavedStatefullyRelease(node);booleantimedoutfalse;intinterruptMode0;while(!isOnSyncQueue(node)){if(System。currentTimeMillis()abstime){timedouttransferAfterCancelledWait(node);break;}LockSupport。parkUntil(this,abstime);if((interruptModecheckInterruptWhileWaiting(node))!0)break;}if(acquireQueued(node,savedState)interruptMode!THROWIE)interruptModeREINTERRUPT;if(node。nextWaiter!null)unlinkCancelledWaiters();if(interruptMode!0)reportInterruptAfterWait(interruptMode);return!timedout;}Implementstimedconditionwait。olliIfcurrentthreadisinterrupted,throwInterruptedException。liSavelockstatereturnedby{linkgetState}。liInvoke{linkrelease}withsavedstateasargument,throwingIllegalMonitorStateExceptionifitfails。liBlockuntilsignalled,interrupted,ortimedout。liReacquirebyinvokingspecializedversionof{linkacquire}withsavedstateasargument。liIfinterruptedwhileblockedinstep4,throwInterruptedException。liIftimedoutwhileblockedinstep4,returnfalse,elsetrue。ol等待,当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态。此方法在行为上等效于:awaitNanos(unit。toNanos(time))0publicfinalbooleanawait(longtime,TimeUnitunit)throwsInterruptedException{longnanosTimeoutunit。toNanos(time);if(Thread。interrupted())thrownewInterruptedException();NodenodeaddConditionWaiter();intsavedStatefullyRelease(node);finallongdeadlineSystem。nanoTime()nanosTimeout;booleantimedoutfalse;intinterruptMode0;while(!isOnSyncQueue(node)){if(nanosTimeout0L){timedouttransferAfterCancelledWait(node);break;}if(nanosTimeoutspinForTimeoutThreshold)LockSupport。parkNanos(this,nanosTimeout);if((interruptModecheckInterruptWhileWaiting(node))!0)break;nanosTimeoutdeadlineSystem。nanoTime();}if(acquireQueued(node,savedState)interruptMode!THROWIE)interruptModeREINTERRUPT;if(node。nextWaiter!null)unlinkCancelledWaiters();if(interruptMode!0)reportInterruptAfterWait(interruptMode);return!timedout;}supportforinstrumentationReturnstrueifthisconditionwascreatedbythegivensynchronizationobject。return{codetrue}ifownedfinalbooleanisOwnedBy(AbstractQueuedSynchronizersync){returnsyncAbstractQueuedSynchronizer。this;}Querieswhetheranythreadsarewaitingonthiscondition。Implements{linkAbstractQueuedSynchronizerhasWaiters(ConditionObject)}。return{codetrue}ifthereareanywaitingthreadsthrowsIllegalMonitorStateExceptionif{linkisHeldExclusively}returns{codefalse}查询是否有正在等待此条件的任何线程protectedfinalbooleanhasWaiters(){if(!isHeldExclusively())thrownewIllegalMonitorStateException();for(NodewfirstWaiter;w!null;ww。nextWaiter){if(w。waitStatusNode。CONDITION)returntrue;}returnfalse;}Returnsanestimateofthenumberofthreadswaitingonthiscondition。Implements{linkAbstractQueuedSynchronizergetWaitQueueLength(ConditionObject)}。returntheestimatednumberofwaitingthreadsthrowsIllegalMonitorStateExceptionif{linkisHeldExclusively}returns{codefalse}返回正在等待此条件的线程数估计值protectedfinalintgetWaitQueueLength(){if(!isHeldExclusively())thrownewIllegalMonitorStateException();intn0;for(NodewfirstWaiter;w!null;ww。nextWaiter){if(w。waitStatusNode。CONDITION)n;}returnn;}ReturnsacollectioncontainingthosethreadsthatmaybewaitingonthisCondition。Implements{linkAbstractQueuedSynchronizergetWaitingThreads(ConditionObject)}。returnthecollectionofthreadsthrowsIllegalMonitorStateExceptionif{linkisHeldExclusively}returns{codefalse}返回包含那些可能正在等待此条件的线程集合protectedfinalCollectionThreadgetWaitingThreads(){if(!isHeldExclusively())thrownewIllegalMonitorStateException();ArrayListThreadlistnewArrayListThread();for(NodewfirstWaiter;w!null;ww。nextWaiter){if(w。waitStatusNode。CONDITION){Threadtw。thread;if(t!null)list。add(t);}}returnlist;}}
此类实现了Condition接口,Condition接口定义了条件操作规范,具体如下publicinterfaceCondition{等待,当前线程在接到信号或被中断之前一直处于等待状态voidawait()throwsInterruptedException;等待,当前线程在接到信号之前一直处于等待状态,不响应中断voidawaitUninterruptibly();等待,当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态longawaitNanos(longnanosTimeout)throwsInterruptedException;等待,当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态。此方法在行为上等效于:awaitNanos(unit。toNanos(time))0booleanawait(longtime,TimeUnitunit)throwsInterruptedException;等待,当前线程在接到信号、被中断或到达指定最后期限之前一直处于等待状态booleanawaitUntil(Datedeadline)throwsInterruptedException;唤醒一个等待线程。如果所有的线程都在等待此条件,则选择其中的一个唤醒。在从await返回之前,该线程必须重新获取锁。voidsignal();唤醒所有等待线程。如果所有的线程都在等待此条件,则唤醒所有线程。在从await返回之前,每个线程都必须重新获取锁。voidsignalAll();}
Condition接口中定义了await、signal方法,用来等待条件、释放条件。之后会详细分析CondtionObject的源码。类的属性
属性中包含了头节点head,尾结点tail,状态state、自旋时间spinForTimeoutThreshold,还有AbstractQueuedSynchronizer抽象的属性在内存中的偏移地址,通过该偏移地址,可以获取和设置该属性的值,同时还包括一个静态初始化块,用于加载内存偏移地址。publicabstractclassAbstractQueuedSynchronizerextendsAbstractOwnableSynchronizerimplementsjava。io。Serializable{版本号privatestaticfinallongserialVersionUID7373984972572414691L;头节点privatetransientvolatileNodehead;尾结点privatetransientvolatileNodetail;状态privatevolatileintstate;自旋时间staticfinallongspinForTimeoutThreshold1000L;Unsafe类实例privatestaticfinalUnsafeunsafeUnsafe。getUnsafe();state内存偏移地址privatestaticfinallongstateOffset;head内存偏移地址privatestaticfinallongheadOffset;state内存偏移地址privatestaticfinallongtailOffset;tail内存偏移地址privatestaticfinallongwaitStatusOffset;next内存偏移地址privatestaticfinallongnextOffset;静态初始化块static{try{stateOffsetunsafe。objectFieldOffset(AbstractQueuedSynchronizer。class。getDeclaredField(state));headOffsetunsafe。objectFieldOffset(AbstractQueuedSynchronizer。class。getDeclaredField(head));tailOffsetunsafe。objectFieldOffset(AbstractQueuedSynchronizer。class。getDeclaredField(tail));waitStatusOffsetunsafe。objectFieldOffset(Node。class。getDeclaredField(waitStatus));nextOffsetunsafe。objectFieldOffset(Node。class。getDeclaredField(next));}catch(Exceptionex){thrownewError(ex);}}}类的构造方法
此类构造方法为从抽象构造方法,供子类调用。protectedAbstractQueuedSynchronizer(){}类的核心方法acquire方法
该方法以独占模式获取(资源),忽略中断,即线程在aquire过程中,中断此线程是无效的。源码如下:publicfinalvoidacquire(intarg){if(!tryAcquire(arg)acquireQueued(addWaiter(Node。EXCLUSIVE),arg))selfInterrupt();}
由上述源码可以知道,当一个线程调用acquire时,调用方法流程如下
首先调用tryAcquire方法,调用此方法的线程会试图在独占模式下获取对象状态。此方法应该查询是否允许它在独占模式下获取对象状态,如果允许,则获取它。在AbstractQueuedSynchronizer源码中默认会抛出一个异常,即需要子类去重写此方法完成自己的逻辑。之后会进行分析。若tryAcquire失败,则调用addWaiter方法,addWaiter方法完成的功能是将调用此方法的线程封装成为一个结点并放入Syncqueue。调用acquireQueued方法,此方法完成的功能是Syncqueue中的结点不断尝试获取资源,若成功,则返回true,否则,返回false。由于tryAcquire默认实现是抛出异常,所以此时,不进行分析,之后会结合一个例子进行分析。
首先分析addWaiter方法添加等待者privateNodeaddWaiter(Nodemode){新生成一个结点,默认为独占模式NodenodenewNode(Thread。currentThread(),mode);Trythefastpathofenq;backuptofullenqonfailure保存尾结点Nodepredtail;if(pred!null){尾结点不为空,即已经被初始化将node结点的prev域连接到尾结点node。prevpred;if(compareAndSetTail(pred,node)){比较pred是否为尾结点,是则将尾结点设置为node设置尾结点的next域为nodepred。nextnode;returnnode;返回新生成的结点}}enq(node);尾结点为空(即还没有被初始化过),或者是compareAndSetTail操作失败,则入队列returnnode;}
addWaiter方法使用快速添加的方式往syncqueue尾部添加结点,如果syncqueue队列还没有初始化,则会使用enq插入队列中,enq方法源码如下privateNodeenq(finalNodenode){for(;;){无限循环,确保结点能够成功入队列保存尾结点Nodettail;if(tnull){尾结点为空,即还没被初始化if(compareAndSetHead(newNode()))头节点为空,并设置头节点为新生成的结点tailhead;头节点与尾结点都指向同一个新生结点}else{尾结点不为空,即已经被初始化过将node结点的prev域连接到尾结点node。prevt;if(compareAndSetTail(t,node)){比较结点t是否为尾结点,若是则将尾结点设置为node设置尾结点的next域为nodet。nextnode;returnt;返回尾结点}}}}
enq方法会使用无限循环来确保节点的成功插入。
现在,分析acquireQueue方法。其源码如下sync队列中的结点在独占且忽略中断的模式下获取(资源)finalbooleanacquireQueued(finalNodenode,intarg){标志booleanfailedtrue;try{中断标志booleaninterruptedfalse;for(;;){无限循环获取node节点的前驱结点finalNodepnode。predecessor();if(pheadtryAcquire(arg)){前驱为头节点并且成功获得锁setHead(node);设置头节点p。nextnull;helpGCfailedfalse;设置标志returninterrupted;}if(shouldParkAfterFailedAcquire(p,node)parkAndCheckInterrupt())interruptedtrue;}}finally{if(failed)cancelAcquire(node);}}
首先获取当前节点的前驱节点,如果前驱节点是头节点并且能够获取(资源),代表该当前节点能够占有锁,设置头节点为当前节点,返回。否则,调用shouldParkAfterFailedAcquire和parkAndCheckInterrupt方法,首先,我们看shouldParkAfterFailedAcquire方法,代码如下当获取(资源)失败后,检查并且更新结点状态privatestaticbooleanshouldParkAfterFailedAcquire(Nodepred,Nodenode){获取前驱结点的状态intwspred。waitStatus;if(wsNode。SIGNAL)状态为SIGNAL,为1Thisnodehasalreadysetstatusaskingareleasetosignalit,soitcansafelypark。可以进行park操作returntrue;if(ws0){表示状态为CANCELLED,为1Predecessorwascancelled。Skipoverpredecessorsandindicateretry。do{node。prevpredpred。prev;}while(pred。waitStatus0);找到pred结点前面最近的一个状态不为CANCELLED的结点赋值pred结点的next域pred。nextnode;}else{为PROPAGATE3或者是0表示无状态,(为CONDITION2时,表示此节点在conditionqueue中)waitStatusmustbe0orPROPAGATE。Indicatethatweneedasignal,butdontparkyet。Callerwillneedtoretrytomakesureitcannotacquirebeforeparking。比较并设置前驱结点的状态为SIGNALcompareAndSetWaitStatus(pred,ws,Node。SIGNAL);}不能进行park操作returnfalse;}
只有当该节点的前驱结点的状态为SIGNAL时,才可以对该结点所封装的线程进行park操作。否则,将不能进行park操作。再看parkAndCheckInterrupt方法,源码如下进行park操作并且返回该线程是否被中断privatefinalbooleanparkAndCheckInterrupt(){在许可可用之前禁用当前线程,并且设置了blockerLockSupport。park(this);returnThread。interrupted();当前线程是否已被中断,并清除中断标记位}
parkAndCheckInterrupt方法里的逻辑是首先执行park操作,即禁用当前线程,然后返回该线程是否已经被中断。再看final块中的cancelAcquire方法,其源码如下取消继续获取(资源)privatevoidcancelAcquire(Nodenode){Ignoreifnodedoesntexistnode为空,返回if(nodenull)return;设置node结点的thread为空node。threadnull;Skipcancelledpredecessors保存node的前驱结点Nodeprednode。prev;while(pred。waitStatus0)找到node前驱结点中第一个状态小于0的结点,即不为CANCELLED状态的结点node。prevpredpred。prev;predNextistheapparentnodetounsplice。CASesbelowwillfailifnot,inwhichcase,welostracevsanothercancelorsignal,sonofurtheractionisnecessary。获取pred结点的下一个结点NodepredNextpred。next;CanuseunconditionalwriteinsteadofCAShere。Afterthisatomicstep,otherNodescanskippastus。Before,wearefreeofinterferencefromotherthreads。设置node结点的状态为CANCELLEDnode。waitStatusNode。CANCELLED;Ifwearethetail,removeourselves。if(nodetailcompareAndSetTail(node,pred)){node结点为尾结点,则设置尾结点为pred结点比较并设置pred结点的next节点为nullcompareAndSetNext(pred,predNext,null);}else{node结点不为尾结点,或者比较设置不成功Ifsuccessorneedssignal,trytosetpredsnextlinksoitwillgetone。Otherwisewakeituptopropagate。intws;if(pred!head((wspred。waitStatus)Node。SIGNAL(ws0compareAndSetWaitStatus(pred,ws,Node。SIGNAL)))pred。thread!null){(pred结点不为头节点,并且pred结点的状态为SIGNAL)或者pred结点状态小于等于0,并且比较并设置等待状态为SIGNAL成功,并且pred结点所封装的线程不为空保存结点的后继Nodenextnode。next;if(next!nullnext。waitStatus0)后继不为空并且后继的状态小于等于0compareAndSetNext(pred,predNext,next);比较并设置pred。nextnext;}else{unparkSuccessor(node);释放node的前一个结点}node。nextnode;helpGC}}
该方法完成的功能就是取消当前线程对资源的获取,即设置该结点的状态为CANCELLED,接着我们再看unparkSuccessor方法,源码如下释放后继结点privatevoidunparkSuccessor(Nodenode){Ifstatusisnegative(i。e。,possiblyneedingsignal)trytoclearinanticipationofsignalling。ItisOKifthisfailsorifstatusischangedbywaitingthread。获取node结点的等待状态intwsnode。waitStatus;if(ws0)状态值小于0,为SIGNAL1或CONDITION2或PROPAGATE3比较并且设置结点等待状态,设置为0compareAndSetWaitStatus(node,ws,0);Threadtounparkisheldinsuccessor,whichisnormallyjustthenextnode。Butifcancelledorapparentlynull,traversebackwardsfromtailtofindtheactualnoncancelledsuccessor。获取node节点的下一个结点Nodesnode。next;if(snulls。waitStatus0){下一个结点为空或者下一个节点的等待状态大于0,即为CANCELLEDs赋值为空snull;从尾结点开始从后往前开始遍历for(Nodettail;t!nullt!node;tt。prev)if(t。waitStatus0)找到等待状态小于等于0的结点,找到最前的状态小于等于0的结点保存结点st;}if(s!null)该结点不为为空,释放许可LockSupport。unpark(s。thread);}
该方法的作用就是为了释放node节点的后继结点。
对于cancelAcquire与unparkSuccessor方法,如下示意图可以清晰的表示:
其中node为参数,在执行完cancelAcquire方法后的效果就是unpark了s结点所包含的t4线程。
现在,再来看acquireQueued方法的整个的逻辑。逻辑如下:判断结点的前驱是否为head并且是否成功获取(资源)。若步骤1均满足,则设置结点为head,之后会判断是否finally模块,然后返回。若步骤2不满足,则判断是否需要park当前线程,是否需要park当前线程的逻辑是判断结点的前驱结点的状态是否为SIGNAL,若是,则park当前结点,否则,不进行park操作。若park了当前线程,之后某个线程对本线程unpark后,并且本线程也获得机会运行。那么,将会继续进行步骤的判断。类的核心方法release方法
以独占模式释放对象,其源码如下:publicfinalbooleanrelease(intarg){if(tryRelease(arg)){释放成功保存头节点Nodehhead;if(h!nullh。waitStatus!0)头节点不为空并且头节点状态不为0unparkSuccessor(h);释放头节点的后继结点returntrue;}returnfalse;}
其中,tryRelease的默认实现是抛出异常,需要具体的子类实现,如果tryRelease成功,那么如果头节点不为空并且头节点的状态不为0,则释放头节点的后继结点,unparkSuccessor方法已经分析过,不再累赘。
对于其他方法我们也可以分析,与前面分析的方法大同小异,所以,不再累赘。AbstractQueuedSynchronizer示例详解一
借助下面示例来分析AbstractQueuedSyncrhonizer内部的工作机制。示例源码如下importjava。util。concurrent。locks。Lock;importjava。util。concurrent。locks。ReentrantLock;classMyThreadextendsThread{privateLocklock;publicMyThread(Stringname,Locklock){super(name);this。locklock;}publicvoidrun(){lock。lock();try{System。out。println(Thread。currentThread()running);}finally{lock。unlock();}}}publicclassAbstractQueuedSynchonizerDemo{publicstaticvoidmain(String〔〕args){LocklocknewReentrantLock();MyThreadt1newMyThread(t1,lock);MyThreadt2newMyThread(t2,lock);t1。start();t2。start();}}
运行结果(可能的一种):Thread〔t1,5,main〕runningThread〔t2,5,main〕running
结果分析:从示例可知,线程t1与t2共用了一把锁,即同一个lock。可能会存在如下一种时序。
说明:首先线程t1先执行lock。lock操作,然后t2执行lock。lock操作,然后t1执行lock。unlock操作,最后t2执行lock。unlock操作。基于这样的时序,分析AbstractQueuedSynchronizer内部的工作机制。t1线程调用lock。lock方法,其方法调用顺序如下,只给出了主要的方法调用。
说明:其中,前面的部分表示哪个类,后面是具体的类中的哪个方法,AQS表示AbstractQueuedSynchronizer类,AOS表示AbstractOwnableSynchronizer类。t2线程调用lock。lock方法,其方法调用顺序如下,只给出了主要的方法调用。
说明:经过一系列的方法调用,最后达到的状态是禁用t2线程,因为调用了LockSupport。park。t1线程调用lock。unlock,其方法调用顺序如下,只给出了主要的方法调用。
说明:t1线程中调用lock。unlock后,经过一系列的调用,最终的状态是释放了许可,因为调用了LockSupport。unpark。这时,t2线程就可以继续运行了。此时,会继续恢复t2线程运行环境,继续执行LockSupport。park后面的语句,即进一步调用如下。
说明:在上一步调用了LockSupport。unpark后,t2线程恢复运行,则运行parkAndCheckInterrupt,之后,继续运行acquireQueued方法,最后达到的状态是头节点head与尾结点tail均指向了t2线程所在的结点,并且之前的头节点已经从sync队列中断开了。t2线程调用lock。unlock,其方法调用顺序如下,只给出了主要的方法调用。
说明:t2线程执行lock。unlock后,最终达到的状态还是与之前的状态一样。AbstractQueuedSynchronizer示例详解二
下面我们结合Condition实现生产者与消费者,来进一步分析AbstractQueuedSynchronizer的内部工作机制。Depot(仓库)类importjava。util。concurrent。locks。Condition;importjava。util。concurrent。locks。Lock;importjava。util。concurrent。locks。ReentrantLock;publicclassDepot{privateintsize;privateintcapacity;privateLocklock;privateConditionfullCondition;privateConditionemptyCondition;publicDepot(intcapacity){this。capacitycapacity;locknewReentrantLock();fullConditionlock。newCondition();emptyConditionlock。newCondition();}publicvoidproduce(intno){lock。lock();intleftno;try{while(left0){while(sizecapacity){System。out。println(Thread。currentThread()beforeawait);fullCondition。await();System。out。println(Thread。currentThread()afterawait);}intinc(leftsize)capacity?(capacitysize):left;leftinc;sizeinc;System。out。println(produceinc,sizesize);emptyCondition。signal();}}catch(InterruptedExceptione){e。printStackTrace();}finally{lock。unlock();}}publicvoidconsume(intno){lock。lock();intleftno;try{while(left0){while(size0){System。out。println(Thread。currentThread()beforeawait);emptyCondition。await();System。out。println(Thread。currentThread()afterawait);}intdec(sizeleft)0?left:size;leftdec;sizedec;System。out。println(consumedec,sizesize);fullCondition。signal();}}catch(InterruptedExceptione){e。printStackTrace();}finally{lock。unlock();}}}测试类classConsumer{privateDepotdepot;publicConsumer(Depotdepot){this。depotdepot;}publicvoidconsume(intno){newThread(newRunnable(){Overridepublicvoidrun(){depot。consume(no);}},noconsumethread)。start();}}classProducer{privateDepotdepot;publicProducer(Depotdepot){this。depotdepot;}publicvoidproduce(intno){newThread(newRunnable(){Overridepublicvoidrun(){depot。produce(no);}},noproducethread)。start();}}publicclassReentrantLockDemo{publicstaticvoidmain(String〔〕args)throwsInterruptedException{DepotdepotnewDepot(500);newProducer(depot)。produce(500);newProducer(depot)。produce(200);newConsumer(depot)。consume(500);newConsumer(depot)。consume(200);}}运行结果(可能的一种):produce500,size500Thread〔200producethread,5,main〕beforeawaitconsume500,size0Thread〔200consumethread,5,main〕beforeawaitThread〔200producethread,5,main〕afterawaitproduce200,size200Thread〔200consumethread,5,main〕afterawaitconsume200,size0
说明:根据结果,我们猜测一种可能的时序如下
说明:p1代表produce500的那个线程,p2代表produce200的那个线程,c1代表consume500的那个线程,c2代表consume200的那个线程。p1线程调用lock。lock,获得锁,继续运行,方法调用顺序在前面已经给出。p2线程调用lock。lock,由前面的分析可得到如下的最终状态。
说明:p2线程调用lock。lock后,会禁止p2线程的继续运行,因为执行了LockSupport。park操作。c1线程调用lock。lock,由前面的分析得到如下的最终状态。
说明:最终c1线程会在syncqueue队列的尾部,并且其结点的前驱结点(包含p2的结点)的waitStatus变为了SIGNAL。c2线程调用lock。lock,由前面的分析得到如下的最终状态。
说明:最终c1线程会在syncqueue队列的尾部,并且其结点的前驱结点(包含c1的结点)的waitStatus变为了SIGNAL。p1线程执行emptyCondition。signal,其方法调用顺序如下,只给出了主要的方法调用。
说明:AQS。CO表示AbstractQueuedSynchronizer。ConditionObject类。此时调用signal方法不会产生任何其他效果。p1线程执行lock。unlock,根据前面的分析可知,最终的状态如下。
说明:此时,p2线程所在的结点为头节点,并且其他两个线程(c1、c2)依旧被禁止,所以,此时p2线程继续运行,执行用户逻辑。p2线程执行fullCondition。await,其方法调用顺序如下,只给出了主要的方法调用。
说明:最终到达的状态是新生成了一个结点,包含了p2线程,此结点在conditionqueue中;并且syncqueue中p2线程被禁止了,因为在执行了LockSupport。park操作。从方法一些调用可知,在await操作中线程会释放锁资源,供其他线程获取。同时,head结点后继结点的包含的线程的许可被释放了,故其可以继续运行。由于此时,只有c1线程可以运行,故运行c1。继续运行c1线程,c1线程由于之前被park了,所以此时恢复,继续之前的步骤,即还是执行前面提到的acquireQueued方法,之后,c1判断自己的前驱结点为head,并且可以获取锁资源,最终到达的状态如下。
说明:其中,head设置为包含c1线程的结点,c1继续运行。c1线程执行fullCondtion。signal,其方法调用顺序如下,只给出了主要的方法调用。
说明:signal方法达到的最终结果是将包含p2线程的结点从conditionqueue中转移到syncqueue中,之后conditionqueue为null,之前的尾结点的状态变为SIGNAL。c1线程执行lock。unlock操作,根据之前的分析,经历的状态变化如下。
说明:最终c2线程会获取锁资源,继续运行用户逻辑。c2线程执行emptyCondition。await,由前面的第七步分析,可知最终的状态如下。
说明:await操作将会生成一个结点放入conditionqueue中与之前的一个conditionqueue是不相同的,并且unpark头节点后面的结点,即包含线程p2的结点。p2线程被unpark,故可以继续运行,经过CPU调度后,p2继续运行,之后p2线程在AQS:await方法中被park,继续AQS。CO:await方法的运行,其方法调用顺序如下,只给出了主要的方法调用。
p2继续运行,执行emptyCondition。signal,根据第九步分析可知,最终到达的状态如下。
说明:最终,将conditionqueue中的结点转移到syncqueue中,并添加至尾部,conditionqueue会为空,并且将head的状态设置为SIGNAL。p2线程执行lock。unlock操作,根据前面的分析可知,最后的到达的状态如下。
说明:unlock操作会释放c2线程的许可,并且将头节点设置为c2线程所在的结点。c2线程继续运行,执行fullCondition。signal,由于此时fullCondition的conditionqueue已经不存在任何结点了,故其不会产生作用。c2执行lock。unlock,由于c2是sync队列中最后一个结点,故其不会再调用unparkSuccessor了,直接返回true。即整个流程就完成了。AbstractQueuedSynchronizer总结
对于AbstractQueuedSynchronizer的分析,最核心的就是syncqueue的分析。每一个结点都是由前一个结点唤醒当结点发现前驱结点是head并且尝试获取成功,则会轮到该线程运行。conditionqueue中的结点向syncqueue中转移是通过signal操作完成的。当结点的状态为SIGNAL时,表示后面的结点需要运行。