Basic concepts
In this chapter, we will explain the principle of "thread gain fair lock", and we need to understand several basic concepts before we explain it. The following are all based on these concepts; These concepts may be tedious, but from these concepts it is possible to glimpse some of the architecture of the "Java Lock", which is helpful in understanding the lock.
1. Aqs -refers to the Abstractqueuedsynchronizer class.
Aqs is an abstract class that manages "locks" in Java, and many of the public methods of locks are implemented in this class. Aqs are public parent classes that are exclusive locks (for example, Reentrantlock) and shared locks (for example, semaphore).
2. The category of the Aqs lock is divided into " exclusive lock " and " shared lock " two kinds.
(a) exclusive lock --locked at a point in time can only be occupied by a thread lock. According to the lock acquisition mechanism, it is divided into " fair lock " and "unjust lock ". A fair lock is a fair acquisition of a lock in accordance with a first-come-first-served rule through the CLH, rather than a fair lock, which, when the thread is to acquire the lock, ignores the CLH wait queue and acquires the lock directly. The typical real example of an exclusive lock is reentrantlock, in addition, the Reentrantreadwritelock.writelock is also an exclusive lock.
shared Locks -a lock that can be shared by multiple threads at the same time. The Reentrantreadwritelock.readlock, Cyclicbarrier, Countdownlatch, and semaphore in the Juc package are shared locks. The use and principle of these locks are described in detail in later chapters.
3. CLH Queue --Craig, Landin, and Hagersten lock queue
The CLH queue is a thread queue in the Aqs "Wait for lock". In multiple threads, in order to protect competing resources from being manipulated simultaneously by multiple threads, we often need to protect these resources by locking them. In an exclusive lock, a competing resource can only be accessed by one thread lock at a point in time, while the other threads need to wait. CLH is the queue that manages these "waiting locks" threads.
The CLH is a non-blocking FIFO queue. In other words, when inserting or removing a node to the inside, it is not blocked under concurrent conditions, but atomicity is inserted and removed by the spin lock and the CAS guaranteed node.
4. CAs function --Compare and Swap
CAS function, which is the comparison and Exchange function, which is the atomic operation function, that is, the data that is operated through CAS is atomic . For example, Compareandsethead (), Compareandsettail (), Compareandsetnext () and other functions. Their common feature is that the actions performed by these functions are carried out in an atomic manner.
This chapter expands on how to get a lock around "fair lock". "Fair lock" involves more knowledge points, but overall, not particularly difficult, if the reader can read the general meaning of the two classes of Aqs and Reentrantlock.java, understanding the principles and mechanisms of the lock is not a problem. This chapter is just the author of a little understanding of the lock, I hope this part of the knowledge can help you understand the "fair lock" of the acquisition process, understand the "lock" framework.
Reentrantlock Data structure
UML class diagram of Reentrantlock
As you can see from the diagram:
(Reentrantlock) The lock interface is implemented.
Reentrantlock and sync are combinatorial relationships. In Reentrantlock, the Sync object is included, and sync is a subclass of Aqs, and more importantly, Sync has two subclasses Fairsync (fair locks) and Nonfairsync (not fair locks). Reentrantlock is an exclusive lock and whether it is a fair or an unfair lock depends on whether the Sync object is an instance of "Fairsync" or "Nonfairsync".