Mastering Java ReentrantLock: Synchronization, Communication, and Fairness

This article explains how Java's Lock interface and its ReentrantLock implementation provide flexible thread synchronization, inter‑thread communication via Condition objects, and the distinction between fair and non‑fair locking, complemented by illustrative code screenshots.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Mastering Java ReentrantLock: Synchronization, Communication, and Fairness

Lock Object Overview

The Lock interface, introduced after JDK 1.5, has common implementations such as ReentrantLock; these are collectively referred to as Lock objects.

ReentrantLock for Thread Synchronization

ReentrantLock can achieve the same mutual exclusion as the synchronized keyword while offering additional flexible features.

Running the example shows that a thread releases the lock after printing, allowing other threads to acquire it; the printed groups are ordered because the lock is held during each thread's output, though the overall order is random.

Another test demonstrates ReentrantLock’s ability to synchronize threads using lock.lock().

Result shows that while a thread holds the lock, other threads wait, mirroring synchronized behavior.

Using Lock for Thread Communication

Beyond synchronization, ReentrantLock enables thread communication via Condition objects, which replace the wait/notify pattern used with synchronized.

Condition creation example:

Conditions allow selective notification, unlike synchronized’s single monitor where notify/notifyAll awaken threads randomly.

Lock and Condition Wait/Notify Example

Key method mappings:

Object.wait() ↔ Condition.await()

Object.notify() ↔ Condition.signal()

Object.notifyAll() ↔ Condition.signalAll()

Example code (illustrated in images) demonstrates acquiring the lock before using Condition methods.

Result confirms correct execution.

Lock with Multiple Conditions Example

Using several Condition objects enables notifying specific groups of threads.

Output shows selective notifications, demonstrating that signal can wake a particular group while signalAll notifies all waiting threads in that group.

Fair and Unfair Locks

A fair lock grants access in FIFO order, while an unfair lock allows threads to acquire the lock opportunistically, leading to random acquisition.

ReentrantLock provides a constructor to specify fairness:

Setting the boolean parameter fair to true creates a fair lock.

Other ReentrantLock Methods

Key methods include:

getHoldCount(): returns how many times the current thread has acquired the lock.

getQueueLength(): estimates the number of threads waiting for the lock.

isFair(): indicates whether the lock is fair.

Further methods follow the same intuitive naming.

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.

JavaconcurrencyLockReentrantLockthread synchronizationCondition
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.