java concurrent programming resolve the AQS

What is the AQS

For example, exclusive locks and shared locks ReentrantLock .... countdownlatch, Semaphore AbstractQueuedSynchronizer are based on a common base class according to AQS we can easily build their own synchronizer

AQS lock category with users exclusive lock and a shared lock

Locked in a point in time can only be possessed a thread. According to acquire the lock mechanism, is divided into equity and non-equity lock lock queue waiting to acquire the lock according to the FIFO principle, the longer the waiting time of the first thread to acquire the lock, which is fair to acquire the lock, the lock that is fair. Rather than the fair locks, thread gets the lock when, ignoring the queue directly acquire the lock. ReentrantLock and ReentrantReadWriteLock.Writelock are exclusive lock

Shared locks: the same time can be more thread gets locked, the lock can be shared. JUC package ReentrantReadWriteLock.ReadLock, CyclicBarrier, CountDownLatch and Semaphore all shared locks.

AQS sync node structure provided

1. Obtain the original first node the first node to release and wake successor nodes (first node acquires the synchronous queue)

1. added to obtain a lock thread synchronization queue (the head node acquires the synchronous queue)

Use volatile modifications to ensure the visibility of the thread

AQS == AbstractQueuedSynchronizer, queue synchronizer, which is used to build the basic framework Java concurrency locks and other synchronization component has a FIFO (first in first out queue) inside

Synchronous application subclasses to achieve some abstract methods inherited by the synchronizer, the subclass mainly provided by using a method of synchronizing the state to statemodify

Modified state three methods:

getState() Gets the current sync status

setState(int newState) Set the current sync status

compareAndSetState(int expect,int update) Sets the current state using the CAS method can guarantee atomicity state set

AQS AQS use and design patterns using a template method pattern (the parent class defines a framework to achieve sub-class method called the father at the time called it) subclass inherits the parent class and then override the specified

AQS methods need to be rewritten, what does?

protected boolean tryAcquire(int arg): Get exclusive synchronization status, try to get returns true if successful, otherwise it is false protected boolean tryRelease(int arg): exclusive release synchronous state, other threads waiting at this time will have the opportunity to acquire synchronization status; protected int tryAcquireShared(int arg): Shared get synchronized state, the return value is greater than equal to 0, representative for success; on the contrary acquisition failure; protected boolean tryReleaseShared(int arg): shared released synchronization status, success is true, failed to false protected boolean isHeldExclusively(): if the thread is occupied in exclusive mode.

In fact, here we found that due to the use of java the tube, so AQS also used this idea acquireand releaseis used to obtain the lock and release the lock

AQS share exclusive lock cover lock subclasses process

It has been reentrantLockan example class that inherits AbstractQueuedSynchronizerlater realization of the need to rewrite the corresponding

AQS exclusive synchronous state acquisition and release

Sovereign state synchronization process to obtain the release of all

Get exclusive lock

The overall process:

Call synchronizer acquire (int arg) method gets synchronization state, the method is insensitive to interrupt, i.e., the thread acquires the synchronous queue synchronization state fails to enter, when the subsequent interrupt operation of the thread, the thread does not remove from the synchronization queue .

  • The current thread to realize () method attempts to acquire the lock by tryAcquire, obtain successful direct return, if the attempt fails, then enter the queue waiting to be thread-safe (CAS) acquisition synchronization status.

  • If the attempt to acquire the lock fails, synchronizing node configuration (exclusive of Node.EXCLUSIVE), by addWaiter (Node node, int args) method, the node is added to the tail of the queue in the synchronous queue.

  • Last call acquireQueued (final Node node, int args) method, so that the node by way of an endless loop of acquiring synchronization status, if the acquisition is less than the nodes in the thread is blocked. acquireQueued method to obtain the current thread synchronization state in an endless loop, and only when the precursor node is head node before attempting to acquire the lock (synchronous state) (p == head && tryAcquire (arg)).

Guess you like

Origin juejin.im/post/5d58b4b251882505750d646c