Easy learning with example program codes

readwritelock in java

The ReadWriteLock is an interface which provides an advanced thread lock mechanism. It maintained a pair of locks (one for read only and other for write). The basic idea for ReadWriteLock is to increase the performance by allowing multiple threads to read a shared resource, when no thread is writing.

Note: Only one thread can write the shared resource value at a time.

ReadWriteLock locking rules:

Read Lock: If no thread have or not requested ReadWriteLock for writing then multiple threads can lock the read lock for shared resource.

Write Lock: If no thread have read or write lock then one thread can lock write lock on shared resource.

Note: It performs best when there are more read locks required as compared to write locks.


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class LockTest<E> {
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();	
	private final Lock readLock = readWriteLock.readLock();
	private final Lock writeLock = readWriteLock.writeLock();
	private final List<E> list = new ArrayList<>();
	public void set(E o){
			System.out.println("Add element: "+Thread.currentThread().getName());
	        } finally {
	public E get(int i) {
		try	{
                        System.out.println("Print elements: "+Thread.currentThread().getName());
			return list.get(i);
		} finally {
	public static void main(String[] args) {
		LockTest<String> lockTest = new LockTest<>();
                System.out.println("First Element: "+lockTest.get(1));


Add element: main
Add element: main
Add element: main
Print elements: main
First Element: 2

Related topics

Please follow and like us:
Posted in Java   

Core Java Tutorial

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki