AQS profile
AQS is an abstract class that cannot be instantiated, and is designed to allow subclasses to implement a variety of functions through inheritance. It internally provides a FIFO wait queue for multiple threads waiting for an event (lock). It has an important state flag, state, which is an int value representing the current state of the object (for example, 0 for lock, 1 for UNLOCK). AQS provides three protected final methods to change state: getState, setState(int), and compareAndSetState(int, int). According to the modifiers, they cannot be overridden by subclasses, but can be called in subclasses, which means that subclasses can decide how to use state based on their own logic.
Subclasses of AQS should be defined as internal classes that act as internal helper objects. In fact, this is also the way JUC locks, such as ReentrantLock, inherit AQS from internal Sync objects. Some unimplemented methods defined in AQS (throw UnsupportedOperationException)
TryAcquire (int) attempted to obtain state
TryRelease (int) attempts to release state
TryAcquireShared (int) Shared mode try to get
TryReleaseShared (int) Shared mode attempted release
IsHeldExclusively () determines whether the current lock is an exclusive lock
These methods are what subclasses need to implement, and you can choose to implement some of them. According to different implementation methods, it can be divided into two types: exclusive lock and shared lock. The lock classification in JUC is as follows:
An exclusive lock: already, ReentrantReadWriteLock. WriteLock
A Shared lock: ReentrantReadWriteLock ReadLock, CountDownLatch, CyclicBarrier, Semaphore
Its implementation is as follows:
Exclusive lock implementation tryAcquire(int), tryRelease(int)
TryAcquireShared (int), tryReleaseShared(int)
There is also an internal class ConditionObject provided in AQS that implements the Condition interface and can be used with await/signal. Using CLH queue algorithm, wake up the thread corresponding to the next node of the current thread, and signalAll wake up all threads.
In general, AQS provides three functions:
Implementing an exclusive lock
Implementing shared locks
Implementing the Condition model
AQS popular examples:
If you don’t quite understand what AQS can do, take a look at the next analogy, in which we personify AQS and threading collaboration tool classes to HR and interviewers.
Here is a simulation of the scene of candidates attending school recruitment interview. For companies, interviews usually involve the interviewer and HR. There are usually two kinds of interviews, one is group interview, one is single interview, group interview refers to the interview of many students together, for example, the rule is to interview 10 people together, the group interview rule is to gather 10 people first, and then unified interview.
One-on-one interviews are often assembly-line, one-on-one interviews. Suppose we have a total of 5 interviewers for a single interview, that is, these 5 interviewers interview a candidate at the same time, during the interview, candidates will queue up, after the front candidate finishes the interview, the back candidate will follow, looking for a spare interviewer to start the interview, this is the single interview scenario.
At first glance, the rules of group and single interview are very different: the former involves a group of people, while the latter involves individual interviews. But in fact, there are many similarities (or processes or links) between group and single side, and these similarities are often handled by HR. Such as the interviewer has come, HR needs to arrange candidate check-ins, sat waiting, waiting, then HR will cry in sequence, so as to avoid conflict of multiple candidate, at the same time, HR must wait for the classmates can eventually was called, the HR is responsible for all the series of content, and the content of both single and group of face are the same. What these HR people do in the interview can be compared to the work of AQS. As for specific interview rules, for example, is the group interview rule 5 or 10 people together? Is it a single side or a group? These are arranged by the interviewer. As for the interviewer, he doesn’t care about whether the candidate has a conflict of numbers, how to wait, how to call, whether there is a place to rest, etc., because these are the responsibilities of HR. Here the interviewer is using AQS to implement specific collaboration logic, and HR stands for AQS. To let the candidate rest means to block the thread so that it does not continue to consume CPU. A follow-up call to the candidate for an interview means waking up the thread. The group latch process is similar to CountDownLatch. CountDownLatch sets an initial value that needs to be counted backwards, which is assumed to be 10. For each candidate, the count decreases by one. Similarly, one side can be understood as a Semaphore Semaphore, assuming that there are five licenses, each thread getting one license at a time, which is similar to having five interviewers interviewing in parallel. Candidates need to obtain licenses before the interview and return licenses after the interview. For utility classes like CountDownLatch and Semaphore, its “VIP” rule is to get 10 candidates together for an interview, like a group interview? Or do you go out one and in one, like one side? Once the rules are established, the rest of the work, such as greeting candidates (analogous to scheduling a thread), can be left to AQS, so that their responsibilities are very separate and distinct.
Conclusion:
learn the idea of AQS, why need AQS, and the role of AQS, using AQS can be very convenient to achieve thread collaboration tool class, and AQS is widely used in JUC package.
The above is just a brief introduction to AQS. For a more comprehensive and in-depth explanation of AQS, please watch the live course of “AQS”. If you are interested in this live broadcast, please scan the qr code below
Long press the scan code 👆, lock 0 yuan quota for free access to AQS course materials and live links! First 50 people only, first come, first served!