[Java concurrent programming practice] ----- "J. U. C": AQS Analysis (2): Getting and Releasing locks
The previous blog briefly introduced AQS. Next we will take a look at the AQS acquisition and lock release.
Obtain the AQS lock
AQS includes the following methods:
Acquire (int arg): obtains objects in exclusive mode and ignores interruptions.
AcquireInterruptibly (int arg): obtains the object in exclusive mode. if the object is interrupted, it is aborted.
AcquireShared (int arg): obtains objects in Shared Mode and ignores interruptions.
AcquireSharedInterruptibly (int arg) obtains the object in shared mode. if the object is interrupted, it is aborted.
TryAcquire (int arg): attempts to obtain the object state in exclusive mode.
TryAcquireNanos (int arg, long nanosTimeout.
TryAcquireShared (int arg): attempts to obtain the object state in shared mode.
TryAcquireSharedNanos (int arg, long nanosTimeout.
For lock. lock (), the acquire () method of AQS will be called. Semaphore. acquire () will call the acquireSharedInterruptibly () method of AQS. The source code of acquire () is as follows:
public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
TryAcquire: attempts to obtain the lock. If the acquisition succeeds, the lock status is set and true is returned. Otherwise, false is returned.
AddWaiter: adds the current thread to the end of the CLH queue.
AcquireQueued: The current thread will block and wait Based on the fairness principle until the lock is obtained. It also returns whether the current thread has been interrupted while waiting.
SelfInterrupt: an interruption occurs.
The main process is as follows:
1. First, the thread tries to obtain the lock. If the lock is successful, it returns directly. If the lock fails, a new Node is created and added to the CLH queue. TryAcquire tries to get the lock. addWaiter creates a node and adds it to the CLH queue. TryAcquire and AQS do not provide implementation. It only throws an exception. The specific implementation requires the implementation of each lock.
protected boolean tryAcquire(int arg) { throw new UnsupportedOperationException(); }
AddWaiter is described later.
2. The main function of acquireQueued is to find the header node of the CLH Queue Based on the node, and try to obtain the lock to determine whether to suspend and return the suspension ID. As follows:
final boolean acquireQueued(final Node node, int arg) { try { boolean interrupted = false; for (;;) { final Node p = node.predecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GCreturn interrupted; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } catch (RuntimeException ex) { cancelAcquire(node); throw ex; } }
Within acquireQueued (), tryAcquire () is still called to obtain the lock. For more details, see [Java concurrent programming practices] -- "J. U. C": ReentrantLock 2 lock Method Analysis
SelfInterrupt: an interruption occurs. If the current thread is interrupted in acquireQueued (), an interruption is required.
private static void selfInterrupt() { Thread.currentThread().interrupt();}
AQS lock release
AQS release locks mainly by the following methods:
Release (int arg): releases an object in exclusive mode.
ReleaseShared (int arg): releases an object in shared mode.
TryRelease (int arg): attempts to set the status to reflect a release in exclusive mode.
TryReleaseShared (int arg): attempts to set the status to reflect a release in shared mode.
Releasing a lock is relatively simple compared to obtaining a lock. The main process is as follows:
The Code is as follows (release ()):
public final boolean release(int arg) { if (tryRelease(arg)) { Node h = head; if (h != null && h.waitStatus != 0) unparkSuccessor(h); return true; } return false; }
TryeRelease (): tries to release the lock, and AQS does not provide implementation. The specific implementation method must be implemented internally by its subclass. AQS only throws an exception.
protected boolean tryRelease(int arg) { throw new UnsupportedOperationException(); }
UnparkSuccessor: used to wake up a node. For more information, see [Java concurrent programming practices] -- "J. U. C": ReentrantLock 3 unlock Method Analysis