AQS:AbstractQueuedSynchronizer
A, AQS frame is used to build a lock and synchronizer using a AQS can be constructed simply and efficiently a large number of widely synchronizer.
Second, the principle: AQS core idea is that if the requested shared resource is free, then the current request thread sets of resources for effective worker threads, and a shared resource to a locked state. If the shared resource is requested is occupied, then you need a thread to block and wait for the lock mechanism assigned to be awakened, AQS this mechanism is implemented by CLH lock queue, is about to get less than temporarily lock a thread is added to the queue.
Schematic:
Third, learn the road map. A large number of applications in various classes and basic toolbar in juc, form the basis of Java and contract.
Fourth, 1, there is an important status symbol --state, this property is an int representing the current state of the object. Protected final three way to change the state of the value, namely: getState, setState (int), compareAndSetState (int, int).
2, AQS defines two resource sharing
-
Exclusive (exclusive): Only one thread can perform, such as ReentrantLock. Lock can be divided into equity and non-equity locks:
- Fair lock: line up the thread according to the order in the queue, first come, first get a lock
- Unfair lock: When a thread to acquire the lock, ignoring the direct order of the queue to grab the lock, who is who grab
- Share (Shared): multiple threads may be executed simultaneously, such as Semaphore / CountDownLatch. Semaphore, CountDownLatCh, CyclicBarrier, ReadWriteLock.