AQS face questions

Q: What is the AQS?

A: the AQS full name (AbstractQueuedSynchronizer), this class java.util.concurrent.locks package below. AQS is used to build a frame lock and a synchronizer , such ReentrantLock, Semaphore, ReentrantReadWriteLock, SynchronousQueue, FutureTask the like are all based on the AQS.

Q: What AQS is the core idea? It is how to achieve?

A: 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.  AQS use a voliate int member variables to represent the synchronization status, access to resources to complete the work queue threads through built-in FIFO queue. CAS AQS using the atomic operation to achieve the synchronization state changes in its value.

AQS defines two resource Access: exclusive (only one thread can access the execution, but also according to whether the order queue lock into equity and non-equity locks, such as ReentrantLock ) and shared (multiple threads execute simultaneously accessible, such as Semaphore / CountDownLatch, Semaphore, CountDownLatch, CyclicBarrier ). ReentrantReadWriteLock can be seen as modular, allowing multiple threads to read a resource. 

AQS bottom using a template method pattern,  custom synchronization is achieved only when the need to achieve and obtain the release of ways to share resources to state, but the specific thread waits to maintain the queue (access to resources such as failure of the team / wake-out teams), AQS has already helped us achieve top good.

Guess you like

Origin www.cnblogs.com/doit8791/p/10971420.html