Aqs-based Locks

Source: Internet
Author: User
Tags cas

Locks are divided into exclusive and shared locks, and their main implementations are dependent on Abstractqueuedsynchronizer, which provides only a series of common methods for subclasses to invoke. Based on my understanding of the class, from the properties of this class, methods, and the acquisition of exclusive locks to understand this kind.

Main properties and methods of Abstractqueuedsynchronizer:

Properties/Methods Meaning
Thread Exclusiveownerthread This is the property of the Aqs parent class Abstractownablesynchronizer, which represents the current owner of the exclusive mode Synchronizer
Node As already described above, the basic unit of the FIFO queue
Node Head Header node in FIFO queue
Node Tail The tail node in the FIFO queue
int state Sync status, 0 means unlocked
int GetState () Get synchronization Status
setState (int newstate) Set synchronization Status
Boolean compareandsetstate (int expect, int update) Using CAs to set state
Long Spinfortimeoutthreshold = 1000L Time of thread spin wait
Node Enq (final node node) Inserting a node into the FIFO queue
Node Addwaiter (node mode) Creates and expands a wait queue for the current thread and the specified pattern
void Sethead (Node node) Set the head node of the queue
void Unparksuccessor (Node node) Invoke node-held thread if it exists
void Doreleaseshared () Action to do a release lock in shared mode
void Cancelacquire (Node node) Attempt to cancel an in-progress node acquisition lock
Boolean Shouldparkafterfailedacquire (node pred, node node) Whether the current thread should be disabled after attempting to acquire a lock and wait for
void Selfinterrupt () Interrupts the current thread itself
Boolean parkandcheckinterrupt () Disables the current thread from entering the wait state and interrupting the thread itself
Boolean acquirequeued (Final node node, int arg) Threads in the queue acquire locks
Tryacquire (int arg) Attempt to acquire a lock ( implemented by Aqs subclasses )
tryrelease (int arg) Attempt to release the lock ( implemented by Aqs subclasses )
Isheldexclusively () Whether to hold the lock alone
Acquire (int arg) Get lock
Release (int arg) Release lock
Compareandsethead (Node Update) Using CAs to set head node
Compareandsettail (node expect, node update) Using CAs to set up tail node
Compareandsetwaitstatus (node node, int expect, int update) Use CAs to set the wait state in a node

It can be seen that this class is a linked list structure, with head and Tail,next, its synchronization state is represented by the type of int, 0 means not acquired, 1 for fetch, greater than 1 for the number of reentrant, compareandsetstate (int expect,int Update) and Spinfortimeoutthreshold are guaranteed to be fast and unique when inserting nodes into a queue under multiple threads. Compareandsetstate and SetState are set the value of state, you can see the source code, found that Compareandsetstate is mainly to obtain locks, multi-threaded competition when used, and setState in the lock when the use of re-entry, Have the function of biased locking.

Aqs is a FIFO-based implementation of the queue, with a node's inner class, the main attributes are as follows:

Properties Custom
Node SHARED = new Node () Indicates that node is in shared mode
Node EXCLUSIVE = null Indicates that node is in exclusive mode
int CANCELLED = 1 node is set to cancel status because of timeout or interruption, the canceled node should not go to competition lock, can only keep the cancellation state unchanged, cannot be converted to other State, node in this state will be kicked out of the queue, collected by GC
int SIGNAL =-1 Indicates that node's successor node is blocked and needs to be notified.
int CONDITION =-2 Indicates that node is in the conditional queue because it is blocked waiting for a condition
int PROPAGATE =-3 Use in Shared Mode header node may be in this state, indicating that the next acquisition of a lock can be transmitted unconditionally
int Waitstatus 0, new node will be in this state
Node prev The predecessor node of a node in the queue
Node Next The successor node of a node in the queue
Thread thread This node holds the thread that represents the thread waiting for the lock
Node Nextwaiter Represents the next node waiting for condition

The status of acquiring an exclusive lock is as follows:

Aqs-based Locks

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.