Java Aqs Learning

Source: Internet
Author: User

Brief introduction of AQS effect AQS

AQS is the acronym for Abstractqueuedsynchronizer, AQS is the underlying framework (or basic tool Class) used to build locks or other synchronization components, which maintains a volatile int state To represent the synchronization state and a FIFO queue to complete the blocking queue during resource acquisition, and the packet author (Doug Lea) expects it to be the basis for most of the synchronization requirements.

Use of Aqs

The primary use of Aqs is inheritance, which manages the state of synchronization by inheriting Aqs and implementing its abstract methods, which are defined as static inner classes of custom synchronization components. Subclasses manage the synchronization state by inheriting the Synchronizer and implementing an abstract method of Aqs, and the Synchronizer itself does not implement any synchronization interfaces, it is simply a tool method that defines a number of synchronization state fetches and releases for use by custom synchronization components. AQS uses the CAS approach to ensure that the synchronization state changes are thread-safe. Three ways to access state status:

    • GetState ()
    • SetState ()
    • Compareandsetstate ()

AQS defines two ways to share resources: Exclusive (exclusive, only one thread can execute at the same time, such as Reentrantlock) and share (shared, multiple threads can access simultaneously, such as Semaphore/countdownlatch).

Different custom synchronizers compete for shared resources in different ways. The custom Synchronizer needs to realize how to obtain and release the shared resource state when implementing it, and for the implementation of the wait queue maintenance (such as queue \ wake-up queues for resource failure), AQS is already implemented at the bottom. There are several main ways to implement the custom Synchronizer implementation:

    • Isheldexclusively (): Whether the thread is monopolizing the resource. Only use the condition to realize it.
    • Tryacquire (int): Exclusive mode. Attempts to get a resource, success returns TRUE, and failure returns false.
    • Tryrelease (int): Exclusive mode. Attempt to free the resource, success returns TRUE, and failure returns false.
    • tryacquireshared (int): How to share. Try to get the resource. A negative number indicates a failure; 0 indicates success, but there is no remaining available resources; A positive number indicates success and there are remaining resources.
    • tryreleaseshared (int): How to share. Attempt to free the resource, success returns TRUE, and failure returns false.

In Reentrantlock, for example, state is initialized to 0, which indicates an unlocked status. A thread lock () will call Tryacqure () to monopolize the lock and will state+1. Later, the other thread Tryacquire () fails until the A thread unlock () to state=0 (that is, releasing the lock), and the other thread has the opportunity to acquire the lock. Of course, before releasing the lock, the a thread itself can get the lock repeatedly (state accumulation), which is the concept of reentrant. Be aware, however, how many times you have to release it to ensure that the state is back to 0 states.

In Countdownlatch, for example, the task is divided into N threads for execution, and the state is initialized to n (Note that n is consistent with the number of threads). This n sub-thread is executed in parallel, once each child thread finishes executing countdown () once, state CAs minus 1. When all child threads are executed (that is, state=0), the thread that invokes the thread is Unpark (), and the thread will return from the await () function, continuing with the remainder action.

In general, the custom Synchronizer is either exclusive or shared, and they only need to implement one of the Tryacquire-tryrelease, tryacquireshared-tryreleaseshared. However, Aqs also supports both exclusive and shared implementations of the custom Synchronizer, such as: Reentrantreadwritelock.

The template methods provided by the Synchronizer are basically divided into 3 categories: Exclusive get and Release synchronization state, shared fetch and release synchronization state, and query waiting threads in the synchronization queue.

Java Aqs Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.