ReentrantLock Class

  1. It is an Implementation class of lock interface and it is the directly child class of object.
  2. Reentrant means a thread can acquire same lock multiple times without any issue.
  3. Internally reentrant lock increments threads personal count when ever we call lock method and decrements count value whenever thread calls unlock method and lock will be release only when count reaches zero

Constructors :

  • ReentrantLock l = new ReentrantLock()
    1. Creates a instance of reentrant lock
    2. By befault faireness in set to false
  • ReentarantLock l = new ReentrantLock(boolean fairness)
    1. Creates Reentrant lock with given fainess policy
    2. If the fairness is true then longest waiting thread can acquire the lock if it is available i.e it follows FCFS(First Come First Serve) policy.
    3. If the fairness is false then which waiting thread will get the chance we can expect.

Important methods of ReentrantLock class

  • void lock()
  • void trylock()
  • void trylock(long l,TimeUnit unit)
  • void lockInterruptably()
  • void unlock()
  • int getHoldCount()
  • boolean isHeldByCurrentThread()
  • int getQueueLength
  • Collection getQueuedThreads()
  • boolean hasQueuedThreads()
  • boolean isLocked()
  • boolean isFair()
  • Thread getOwner()
  • int getHoldCount()
    Returns number of holds on this lock by current Thread
  • boolean isHeldByCurrentThread()
    Returns true if and only lock is hold by current Thread
  • int getQueueLength()
    Returns number of Threads waiting for the lock
  • Collection getQueuedThreads()
    It returns a collection of Threads which are waiting to get the lock
  • boolean hasQueuedThreads()
    It returns true if any thread waiting to get the lock
  • boolean isLocked()
    Returns true if the lock is acquired by some thread.
  • boolean isFair()
    Returns true if the fairness policy with the true value.
  • Thread getOwner()
    Returns the thread which acquires the lock
public class ReentrantLockDemo1 {
	public static void main(String[] args) {
		ReentrantLock l = new ReentrantLock();
		l.lock();
		l.lock();
		System.out.println(l.isLocked());
		System.out.println(l.isHeldByCurrentThread());
		l.unlock();
		System.out.println(l.isHeldByCurrentThread());
		System.out.println(l.isLocked());
		l.unlock();
		System.out.println(l.isLocked());
		System.out.println(l.isFair());
	}
}

Demo of lock() method

public class ReentrantLockDemo2 {
	public static void main(String[] args) {
		Display d = new Display();
		MyThread t1 = new MyThread(d,"Tyson");
		MyThread t2 = new MyThread(d,"Justin");
		MyThread t3 = new MyThread(d,"Martin");
		t1.start();
		t2.start();
		t3.start();
	}
}
class MyThread extends Thread{
	Display d;
	String name;
	MyThread(Display d,String name){
		this.d = d;
		this.name = name;
	}
	public void run() {
		try {
			d.wish(name);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class Display{
	ReentrantLock l = new ReentrantLock();
	
	public void wish(String name) throws InterruptedException {
		l.lock();
		for(int i=0;i<5;i++) {
			System.out.println("Hello "+name);
			Thread.sleep(500);
		}
		l.unlock();
	}
}

Demo program from trylock()

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo3 {
	public static void main(String[] args) {
		MyThread1 t1 = new MyThread1("First Thread ");
		MyThread1 t2 = new MyThread1("Second Thread ");
		t1.start();
		t2.start();
	}
}
class MyThread1 extends Thread{
	static ReentrantLock l = new ReentrantLock();
	MyThread1(String name){
		super(name);
	}
	public void run() {
		if(l.tryLock()) {
			for(int i=0;i<10;i++) {
				System.out.println("Hello "+Thread.currentThread().getName()+i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			l.unlock();
		}else {
			System.out.println(Thread.currentThread().getName()+" Unable to get lock");
		}
	}
}

Demo program for tryLock(long time,TimeUnit unit)

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo3 {
	public static void main(String[] args) {
		MyThread1 t1 = new MyThread1("First Thread ");
		MyThread1 t2 = new MyThread1("Second Thread ");
		t1.start();
		t2.start();
	}
}
class MyThread1 extends Thread{
	static ReentrantLock l = new ReentrantLock();
	MyThread1(String name){
		super(name);
	}
	public void run() {
		do {
			try {
				if(l.tryLock(3000,TimeUnit.MILLISECONDS)) {
					System.out.println(Thread.currentThread().getName()+" got the lock");
					for(int i=0;i<10;i++) {
						System.out.println("Hello "+Thread.currentThread().getName()+i);
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					System.out.println(Thread.currentThread().getName()+" releasing the lock");
					l.unlock();
					break;
				}else {
					System.out.println(Thread.currentThread().getName()+" : Unable to get lock will be trying again");
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}while(true);
	}
}

Leave a Comment