Multithreading
ReadWriteLock
Understand read-write separation locks and ReentrantReadWriteLock usage.
Interview: Focuses on write-exclusion vs read-sharing trade-offs, lock degradation/promotion rules.
Standard locks enforce complete mutual exclusion, restricting throughput even when threads are only reading. ReadWriteLock divides access between multiple parallel readers and a single exclusive writer.
Core Idea
ReadWriteLock allows multiple threads to read concurrently, but only one thread to write exclusively.
Why It Matters
Significantly improves concurrent read performance for read-heavy shared data caches.
Interview Lens
Tests lock downgrading (writer acquiring read lock before releasing write lock) and promotional limitations.
Lock Rules & Downgrading
A ReentrantReadWriteLock follows these coordination rules:
- Read Lock: Multiple threads can hold it concurrently, provided no write lock is active.
- Write Lock: Only one thread can hold it. Readers and other writers are blocked.
- Lock Downgrading: A thread holding the write lock can acquire the read lock, then release the write lock. This is safe and keeps the resource visible.
- Lock Promotion (Not Allowed): A thread holding the read lock cannot acquire the write lock directly without first releasing the read lock, as this can cause deadlocks if multiple readers attempt it simultaneously.
Code Walkthrough
This class implements a thread-safe data cache using ReentrantReadWriteLock to allow high-throughput concurrent reads.
import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;public class CacheService { private final Map cache = new HashMap<>(); private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
public String get(String key) { rwLock.readLock().lock(); // Multiple threads can execute this concurrently try { return cache.get(key); } finally { rwLock.readLock().unlock(); } }
public void put(String key, String value) { rwLock.writeLock().lock(); // Exclusive lock: blocks readers and other writers try { cache.put(key, value); } finally { rwLock.writeLock().unlock(); } } }
Interview-Relevant Information
Q: Does ReentrantReadWriteLock prevent writer starvation?
Answer: By default, writers can be starved if readers keep arriving and acquiring read locks continuously. To prevent this, the lock can be configured with a fairness policy (new ReentrantReadWriteLock(true)). This ensures writers are queued in order, and readers block if a writer is waiting in the queue ahead of them.
Quick Checklist
Can multiple readers access state at the same time? What is lock downgrading? Why is lock promotion not allowed? If yes, you understand ReadWriteLock.
Use Cases
Thread-safe routing table updates in networking components.
High-throughput user session caches.
Common Mistakes
Using ReadWriteLock for write-heavy data structures. The overhead of managing read/write queues makes it slower than standard ReentrantLock if writes are frequent.
Attempting lock promotion directly (acquiring write lock while holding read lock), causing permanent deadlocks.