Reentrantlock Java

By | | Updated : 2021-03-12 | Viewed : 2139 times

Reentrantlock Java

The reentrant Lock mechanism is one of the techniques to avoid data inconsistency in multi-threaded environments. It is introduced in java 1.5 in the package of java.util.concurrent. It is very useful in multi-threading.

Quick note for Lock in Java

The lock provides access control to thread exclusively on share resources to avoid access control on shared resources by other threads at the same time. A lock is the implementation of the Lock interface which in the package java.util.concurrent.locks.Lock. Synchronization also uses the same type of locking i.e., monitor lock internally to acquired access control on shared resources. But it has its own disadvantages. To overcome these advantages Lock concept is introduced.

Reentrant Lock in Java

Reentrant Lock allows the thread to hold a lock on a particular object. It is mutual lock means the current thread acquires when no other thread acquires the lock. If another thread obtains a lock then the current thread can not obtain the lock until another thread unlocks. in other words, the thread can obtain lock using with lock() method. the lock will be held with the same thread until the method unlock() execution.

lock() method in the ReentrantLock class does acquire the lock on a particular object whereas unlock() method for releasing the lock.

Reentrant Lock same synchronized block i.e., implicit lock. But it provides more extensibility than implicit locks. Reentrant provide fairness where thread preference is the main one for getting the lock.

Reentrant Lock Example

CounterWithReentrantLock.java
package com.docsconsole.locks.example1;

import java.util.concurrent.locks.ReentrantLock;

public class CounterWithReentrantLock {
	
	private final ReentrantLock lock = new ReentrantLock();
	private int counter = 0;
	
	public int getCount() {
		lock.lock();
		try {
			String currentThreadName = Thread.currentThread().getName();
			System.out.println("Current Thread"+ currentThreadName + " counter: " + counter);
			return counter ++;
		} finally {
			lock.unlock();
		}

	}
	
	public static void main(String args[]) {
		
	       final CounterWithReentrantLock counter = new CounterWithReentrantLock();
	       Thread t1 = new Thread() {

	           @Override
	           public void run() {
	               while (counter.getCount() < 6) {
	                   try {
	                       Thread.sleep(100);
	                   } catch (InterruptedException ex) {
	                       ex.printStackTrace();                    }
	               }
	           }
	       };
	     
	       Thread t2 = new Thread() {

	           @Override
	           public void run() {
	               while (counter.getCount() < 6) {
	                   try {
	                       Thread.sleep(100);
	                   } catch (InterruptedException ex) {
	                       ex.printStackTrace();
	                   }
	               }
	           }
	       };
	     
	       t1.start();
	       t2.start();
	     
	   }
}

ReentrantLock vs synchornzed

Fairness locking: Reentrant lock does have the ability where the lock is owned by a thread based on thread preference. But it is not possible in the case of synchronized.

Ordering and Visibility: It does provide the same ordering and visibility as same as synchronized.

lockInterruptibly(): It will interrupt the thread where the thread is waiting for acquiring the lock. It is not a possible case with a synchronized keyword.

trylock(): trylock() will try to acquire the lock where no lock existed on particular shared resources.

Leave A Reply