Implementation of custom/own ReadWriteLock and ReentrantReadWriteLock in java - Detailed explanation with full program




Contents of page :
  • 1) How to implement custom ReadWriteLock in java?
  • 2) ReadWriteLock interface important methods in java >
    • void readLock()
      • CODE in thread concurrency in java >
    • void writeLock()
      • CODE in thread concurrency in java >
  • 3) ReentrantReadWriteLock class in java >
  • 4) Custom ReadWriteLock  and ReentrantReadWriteLock’s code in java >


1) How to implement Custom ReadWriteLock in java?
In previous tutorial we read how to use ReadWriteLock and ReentrantReadWriteLock in java. In this thread concurrency tutorial we will be implementing custom ReadWriteLock. This thread concurrency tutorial intends you give you basic functionality of ReadWriteLock using your own java code.


custom ReadWriteLock is a  interface, it provides following methods in java >
  • which allows multiple threads to read shared resource at same time, but
  • only one thread can write to shared resource at same time in java.


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) ReadWriteLock interface important methods in java >


void readLock()
More than one threads can acquire readLock at a time, provided no other thread is acquiring writeLock at same time.
CODE in thread concurrency in java >
ReadLock has been created as Inner class which provides readlock.
public class ReadLock{
          public synchronized void lock() {
                 /*
                 * More than one threads can acquire readLock at a time, provided
                 * no other thread is acquiring writeLock at same time.
                 */
                 if(writeLockCount==0){
                       readLockCount++;
                 }
                 /*
                 * if some other thread is
                 * acquiring write lock at that time,
                 * than current thread waits.
                 */
                 else{
                       try {
                              wait();
                       } catch (InterruptedException e) {
                              e.printStackTrace();
                       }
                 }
          }
          public synchronized void unlock() {
                 readLockCount--; //decrement readLockCount.
                
                 /*
                 * If readLockCount has become 0,
                 * all threads waiting to write will be notified
                 * and can acquire lock.
                 */
                 if(readLockCount==0)
                       notify();
          }
     
   }


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.
CODE in thread concurrency in java >
WriteLock has been created as Inner class which provides writelock.
   public class WriteLock{
          public synchronized void lock() {
                 /*
             * writeLock can only be obtained if no other thread is
                 * acquiring read or write lock at that time.
                 */
                 if(writeLockCount==0 &&
                              readLockCount==0){
                       writeLockCount++;
                 }
                 /*
                 * if some other thread is
                 * acquiring read or write lock at that time,
                 * than current thread waits.
                 */
                 else{
                       try {
                              wait();
                       } catch (InterruptedException e) {
                              e.printStackTrace();
                       }
                 }
          }
          public synchronized void unlock() {
                 writeLockCount--; //decrement writeLockCount.
                 notify(); //notify all waiting threads.
          }
     
   }


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


4) Custom ReadWriteLock and ReentrantReadWriteLock’s code in java >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
interface ReadWriteLock {
   public ReentrantReadWriteLock.WriteLock writeLock();
   public ReentrantReadWriteLock.ReadLock  readLock();
  
}
class ReentrantReadWriteLock implements ReadWriteLock{
   //Variables to maintain read and write lock count.
   private int readLockCount;
   private int writeLockCount;
   /* Inner class providing readlock */
   private final ReentrantReadWriteLock.ReadLock readerLock;
  
   /* Inner class providing writelock */
   private final ReentrantReadWriteLock.WriteLock writerLock;
   public ReentrantReadWriteLock.WriteLock writeLock() {
      return writerLock;
   }
   public ReentrantReadWriteLock.ReadLock  readLock()  {
      return readerLock;
   }
  
   /**
    * Constructor
    */
   public ReentrantReadWriteLock() {
       readerLock = new ReadLock();
       writerLock = new WriteLock();
   }
  
   /**
    * More than one threads can acquire readLock at a time, provided
    * no other thread is acquiring writeLock at same time.
    */
   public class ReadLock{
          public synchronized void lock() {
                 /*
                 * More than one threads can acquire readLock at a time, provided
                 * no other thread is acquiring writeLock at same time.
                 */
                 if(writeLockCount==0){
                       readLockCount++;
                 }
                 /*
                 * if some other thread is
                 * acquiring write lock at that time,
                 * than current thread waits.
                 */
                 else{
                       try {
                              wait();
                       } catch (InterruptedException e) {
                              e.printStackTrace();
                       }
                 }
          }
          public synchronized void unlock() {
                 readLockCount--; //decrement readLockCount.
                
                 /*
                 * If readLockCount has become 0,
                 * all threads waiting to write will be notified
                 * and can acquire lock.
                 */
                 if(readLockCount==0)
                       notify();
          }
     
   }
  
  
   /**
    * 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.
    */
   public class WriteLock{
          public synchronized void lock() {
                 /*
             * writeLock can only be obtained if no other thread is
                 * acquiring read or write lock at that time.
                 */
                 if(writeLockCount==0 &&
                              readLockCount==0){
                       writeLockCount++;
                 }
                 /*
                 * if some other thread is
                 * acquiring read or write lock at that time,
                 * than current thread waits.
                 */
                 else{
                       try {
                              wait();
                       } catch (InterruptedException e) {
                              e.printStackTrace();
                       }
                 }
          }
          public synchronized void unlock() {
                 writeLockCount--; //decrement writeLockCount.
                 notify(); //notify all waiting threads.
          }
     
   }
  
}

Summary >
In previous tutorial we read how to use ReadWriteLock and ReentrantReadWriteLock in java. In this thread concurrency tutorial we implemented custom ReadWriteLock. This thread concurrency tutorial gave you basic functionality of ReadWriteLock using your own java code.


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

Implementation of custom/own Lock and ReEntrantLock 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 :