Showing posts with label Read Write Lock. Show all posts
Showing posts with label Read Write Lock. Show all posts

Sunday, 11 August 2013

Read-write lock Design Pattern

🚀 Master Concurrency & Multithreading in Java!

Subscribe to Ram N Java for clean, visual, and beginner-friendly programming tutorials!

🔔 Subscribe Now

What is the Read-Write Lock Design Pattern?

In multithreaded programming, a standard mutual exclusion lock (like a simple synchronized block) allows only one thread to access a resource at a time—even if all concurrent threads are simply reading data without modifying it. This introduces unnecessary bottlenecks. The Read-Write Lock Pattern (RW Lock) solves this by distinguishing between read and write access.

Core Definition: The Read-Write Lock pattern is a concurrency design pattern that allows concurrent read access to an object by multiple threads, but requires exclusive access for write operations.

The Readers-Writer Concurrency Problem

The classic Readers-Writer Problem illustrates the fundamental challenges of concurrent shared data access:

  • If a thread is writing to data while another thread is reading, the reader may encounter dirty, inconsistent, or partially updated values.
  • If multiple threads try to write simultaneously without coordination, data gets corrupted.
  • However, multiple threads reading the exact same data at the same time cause zero conflict, as long as no write operation takes place.

Real-World Example: File Object Access

Consider a shared File Object in the file system accessed by four threads:

  • 3 Reader Threads (Threads 1, 2, and 3): Read information from the file object.
  • 1 Writer Thread (Thread 4): Writes updates to the file object.

Rule 1: Concurrent Reading is Permitted

All three reader threads can acquire the Read Lock concurrently and read information from the file in parallel without blocking one another.

Rule 2: Writers Wait for Ongoing Readers

If the writer thread (Thread 4) arrives while the reader threads are active, it cannot acquire the lock. The writer is blocked and must wait until all reader threads finish their work.

Rule 3: Writing Requires Exclusive Access

Once the writer thread acquires the Write Lock, no other thread—whether a reader or another writer—can access the file. All incoming reader threads are blocked until the writer completes its write operation and releases the lock.

Java Implementation: ReentrantReadWriteLock

In enterprise Java, this pattern is built right into java.util.concurrent.locks.ReentrantReadWriteLock:

ReadWriteLock rwLock = new ReentrantReadWriteLock();

// Reading concurrently
rwLock.readLock().lock();
try {
    // Read shared data concurrently
} finally {
    rwLock.readLock().unlock();
}

// Writing exclusively
rwLock.writeLock().lock();
try {
    // Modify shared data exclusively
} finally {
    rwLock.writeLock().unlock();
}

Key Advantages of Read-Write Lock

  • Significantly Higher Read Throughput: Maximizes performance in read-heavy applications (such as in-memory caches and catalogs).
  • Guarantees Thread Safety: Enforces strict exclusive access during writes, preventing race conditions and dirty reads.
  • Separation of Concerns: Explicitly separates read operations from mutation logic.

Recommended Concurrency Tutorials:


Click here to watch in Youtube : https://www.youtube.com/watch?v=xaKrYlmRCjM

Click the below Image to Enlarge
Read-write lock Design Pattern















See also:

  • All Design Patterns Links
  • Tutorials