Mastering ReentrantReadWriteLock: Deep Dive into Java’s Read‑Write Lock Mechanics
This article explains the structure, features, and internal workings of Java's ReentrantReadWriteLock, compares it with ReentrantLock and synchronized, provides usage examples, analyzes state management with bitwise operations, and covers lock acquisition, release, downgrade, and the role of LockSupport.
1. Introduction
Previous articles introduced ReentrantLock and ReentrantReadWriteLock. ReentrantLock is a re‑entrant exclusive lock whose AQS state is 0 when unlocked and greater than 0 when the owning thread has re‑entered.
synchronized also supports re‑entrancy but is implicit compared to ReentrantLock. ReentrantLock can be fair or non‑fair; the default is non‑fair to reduce context switches.
This article focuses on the read‑write lock ReentrantReadWriteLock.
2. Overview of ReentrantReadWriteLock
ReentrantReadWriteLock consists of two inner classes, ReadLock and WriteLock, both implementing the Lock interface. By separating read and write locks, concurrency improves compared to a single exclusive lock.
Key features:
Structure and inner classes are illustrated in the following diagrams:
ReadWriteLock interface provides readLock() and writeLock() factory methods, following the Factory Method pattern.
3. Usage Example
Example using a HashMap‑based cache demonstrates how to acquire read and write locks.
4. Internal Mechanics
4.1 State Design
ReentrantLock uses a single integer state; ReentrantReadWriteLock splits this integer into high 16 bits for read count and low 16 bits for write count, using bitwise operations to manage both simultaneously.
4.2 Write Lock Acquisition and Release
The write lock is exclusive and re‑entrant. If a thread already holds the write lock, the state is incremented; otherwise the thread waits if the read count is non‑zero or another thread holds the write lock.
4.3 Read Lock Acquisition and Release
The read lock is shared and re‑entrant. It succeeds when the write count is zero; otherwise the thread waits.
4.4 Lock Downgrade
Lock downgrade allows a thread holding the write lock to acquire the read lock before releasing the write lock, ensuring data visibility for other threads. Upgrading from read to write lock is not allowed.
4.5 Downgrade Example
5. Brief Introduction to LockSupport
Both ReentrantLock and ReentrantReadWriteLock use LockSupport to block and unblock threads. Methods prefixed with park block a thread, while unpark methods wake it.
6. Summary
1. Read locks are re‑entrant and can be held by multiple threads; write locks are exclusive but also re‑entrant.
2. A thread holding the write lock can acquire the read lock without releasing the write lock (lock downgrade).
3. After fully releasing the write lock, the thread becomes a pure read‑lock holder; further write attempts must reacquire the write lock.
4. In fair mode, lock acquisition follows FIFO order; in non‑fair mode, read threads may bypass waiting writers under certain conditions.
5. Read locks cannot create Condition objects, while write locks support newCondition() like ReentrantLock.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
