AQS is java.util.concurrent.locks.AbstractQueuedSynchronizer
actually this class. Read Java concurrency package source code you will find this class is the java.util.concurrent
core of the whole, can also be said to read the whole and the contract source of a breakthrough.
For example ReentrantLock
, read the source code you will find its core is one of its internal classes Sync
:
The structure of many classes throughout the package is the same, Semaphore
for CountDownLatch
example, there is an Sync
inner class, and all sync is inherited from AbstractQueuedSynchronizer
. So if you want to read Java and contract the code, first you have to read this class.
AQS The core is to synchronize state through a shared variable, the state of the variable is maintained by subclasses, and the AQS Framework does:
- Maintenance of thread blocking queues
- Thread blocking and Wakeup
The modification of shared variables is done through the Unsafe
CAS operations provided by the class. AbstractQueuedSynchronizer
The main method of the class is acquire
and release
, typically, a template method, the following 4 methods are implemented by subclasses:
protected boolean tryacquire (int arg)protectedboolean tryrelease (int Arg)protectedint tryacquireshared (int arg) Protectedboolean tryreleaseshared (int arg)
The acquire method is used to obtain the lock, which returns true to indicate that the thread gets successfully continued execution, and once returned false the thread joins the wait queue, waiting to be awakened, and the release method is used to release the lock. In general implementations these two methods are encapsulated as the lock
and unlock
method.
The SimpleLock
following class implements a function of the simplest non-reentrant mutex, which is ThreadPoolExecutor$Worker
actually the implementation (this class will be mentioned in a future article).
classSimplelockextendsAbstractqueuedsynchronizer {Private Static Final LongSerialversionuid = -7316320116933187634l; PublicSimplelock () {}protected BooleanTryacquire (intunused) { if(Compareandsetstate (0, 1) {setexclusiveownerthread (Thread.CurrentThread ()); return true; } return false; } protected BooleanTryrelease (intunused) {Setexclusiveownerthread (NULL); SetState (0); return true; } Public voidLock () {Acquire (1); } Public BooleanTrylock () {returnTryacquire (1); } Public voidunlock () {Release (1); } Public BooleanisLocked () {returnisheldexclusively (); }}
Public Static voidMain (string[] args)throwsinterruptedexception {FinalSimplelock lock =NewSimplelock (); Lock.lock (); for(inti = 0; I < 10; i++) { NewThread (NewRunnable () {@Override Public voidrun () {lock.lock (); System.out.println (Thread.CurrentThread (). GetId ()+ "acquired the lock!"); Lock.unlock (); }}). Start (); //simply let the thread block on lock in the order of the For LoopThread.Sleep (100); } System.out.println ("Main thread unlock!"); Lock.unlock ();}
Run the above test code with the following results:
MainThreadUnlock!9AcquiredTheLock!10AcquiredTheLock!11AcquiredTheLock! 12 acquired the lock! acquired the lock!acquired the lock!acquired the lock!acquired the lock!acquired the lock! acquired the lock!
/span>
It is found that the waiting threads are fetched to the lock in the order in which they were blocked. This is because Aqs is based on a CLH lock queue
variant called to implement the thread blocking queue, and our next article will simply understand the next CLH lock queue.
Follow-up articles are planned as follows:
- Java AQS framework of Source Learning (ii) CLH lock queue and spin lock
- Aqs framework of Java and Contract source Learning (iii) Locksupport
- Java and Contract Source Learning Aqs Framework (iv) Abstractqueuedsynchronizer source code Analysis
- Java and Contract Source Learning Aqs Framework (v) Conditionobject Source code Analysis
......
- Java and Contract Source Learning Lock (a) overview
- Java and Contract Source Learning Lock (ii) Reentrantlock source analysis
......
- Java and contract source learning thread pool (a) overview
- Java and contract source learning thread pool (ii) Threadpoolexecutor source analysis
......
Learn Java and the original intention of the contract source is to find out before a problem, in fact, very early on to see this piece of source code but has not looked down, so say to see the source must have a purpose can not look at.
Java AQS Framework for the source learning of the contract (a) overview