AQS source code tracking in Reentrantlock (not finished)

AbstractQueuedSynchronizer (AQS), the subclass is Sync, and the subclass of Sync has two implementations: NonfairSync and FairSync. lock() .lock() of NofairSync object final void lock() {<!– –> //state0 represents not locked //1 represents locking if (compareAndSetState(0, 1)) //The first cas lock is successful setExclusiveOwnerThread(Thread.currentThread()); //Set the thread in the lock to the current thread else […]

Understanding the AQS framework (taken from jdk 17.0.7)

Note: In this article, I will try my best to explain the AQS framework in an easy-to-understand manner to help you understand, but I still have to say that this article is not suitable for beginners with zero basic knowledge. Foreword The full name of AQS (AbstractQueuedSynchronizer), this class is under the java.util.concurrent.locks package. AQS […]

Java concurrent AQS understanding

I recently reviewed the knowledge related to AQS in concurrency. I saw a better article here. I will reprint it and record it. Reprinted from: Java AQS core data structure-CLH lock In concurrent programming, locks are a commonly used method to ensure thread safety. There are two main types of locks commonly used in Java. […]

AQS exclusive mode acquisition lock

acquire and tryAcquire public final void acquire(int arg) { //Try to get resources if (!tryAcquire(arg) & amp; & amp; acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); } In the acquire method of AQS, tryAcquire is first called. However, tryAcquire is not implemented in AQS, but an exception is thrown, so it is implemented by its subclass. protected boolean tryAcquire(int […]

Java Core 05-AQS&ReentrantLock&Synchronized and other lock principles

AQS(AbstractQueueSynchronizer) Overview of AQS principles AQS means abstract queue synchronizer. Lock and other concurrency tool classes under the JUC package are implemented based on it. AQS maintains a volatile-modified state variable and a CLH (FIFO) two-way queue. The core idea of AQS is that if the requested shared resource is idle, the thread currently requesting […]

AQS Condition queue source code analysis

Usage of Condition The previous article analyzed the process of ReentrantLock grabbing locks, threads joining the queue, and releasing locks. This article then looks at the application of conditional queues. Condition in AbstractQueuedSynchronizer represents a condition queue. We can implement thread communication through Condition. We want to suspend threads and wake them up when certain […]

Explore Abstract Synchronous Queue AQS

Directory navigation Preface 1. Principle 1.1 CLH Queue 1.2 Thread synchronization 2. Resource acquisition and release 2.1 Exclusive 2.2 Shared 3. Implement custom synchronizer based on AQS 4. Reference materials by emanjusaka from ? https://www.emanjusaka.top/archives/8 What can I do if the flowers on the other side bloom? This article welcomes sharing and aggregation. Please leave […]

Java’s thread synchronization mechanism AQS

A meaningful beginning is always more sunny than meaningless waiting. First, distinguish two concepts, waiting queue and synchronization queue. Both of these are guarantees for the security of thread communication in a queue manner. Before understanding them, you need to know some important objects, LockSupport, ReentrantLock, Sync, AbstractQueuedSynchronizer, and the roles they play. Only by […]

Aqs exclusive/shared mode

The concepts of exclusive locks and shared locks Exclusive lock Also called exclusive lock, it means that the lock can only be held by one thread at a time. If thread T adds an exclusive lock to data A, other threads cannot add any type of lock to A. A thread that obtains an exclusive […]