Java ReentrantReadWriteLock

In this tutorial, we will learn about java.util.concurrent.locks.ReentrantReadWriteLock class, and its methods with explanation and working examples of it.

ReentrantReadWriteLock

In Java, ReentrantReadWriteLock is a class from the concurrent locks package and this is an implementation of ReadWriteLock interface. Which provides a mechanism of multiple reader threads can acquire Read lock at a time in case there is no Writer lock is acquired by any Writer thread. In the similar way Writer thread can acquire a lock only when there is no Reader thread is acquired Read lock.

This kind of mechanism is more suitable where number of Reader threads are high as compared to the Writer threads. And this class provides multiple reads at same time but not multiple writes.

This class methods provides two type of locks one for Reader and another for Writer, this implementation also supports fair sync feature but exceptional to the methods of Reader tryLock() and Writer tryLock().

ReentrantReadWriteLock methods:

writeLock(): This method provides a writer lock in case no Writer thread is currently owned.

readLock(): This method provides a read lock in case there is no write lock is owned by any writer thread.

ReentrantReadWriteLock writeLock() and readLock() methods example:

In the following example, we will use two reader threads and one writer thread. Will use ReentrantReadWriteLock in fair sync policy.

CopiedCopy Code
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
public class ReentrantReadWriteLockExample extends Thread {
	public ReentrantReadWriteLocks rwLocks;
	public ReentrantReadWriteLockExample(String name, ReentrantReadWriteLocks rwLock) {
		super(name);
		this.rwLocks = rwLock;
	}
	public static void main(String[] args) {
		ReentrantReadWriteLocks rwLock = new ReentrantReadWriteLocks(new ReentrantReadWriteLock(true));
		new ReentrantReadWriteLockExample("Writer 1", rwLock).start();
		new ReentrantReadWriteLockExample("Reader 1", rwLock).start();
		new ReentrantReadWriteLockExample("Reader 2", rwLock).start();
	}
	@Override
	public void run() {
		try {
			int i = 0;
			String threaName = Thread.currentThread().getName();
			while (i < 3) {
				if (threaName.contains("Writer")) {
					this.rwLocks.write();
				} else {
					this.rwLocks.read();
				}
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	static class ReentrantReadWriteLocks {
		public ReentrantReadWriteLock reentrantLock;
		public WriteLock writeLock;
		public ReadLock readLock;
		public Integer count;
		public ReentrantReadWriteLocks(ReentrantReadWriteLock reentrantLock) {
			this.reentrantLock = reentrantLock;
			this.writeLock = this.reentrantLock.writeLock();
			this.readLock = this.reentrantLock.readLock();
			this.count = 0;
		}
		public void write() throws Exception {
			this.writeLock.lock();
			System.out.println("Write Lock acquired by : " + Thread.currentThread().getName());
			this.count++;
			Thread.sleep(1000);
			this.writeLock.unlock();
		}
		public Integer read() throws Exception {
			try {
				this.readLock.lock();
				System.out.println("Read Lock acquired by : " + Thread.currentThread().getName());
				Thread.sleep(500);
				return this.count;
			} finally {
				this.readLock.unlock();
			}
		}
	}
}

Output :

Write Lock acquired by : Writer 1
Read Lock acquired by : Reader 1
Read Lock acquired by : Reader 2
Write Lock acquired by : Writer 1
Read Lock acquired by : Reader 2
Read Lock acquired by : Reader 1
Write Lock acquired by : Writer 1
Read Lock acquired by : Reader 2
Read Lock acquired by : Reader 1

Explanation :

We have used fair sync policy and there by every thread gets access and same is reflected in the output. Coming to the example explanation, we have two reader threads named as "Reader 1" and "Reader 2" and one writer thread named as "Writer 1".

We created a class with the name ReentrantReadWriteLocks, this class controls the read and write operations of the threads. We passed the same ReentrantReadWriteLocks object to all the threads, in the run method of threads trying to access the write and read operations. At a time two threads can perform the read operation but at a time only one thread performing write operation during this time no read operation performed.

Conclusion :

In this tutorial, we have covered what is the ReentrantReadWriteLock class its methods along with the working examples of it.