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:
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.