In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is AQS and ReentrantLock". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is AQS, ReentrantLock".
(1) Overview of AQS
The core of Java concurrent programming is the java.concurrent.util package. The implementation of most synchronizers in juc revolves around a common behavior, such as waiting queue, conditional queue, exclusive acquisition, shared acquisition and so on. The abstraction of this behavior is based on AbstractQueuedSynchronized (AQS). AQS defines a synchronizer framework for multithreaded access to shared resources.
In a nutshell, AQS is like a code of conduct under which most synchronizers in concurrent packages are implemented.
AQS has the following features: blocking waiting queue, sharing / exclusive, fair / unfair, reentrant, and allow interrupts.
If you click on the JUC source code, you will find a large number of synchronizer implementations, such as: Lock, Latch, Barrier and so on are all based on AQS implementation.
(2) several important knowledge points
In AQS, we need to remember a few important knowledge points:
1. The implementation of AQS usually defines the inner class Sync inheriting AQS, which maps all calls of the synchronizer to the corresponding methods of Sync.
2. There is an attribute inside the AQS called state, which represents the available status of the resource. There are three access methods for state: getState (), setState () and compareAndSetState ().
3. AQS defines two ways to share resources: Exclusive, such as ReentrantLock, and Share, such as Semaphore or CountDownLatch.
4. A synchronous waiting queue is defined in AQS, which is used to store a queue of waiting threads.
These knowledge points will be used in later content.
(3) ReentrantLock
Let's learn more about AQS through the example of ReentrantLock. I will explain the use of AQS in ReentrantLock through the above four knowledge points.
1. First go inside the source code of ReentrantLock, and you can directly see the inner class Sync defined in ReentrantLock.
Sync inherits AQS and presses AQS to specify synchronization rules.
2. Since inheriting AQS,ReentrantLock is equivalent to having state, this state is used to record the number of locks. ReentrantLock is a reentrant lock. If it is locked multiple times, state will record the number of locks, and the lock needs to be released the same number of times before the lock is released.
3. The resources of ReentrantLock are exclusive, and AbstractQueuedSynchronized inherits an abstract class called AbstractOwnableSynchronizer:
In this class, there is a variable called exclusiveOwnerThread, which records which thread currently owns the lock.
4. Synchronous waiting queue: because ReentrantLock is an exclusive lock, when one thread is using this lock, other threads have to wait in the queue. This queue is a queue based on a two-way linked list (CLH-like queue). Thread information is stored in the node.
(4) reentrant lock
In introducing AQS, we mentioned that there is a status value in AQS, state, which is used to determine the available state of the current resource. A reentrant lock means that an object can be locked multiple times, and state is used to record the number of locks. Write a piece of code below:
Public class ReentrantLockTest {/ / defines the global lock object private static final Lock lock=new ReentrantLock (true); public static int count=0; public static void main (String [] args) {new Thread (new Runnable () {@ Override public void run () {testlock ();}}, "thread A") .start () New Thread (new Runnable () {@ Override public void run () {testlock ();}}, "Thread B") .start ();} private static void testlock () {lock.lock (); count++; System.out.println (Thread.currentThread (). GetName () + "lock for the first time" + count) Lock.lock (); count++; System.out.println (Thread.currentThread (). GetName () + "second lock" + count); count--; lock.unlock (); System.out.println (Thread.currentThread (). GetName () + "first unlock" + count); count--; lock.unlock () System.out.println (Thread.currentThread (). GetName () + "second unlock" + count);}}
Generate two threads, let them execute the testlock method, and then lock the beginning and end of the testlock method to ensure that only one thread can execute the method at the same time. The end result is thread execution in an orderly manner:
In the code, we lock twice, which is the reentrant lock. We use breakpoint debugging to analyze the value in the lock after the second lock, which is explained below.
(5) Fair lock and unfair lock
When we create a ReentrantLock with a constructor, we can pass in a parameter of type boolean, true or false
Private static final Lock lock=new ReentrantLock (true)
The true and false here represent whether the created ReentrantLock object is a fair lock or an unfair lock
Thank you for your reading, the above is the content of "what is AQS, ReentrantLock", after the study of this article, I believe you have a deeper understanding of what is AQS, ReentrantLock, the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.