Java - Read and Write Lock


A ReentrantLock provides a mutually exclusive locking mechanism.

Only one thread can own the ReentrantLock at a time.

If you have a data guarded by a ReentrantLock, a writer thread as well as a reader thread must acquire the lock one at a time to modify or to read the data.

This restriction may downgrade the performance if data is read frequently and modified infrequently.

If the data is being modified, only one writer thread should have the access to the data structure.

Read-Write lock

Read-Write lock implements this kind of locking mechanism using an instance of the ReadWriteLock interface.

ReadWriteLock has two methods: one to get the reader lock and another to get the writer lock.

public interface ReadWriteLock {
 Lock readLock();
 Lock writeLock();

ReentrantReadWriteLock class is an implementation of the ReadWriteLock Interface.

Only one thread can hold the write lock of ReentrantReadWriteLock, whereas multiple threads can hold its read lock.

The following code demonstrates ReentrantReadWriteLock.


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Main {

  static ReadMostlyData data  = new ReadMostlyData(10);
  public static void main(String[] argv) {
    //from   w  w w  .j a va 2  s .co  m
    // Create two Thread objects
    Thread t1 = new Thread(Main::print);
    Thread t2 = new Thread(Main::print);

    // Start both threads

  public static void print() {
    for (int i = 1; i <= 5; i++) {
      try {
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block

class ReadMostlyData {
  private int value;
  private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
  private Lock rLock = rwLock.readLock();
  private Lock wLock = rwLock.writeLock();

  public ReadMostlyData(int value) {
    this.value = value;

  public int getValue() {
    // Use the read lock, so multiple threads may read concurrently
    try {
      return this.value;
    } finally {

  public void setValue(int value) {
    // Use the write lock, so only one thread can write at a time
    try {
      this.value = value;
    } finally {


Related Topic