ReadWriteLock and ReentrantReadWriteLock in java - Detailed explanation with full program



In this thread concurrency tutorial we will learn what is java.util.concurrent.locks.ReadWriteLock and java.util.concurrent.locks.ReEntrantReadWriteLock in java with program and examples. Programs to demonstrate usage of readLock() and writeLock() method of ReadWriteLock in java.

Contents of page :
  • 1) What is java.util.concurrent.locks.ReadWriteLock in java?
  • 2) java.util.concurrent.locks.ReadWriteLock interface important methods in java >
    • void readLock()
    • void writeLock()
  • 3) ReentrantReadWriteLock class in java >
  • 4) Program to demonstrate usage of readLock() method of ReadWriteLock in java  >
    • 4.1) Let’s discuss output in detail, to get better understanding of readLock() usage in program in java >
  • 5) Program to demonstrate usage of writeLock() method of ReadWriteLock in java >
    • 5.1) Let’s discuss output in detail, to get better understanding of writeLock() usage in program in java >


1) What is java.util.concurrent.locks.ReadWriteLock in java?
The java.util.concurrent.locks.ReadWriteLock is a  interface and provides methods
  • which allows multiple threads to read shared resource at same time, but
  • only one thread can write to shared resource at same time.

If a thread attempts to acquire the writeLock on shared resource when it is acquired by another thread, then it waits until the lock is released. In this way we achieve synchronization and race conditions is avoided.


2) java.util.concurrent.locks.ReadWriteLock interface important methods >

void readLock()
More than one threads can acquire readLock at a time, provided no other thread is acquiring writeLock at same time in java.

void writeLock()
Only one threads can acquire writeLock at a time. Means writeLock can only be obtained if no other thread is acquiring read or write lock at that time in java.


3) ReentrantReadWriteLock class in java >
java.util.concurrent.locks.ReentrantReadWriteLock is a class which implements ReadWriteLock interface in java.


4) Program to demonstrate usage of readLock() method of ReadWriteLock in java >
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ReadLockTest {
   public static void main(String[] args) {
                 ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
      MyRunnable myRunnable=new MyRunnable(readWriteLock);
      new Thread(myRunnable,"Thread-1").start();
      new Thread(myRunnable,"Thread-2").start();
   }
}
class MyRunnable implements Runnable{
   ReadWriteLock readWriteLock;
   public MyRunnable(ReadWriteLock readWriteLock) {   
      this.readWriteLock=readWriteLock;
   }
  
   public void run(){
  
          /*
          * More than one threads can acquire readLock at a time provided no
          * other thread is acquiring writeLock at same time.
          */
          readWriteLock.readLock().lock();
          System.out.println(Thread.currentThread().getName()
             +" has acquired read lock.");
          try {
                 Thread.sleep(5000);
          } catch (InterruptedException e) {
                 e.printStackTrace();
          }
         
      System.out.println(Thread.currentThread().getName()
                +" has released read lock.");
          readWriteLock.readLock().unlock();
   }
}
/*OUTPUT
Thread-1 has acquired read lock.
Thread-2 has acquired read lock.
Thread-1 has released read lock.
Thread-2 has released read lock.
*/



4.1) Let’s discuss output in detail, to get better understanding of readLock() usage in program in java >
Note : I have mentioned output in green text.


Thread-1 has acquired read lock.
Thread-2 has acquired read lock.
Thread-1 and Thread-2 were able to acquire readLock at same time.

Thread-1 has released read lock.
Thread-2 has released read lock.



5) Program to demonstrate usage of writeLock() method of ReadWriteLock in java >
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class WriteLockTest {
   public static void main(String[] args) {
      ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
      MyRunnable myRunnable=new MyRunnable(readWriteLock);
      new Thread(myRunnable,"Thread-1").start();
      new Thread(myRunnable,"Thread-2").start();        
   }
}
class MyRunnable implements Runnable{
  
   ReadWriteLock readWriteLock;
   public MyRunnable(ReadWriteLock readWriteLock) {   
      this.readWriteLock=readWriteLock;
   }
  
   public void run(){
          /*
          * Only one threads can acquire writeLock at a time. Means writeLock
          * can only be obtained if no other thread is acquiring read or
          * write lock at that time.
          */
          readWriteLock.writeLock().lock();
          System.out.println(Thread.currentThread().getName()
             +" has acquired write lock.");
  
      try {
        Thread.sleep(5000);
        } catch (InterruptedException e) {
        e.printStackTrace();
      }
  
      System.out.println(Thread.currentThread().getName()
                +" has released write lock.");
          readWriteLock.writeLock().unlock();
   }
}
/*OUTPUT
Thread-1 has acquired write lock.
Thread-1 has released write lock.
Thread-2 has acquired write lock.
Thread-2 has released write lock.
*/



5.1) Let’s discuss output in detail, to get better understanding of writeLock() usage in program in java >
Note : I have mentioned output in green text.


Thread-1 has acquired write lock.
Thread-1 has released write lock.
Thread-1 acquired and released writeLock, thread-2 was waiting to acquire writeLock.

Thread-2 has acquired write lock.
Thread-2 has released write lock.
Thread-2 was able to acquire writeLock only when it was released by Thread-1.



Summary >
So in this thread concurrency tutorial we learned about java.util.concurrent.locks.ReadWriteLock and java.util.concurrent.locks.ReEntrantReadWriteLock in java with program and examples. Programs to demonstrate usage of readLock() and writeLock() method of ReadWriteLock in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Locks and ReEntrantLocks in thread concurrency in java


ReentrantLock class provides implementation of Lock’s newCondition() method - description and solving producer consumer program using this method



ReadWriteLock and ReentrantReadWriteLock >

Implementation of custom/own ReadWriteLock and ReentrantReadWriteLock in java



Thread concurrency Interviews >

eEdit
Must read for you :