Juc Review of the implementation principle of-aqs Synchronizer

Source: Internet
Author: User
Tags cas semaphore

1. What is AQS?

The core idea of Aqs is to modify the current lock state by using a volatile variable such as volatile int, with the unsafe tool to manipulate its atomicity. The Synchronizer internally relies on a FIFO bidirectional queue to complete the queued work of the resource acquisition thread.

2. Application of Synchronizer

The main use of the Synchronizer is inheritance, the subclass inherits the Synchronizer and implements its abstract method to manage the synchronization state, modify the state of the synchronization or access the 3 methods mainly provided by the Synchronizer:

    • GetState () Gets the current synchronization state
    • setState (int newstate) sets the current synchronization state
    • compareandsetstate (int expect,int update) uses CAs to set the current state, which guarantees the atomicity of state settings.

The Synchronizer can support the exclusive acquisition of the synchronization state, also can support the shared state of obtaining synchronization, so that the different types of synchronization components can be easily implemented.

Synchronizer is also the key to implement the lock, in the implementation of the lock aggregation Synchronizer, the use of Synchronizer to implement the semantics of the lock.

3.AQS Synchronization Queue

Synchronizer Aqs internal implementation is dependent on the synchronization queue (a FIFO two-way queue, is actually a data structure bidirectional linked list) to complete the synchronization state management.

When the current thread acquires a synchronization state failure, the Synchronizer Aqs constructs information such as the current thread and wait state to join the synchronization queue and block the current thread;

When the synchronization state is released, the thread in the first node wakes up, causing the first node's thread to try to get the synchronization state again. Aqs is the parent class for the implementation of exclusive and shared locks.

4.AQS Lock Category: divided into exclusive lock and shared lock two kinds.

    • Exclusive lock: The lock can only be occupied by one thread in a point in time. According to the lock mechanism, it is divided into "fair lock" and "non-fair lock". wait for the queue in accordance with the principle of FIFO to obtain a lock, the longer the waiting time line Cheng first acquired to the lock, which is fair to acquire the lock, that is, a fair lock. Instead of a fair lock, the thread acquires the lock while ignoring the waiting queue to acquire the lock directly. Reentrantlock and Reentrantreadwritelock.writelock are exclusive locks.
    • Shared Lock: A lock that can be shared by multiple threads at the same time. Both Reentrantreadwritelock.readlock,cyclicbarrier,countdownlatch and semaphore in the Juc package are shared locks.

The lock in the JUC package includes: Lock interface, Readwritelock interface, condition condition, locksupport blocking primitive.

Abstractownablesynchronizer/abstractqueuedsynchronizer/abstractqueuedlongsynchronizer three abstract classes,

Reentrantlock exclusive lock, Reentrantreadwritelock read-write lock. Countdownlatch,cyclicbarrier and semaphore are also implemented through AQS.

Here are the Aqs and some of the locks implemented with AQS, as well as the architecture diagram of some of the tool classes implemented through AQS:

The structure of the 5.AQS Synchronizer: The Synchronizer has the first node (head) and the tail node (tail). The basic structure of the synchronization queue is as follows:

Figure 1: The basic structure of the synchronization queue Compareandsettail (Node expect,node Update)

    • synchronization Queue set tail node (thread not acquired to lock join synchronization queue): synchronizer Aqs contains a reference to two node types: a reference to the head node (head), a reference to the tail (tail), when a thread succeeds in acquiring the lock (synchronous state), Other threads cannot get to the lock, but are constructed as nodes (containing the current thread, waiting state) joined to the synchronization queue to wait for the thread to acquire the lock. This process of joining the queue must be thread-safe. Otherwise, if multiple threads are in the environment, the nodes that are added to the queue may be in an incorrect order, or the number is not correct. Thus the Synchronizer provides a method for setting the tail node of the CAs atom (to ensure that a thread that is not acquired to the synchronization state is joined to the synchronization queue and the next friend line is able to join). For example, set the tail node:

Figure 2: The settings node of the tail node joins the synchronization queue

    • Synchronization Queue Set the first node (the original head node release lock, Wake the successor node): the synchronization queue follows the FIFO, the head node is the node that obtains the lock (synchronous state) succeeds, the head node wakes up the successor node when the synchronization state is released, The successor node will set itself as the head node when it succeeds in acquiring the lock (synchronous state). The set head node is done by a thread that acquires a successful lock (synchronous state), and because only one thread can get the synchronization state, the method of setting the head node does not require a CAS guarantee, only the head node is set to the successor node of the original node and the next reference to the original header is broken. For example, set the first node:

Figure 3: Settings for the first node

6. Acquisition and release of an exclusive lock:

  

Juc Review of the implementation principle of-aqs Synchronizer

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.