logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramlinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Global Lock OrderingRead Write SynchronizationReadWriteLock vs synchronized vs ReentrantLock60-Day LLD & Machine Coding Mastery Roadmap
The Four Pillars of OOPComposition, Asociation & AggregationDatabase locking / atomic updates
Don't Repeat Yourself (DRY) PrincipleKeep it Simple, Stupid (KISS) PrincipleYou Aren't Gonna Need It (YAGNI) Principle
Design a Library Management System (LMS)Design a Parking LotVending Machine
D : Dependency InversionI : The Interface Segregation PrincipleL : The Liskov Substitution PrincipleO : The Open/Closed PrincipleS : The Single Responsibility Principle
UML Activity Diagrams: Modeling Process and WorkflowUML Class Diagrams: Static Structure ModelingUML Sequence Diagrams: Modeling Dynamic InteractionsUse Case Diagrams: Functional Requirement Modeling
Low Level Design

Read Write Synchronization

3 min read
On this pageReadWriteLock
ReadWriteLockCompatibility RulesWriter starvationNon-fair mode (default)Fair mode

Java's synchronized provides exclusive locking, when one thread enters a method, it acquires the object's monitor lock.

public synchronized double getBalance() {
    return balance;
}

Another thread attempting to enter a synchronized method or block using the same monitor must wait—even if it only wants to read.

All five operations are reads, they are serialized. It is safe, but it may unnecessarily reduce throughput when reads are frequent.

# ReadWriteLock

Java provides the ReadWriteLock interface, with ReentrantReadWriteLock being its standard implementation.

Lock TypeAccess ModeBehavior
Read LockSharedMultiple readers can hold it simultaneously.
Write LockExclusiveOnly one writer can hold it, with no readers allowed.

# Compatibility Rules

Current lock holderNew readerNew writer
ReaderAllowedWait
WriterWaitWait
No lockAllowedAllowed
 
flowchart TD
    A["ReadWriteLock"]
    A --> B["Reader 1"]
    A --> C["Reader 2"]
    A --> D["Reader 3"]
    A --> E["Reader 4"]
    A --> F["Reader 5"]
    B --> G["Concurrent reads"]
    C --> G
    D --> G
    E --> G
    F --> G
    style G fill:#dcfce7,stroke:#16a34a

The writer cannot acquire the write lock until all existing readers release their read locks. While the writer holds the write lock:

  • New readers cannot enter.
  • Other writers cannot enter.
  • The writer can safely modify shared data.
public class BankAccount {
    private double balance = 10000;
    private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
 
    public double getBalance() {
        rwLock.readLock().lock();
        try {
            return balance;
        } finally {
            rwLock.readLock().unlock();
        }
    }
 
    public void deposit(double amount) {
        rwLock.writeLock().lock();
        try {
            balance += amount;
        } finally {
            rwLock.writeLock().unlock();
        }
    }
}

# Writer starvation

In Java ReentrantReadWriteLock supports two fairness policies:

# Non-fair mode (default)

ReadWriteLock lock =
    new ReentrantReadWriteLock(false);
  • Does not guarantee arrival-order fairness.
  • Generally offers better throughput.
  • A waiting writer may be delayed by competing readers.

# Fair mode

ReadWriteLock lock =
    new ReentrantReadWriteLock(true);
  • Generally favors the longest-waiting thread.
  • Reduces the risk of indefinite starvation.
  • May reduce throughput due to additional scheduling constraints.

# ReadWriteLock vs synchronized vs ReentrantLock

FeaturesynchronizedReentrantLockReentrantReadWriteLock
Exclusive write accessYesYesYes
Multiple concurrent readersNo, with the same monitorNo, with the same lockYes
Explicit lock/unlockNoYesYes
Try lock / timed acquisitionNoYesYes
Fairness optionNoYesYes
ReentrantYesYesYes
ComplexityLowMediumHigher
←PreviousGlobal Lock OrderingNext→ReadWriteLock vs synchronized vs ReentrantLock

On this page

ReadWriteLockCompatibility RulesWriter starvationNon-fair mode (default)Fair mode