Multi-thread conditional access tool -- AbstractQueuedSynchronizer and synchronizer

Source: Internet
Author: User

Multi-thread conditional access tool -- AbstractQueuedSynchronizer and synchronizer

This article is original. For more information, see the source!

 

References:

"JUC lock 03: fair lock (1)"

"JUC lock" 03: fair lock (II)

 

 

AbstractOwnableSynchronizer, used for sub-classes to access the thread of the exclusive lock.

  • # GetExclusiveOwnerThread ()/# setExclusiveOwnerThread (Thread): gets/sets the Thread to which the exclusive lock belongs.

AbstractQueuedSynchronizer/AbstractQueuedLongSynchronizer is used to manipulate exclusive locks and shared locks. It is used by sub-classes to provide a policy for the thread to obtain and release locks and to define conditions for thread passage. The former is the int type, and the latter is the long type.

 

Status

State is assigned to specific meanings by subclasses. It can be used as a lock state or other meaning.

  • # GetState (): int
    Obtain the status.
  • # SetState (int)
    Set the status.
  • # CompareAndSetState (int, int): boolean
    Attempt to set the status and return whether the setting is successful.

 

Exclusive lock

Related Operations:

  • Acquire (int)
    Obtain the exclusive lock.
  • AcquireInterruptibly (int)
    Obtain the exclusive lock, which can be interrupted.
  • TryAcquireNanos (int, long): boolean
    Obtain the exclusive lock, which can be interrupted and time-out. If true is returned, the operation is successful. If false is returned, the operation fails.
  • # TryAcquire (int): boolean
    The system tries to obtain the exclusive lock and returns whether the lock can be queued or queued for subclass implementation.
  • Release (int): boolean
    Release the exclusive lock and return whether the operation is successful.
  • # TryRelease (int): boolean
    Try to release the exclusive lock and return whether to wake up the next thread for subclass implementation.
  • Collection <Thread> getExclusiveQueuedThreads ()
    Returns all threads in the queue that need to obtain the exclusive lock.
  • # Boolean isHeldExclusively ()
    Check whether the current thread is the thread to which the exclusive lock belongs for subclass implementation.

Procedure:

 

Shared lock

Related Operations:

  • AcquireShared (int)
    Obtain the shared lock.
  • AcquireSharedInterruptibly (int)
    Obtain the shared lock, which can be interrupted.
  • TryAcquireSharedNanos (int, long): boolean
    Obtain the shared lock, which can be interrupted and times out. If the value true is returned, the operation is successful. If the value false is returned, the operation fails.
  • # TryAcquireShared (int): int
    Try to obtain the shared lock. If the returned value is greater than or equal to 0, it indicates that the lock can be queued; otherwise, it indicates that the lock can be queued for subclass implementation.
  • ReleaseShared (int): boolean
    Release the shared lock and return whether the operation is successful.
  • # TryReleaseShared (int): boolean
    Try to release the shared lock and return whether to wake up the next thread for subclass implementation.
  • Collection <Thread> getSharedQueuedThreads ()

    Returns all threads in the queue that need to obtain the shared lock.

Procedure:

 

Queue

Related Operations:

  • Int getQueueLength ()
    Length of the returned queue
  • Boolean hasQueuedThreads ()
    Returns whether a thread exists in the queue.
  • Collection <Thread> getQueuedThreads ()
    Returns all threads in the queue.
  • Collection <Thread> getExclusiveQueuedThreads ()
    Returns all threads in the queue that need to obtain the exclusive lock.
  • Collection <Thread> getSharedQueuedThreads ()
    Returns all threads in the queue that need to obtain the shared lock.
  • Boolean isQueued (Thread)
    Check whether the specified thread is in the queue
  • Boolean hasQueuedPredecessors ()
    Returns whether the current thread is not in the header of the queue. This method is required for the subclass of a fair policy.
  • Thread getFirstQueuedThread ()
    Returns the thread in the queue header.
  • Boolean hasContended ()
    Check whether a thread has ever entered the queue

 

Condition

ConditionObject is an internal class, and its implementation interface is Condition. You can select whether to provide the creation function for the subclass implementation.

Related Operations:

  • Boolean owns (ConditionObject)
    Check whether the target condition object belongs to the AbstractQueuedSynchronizer object.
  • Boolean hasWaiters (ConditionObject)
    Check whether there is a thread waiting for the target condition
  • Int getWaitingThreads (ConditionObject)
    View the number of threads waiting for the target condition
  • Collection <Thread> getWaitQueueLength (ConditionObject)
    Returns all threads waiting for the target condition.

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.