Locks In Java — Part 2 [ Read Write Locks ]

Avinashsoni
4 min readNov 26, 2023

--

In this article we are going to deep dive into the Read — Write lock Interface in Java.

this basically has two methods

it helps in the scenarios where both read and write worker threads are trying to get the access to the critical section.

  • read lock is a shared lock so multiple threads can read the resource concurrently
  • write lock is a exclusive lock so only one writer worker thread can enter the critical section and do updates

READ LOCK

for read lock we have the following condition to get the lock :

  • there should not be any thread with write lock
  • there should not be any thread requesting the lock for write

then only read lock can be acquired and again as mentioned it can be done by multiple threads.

WRITE LOCK

for write lock we have the following condition to get the lock :

  • there should not be any thread already with the write lock
  • there should not be any thread with the read lock (as it will result into the dirty read problem)

let see the implementation of a ReadWriteLock and then we will discuss about the other scenarios

public class ReadWriteLockImpl {
private int reads = 0;
private int writes = 0;
private int write_requests = 0;

// for read lock no write or write request should be there
public synchronized void addReadLock() throws InterruptedException {
while (writes > 0 || write_requests > 0){
wait();
}
reads++;
}


public synchronized void unlockRead(){
reads--;
notifyAll();
}

// for write lock there should not be any reads / writes happening
public synchronized void addWriteLock() throws InterruptedException {
write_requests++;
while (reads > 0 || writes > 0){
wait();
}
write_requests--;
writes++;
}

public synchronized void unlockWriteLock(){
writes--;
notifyAll();
}

}

why we use NotifyAll() ?

notifyAll is used to awaken all the threads instead of notify() because if suppose it awakens a read thread and there are writter threads also waiting then read thread wont be able to get the lock and now in such a situation if a writer thread is awakened then it also wont be able to get the lock as there are read threads waiting.

so we use notifyAll to awaken all the threads so that now it can process in a safe manner.

also write operations are given more priority than the read operations for consistency.

also there is one more advantage of notifyAll() in scenarios where only read threads are there and no write threads so it awakens all of them instead of doing it one by one and all the read threads who were waiting can get the access directly.

we also need to make sure that these locks are allow themselves to be acquired more than once for the same thread as it might happen that

one thread gets a read access and another thread meanwhile do a write request but it is blocked . now the thread — 1 re acquires a read lock but now cant do as there is a write request . so this is a dead lock.

this we will cover once we are through with reentrant locks which basically allow the lock to be reacquired again by the same threads.

lets see one example to show how the read write lock works using the reentrantlock class(as disccused above) which we will deep dive later .

example :

public class ReadWriteExample {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private int sharedResource = 0;

public int readResource() {
// acquring the read lock
lock.readLock().lock();
try {
// Perform read operations on the shared resource
return sharedResource;
} finally {
// read lock released
lock.readLock().unlock();
}
}

public void writeResource(int value) {
// acquring the write lock
lock.writeLock().lock();
try {
// Perform write operations on the shared resource
sharedResource = value;
} finally {
// write lock released
lock.writeLock().unlock();
}
}
}

this completes this article on the read — write locks. in the next part we will be covering the reentrant locks in java and then we will be able to understand everything with much more clarity.

thanks for reading !!

--

--

Avinashsoni

SDE@BNY MELLON | Spring Boot | Learning and Growing EveryDay 😁 Linkedln 👇: https://www.linkedin.com/in/asoni93/