CodesJava

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.

Example

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){
		writeLock.lock();
		try{
			list.add(o);
			System.out.println("Add element: "+Thread.currentThread().getName());
	        } finally {
			writeLock.unlock();
		}
	}
 
	public E get(int i) {
		readLock.lock();
		try	{
                        System.out.println("Print elements: "+Thread.currentThread().getName());
			return list.get(i);
		} finally {
			readLock.unlock();
		}
	}
	public static void main(String[] args) {
		LockTest<String> lockTest = new LockTest<>();
		lockTest.set("1");
		lockTest.set("2");
		lockTest.set("3");
 
                System.out.println("First Element: "+lockTest.get(1));
	}
}

Output

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

Related topics

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki