Before directly moving to the ReentrantReadWrite Lock, first, let’s understand what the ReadWrite Lock is in Java. The ReadWrite Lock is responsible for allowing multiple threads to read the resources but only one thread will write the resource at a time. This eventually resolves the concurrency problem that may arise due to the threads writing over the same resource. If both the write and read operations are being performed in parallel the concurrency error may arise.
In this article, we will discuss the implementation of ReentrantReadWrite Lock in Java.
How to Use/Implement ReentrantReadWriteLock Class in Java
The ReentrantReadWrite Lock in Java belongs to the “java.util.concurrent.locks” package. It is implemented through the ReadWrite Lock. The ReentrantReadWriteLock has been found beneficial in the case where the read/write operations are performed in a recursive manner.
The syntax of ReentrantReadWriteLock is depicted below
private static final ReentrantReadWriteLock locks = new ReentrantReadWriteLock(true);
Example: Implementation of ReentrantReadWriteLock
The code below depicts in detail the implementation of the ReentrantReadWrite Lock class of Java.
//Import the package
import java.util.concurrent.locks.ReentrantReadWriteLock;
//Create a class for reentrantreadwritelock
class reentrantreadwritelock{
private static final ReentrantReadWriteLock locks = new ReentrantReadWriteLock(true);
// Pass a value in the string
private static String input = "Hi";
//Exception handling
public static void main(String[] args) throws InterruptedException{
//Create threads for reading and writing.
Thread th1 = new Thread(new Readip());
Thread th2 = new Thread(new Firstwrite());
//The threads will start working
th1.start();
th2.start();
th1.join();
th2.join();
}
//The Readip class implements the Runnable Interface
static class Readip implements Runnable {
//The run() method of Runnable Interface
public void run() {
for(int j = 0; j<= 6; j ++) {
//when the write block is owned by any thread.
if(locks.isWriteLocked()) {
System.out.println("Fetch the lock from Write");
}
//The output statement for the readlock().
locks.readLock().lock();
System.out.println("Read the Thread " + Thread.currentThread().getId() + " ----> The output is " + input );
locks.readLock().unlock();
}
}
}
//The Runnable interface of Java
static class Firstwrite implements Runnable {
//The run() method of Runnable interface.
public void run() {
for(int j = 0; j<= 6; j ++) {
//To check for any exceptions
try {
locks.writeLock().lock();
input = input.concat(" Mr");
} finally {
locks.writeLock().unlock();
}
}
}
}
}
In the above code:
- A package of the ReentrantReadWrite Lock class is imported.
- A string “Hi” is passed for the execution.
- Two threads are declared to execute the specified task.
- The first thread is allotted the read operation.
- The second one is allotted the write operation.
- The threads start working by the “.start()” method.
- The run() method of the Runnable Interface is implemented to ensure the concurrent execution.
- The locks.readLock().lock() is responsible for acquiring the read lock.
- The print statement prints the currently running thread with the string that is entered.
- The locks.readLock().unlock() has now released the read lock.
- Now the write lock will work similarly to the read lock.
- The string “Mr” prints when the write lock is locked.
- The write lock gets unlocked now.
Output
In the below output the thread currently running, along with the string that entered the read operation string has been printed. Afterward, the string from the write operation has been printed. Both the strings have been printed in concatenated form.

This sums up the implementation of ReentrantReadWrite lock in Java.
Conclusion
The ReentrantReadWriteLock class in Java belongs to the instance of ReadWrite Lock. The ReadWrite Lock is responsible for performing either the read or write operation. Request can occur from multiple threads if there are currently no requests for write present. For a write request neither read nor write thread can be acquired. In this write-up, we have demonstrated in detail the implementation of Reentrant ReadWriteLock in Java.