Objective
Futuretask in Java is well known as a task that can be executed asynchronously and can obtain execution results. You can usually use Future.get () to get the results of a thread's execution, and until the end of threads execution, the Get method blocks until call () returns, with the advantage that the thread's execution results can also be obtained by using a thread to perform the task asynchronously. But Futuretask's functionality is based on a class called Abstractqueuedsynchronizer, at least in JDK 1.5, JDK1.6 version (starting at 1.7 Futuretask has been written by its author Doug Lea was modified to no longer rely on abstractqueuedsynchronizer implementation, which is one of the JDK1.7 changes. But Abstractqueuedsynchronizer also has many subclasses in JDK1.8 as shown in the following illustration:
The tools in these JDK are more or less used more than once, such as Reentrantlock, we know that Reentrantlock's function is to implement concurrent access control for code snippets, which is the usual meaning of locks, Before you see Abstractqueuedsynchronizer, you might think that its implementation is done by locking objects like synchronized. But in fact it's just a tool class! No more "advanced" machine instructions, not keywords, and do not rely on the JDK compile-time special processing, just as an ordinary class on the completion of the code block concurrent access control, which makes it more doubtful how it implements the code block concurrent access control. So let's take a closer look at how Doug Lea implements this lock. For convenience, this article uses Aqs instead of Abstractqueuedsynchronizer.
In detail Aqs
Before delving into the AQS, I want to start with the Aqs of the function of the Aqs, standing on the user's point of view, Aqs functions can be divided into two categories: exclusive and sharing functions, all of its subclasses, either implement and use its exclusive function of the API, or use the function of shared locks, Instead of using two sets of APIs at the same time, even its most famous subclass Reentrantreadwritelock is implemented through two internal classes: Read locks and write locks, two sets of APIs implemented separately, why do that, and then we analyze, so far, We just need to understand that AQS has exclusive control and shared control functions.
Exclusive lock
Before the real interpretation of Aqs, I would like to start from the use of its exclusive control function of the subclass Reentrantlock, analysis of the reentrantlock at the same time look at the realization of AQS, and then deduce Aqs unique design ideas and ways to achieve. Finally, the realization of the shared control function is looked at.
For Reentrantlock, used students should know, usually so with it:
Reentrantlock.lock ()
//do something
reentrantlock.unlock ()
Reentrantlock will guarantee that do something at the same time only one thread in the execution of this code, or that the same time only one thread lock method will return. The remaining threads are suspended until the lock is acquired. As you can see from here, in fact, Reentrantlock is the function of an exclusive lock: There is and only one thread acquires the lock, all the remaining threads hang until the thread that owns the lock releases the lock, and the suspended thread is awakened to start the competition lock again. Yes, Reentrantlock is using AQS's exclusive API implementations.
So now let's start with the implementation of Reentrantlock to see how the lock is implemented.
First look at the Lock method:
Like Futuretask (JDK1.6), Reentrantlock has a proxy class to complete the operation, Reentrantlock just encapsulates a unified set of APIs. It is noteworthy that the use of Reentrantlock students should know that Reentrantlock is divided into fair and unfair locks, so, reentrantlock internal only two sync implementation: