Fundamentals 12 min read

How Java’s AQS Powers ReentrantLock: Deep Dive into Lock Implementation

This article explores Java’s Lock interface and its underlying implementation, detailing the role of AbstractQueuedSynchronizer (AQS) in building ReentrantLock, the internal class hierarchy, synchronization mechanisms, and provides a step‑by‑step guide to creating a custom lock using AQS.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How Java’s AQS Powers ReentrantLock: Deep Dive into Lock Implementation

1. Lock Interface

In a previous article we introduced using Lock objects to achieve synchronization similar to the synchronized keyword, but with explicit lock acquisition and release, offering features such as interruptible and timed lock acquisition.

Lock enables complex acquisition sequences (e.g., acquire A, then B, release A, acquire C, etc.) that would be cumbersome and error‑prone with synchronized.

The following diagram shows the location of lock‑related classes in the Java 8 java.util.concurrent.locks package.

The inheritance relationship among these classes is illustrated below.

The primary implementation of Lock is ReentrantLock, which relies on AbstractQueuedSynchronizer (AQS) as its core synchronization framework.

2. AbstractQueuedSynchronizer (AQS)

AQS is a framework for building locks and other synchronizers. It maintains an int state variable and a built‑in FIFO queue to manage threads waiting for the lock.

Two key aspects are exposed:

Understanding the current synchronization state of a shared variable.

Queueing threads that cannot acquire the lock, using the internal FIFO queue.

Subclasses extend AQS and override its abstract methods to manipulate the synchronization state. The three core methods used by subclasses are getState(), setState(), and compareAndSetState().

AQS supports both exclusive and shared acquisition modes, enabling implementations such as ReentrantLock, ReentrantReadWriteLock, and CountDownLatch.

AQS defines several categories of methods:

protected methods that subclasses may override (e.g., tryAcquire, tryRelease).

public final template methods that provide the external API (e.g., acquire, release).

protected final methods for low‑level state manipulation.

AQS also contains two important inner classes: ConditionObject (implementing Condition) and Node, which represents a thread waiting in the queue.

3. ReentrantLock Design and Implementation

The class diagram of ReentrantLock shows three static inner classes: Sync, NonfairSync, and FairSync. Sync extends AbstractQueuedSynchronizer, while the two concrete subclasses implement the lock acquisition policy.

Sync

overrides only tryRelease (and tryAcquire in the concrete subclasses). FairSync provides a fair acquisition algorithm by overriding tryAcquire and delegating to the AQS acquire method.

4. Building a Custom Lock with AQS

To create a custom lock, implement the Lock interface and define an inner class that extends AbstractQueuedSynchronizer, overriding the necessary methods ( tryAcquire, tryRelease, etc.). The following schematic shows the implementation steps.

A simple test case demonstrates that the custom lock behaves like ReentrantLock (though it lacks the non‑fair and fair variants).

Adding a fair lock variant would follow the same pattern as the standard ReentrantLock implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaconcurrencyLockAQSReentrantLockCustomLock
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.