Java ReentrantLock

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

ReentrantLock

In Java, ReentrantLock class under the package of concurrent locks and it is a concrete implementation of the Lock interface. This class allows the same thread can be acquired the lock number of times as long it releases the lock with the same number of times.

It also provides fair sync feature, and fair sync is the mechanism of giving a lock to the most waiting thread instead of randomly giving lock any waiting thread. With this feature it gives greater flexibility to provide for all the waiting threads based on the waiting time which enables to predict the thread which is going to get the lock.

ReentrantLock methods:

Here we will discuss on all available methods and enable/disable of fair sync feature.

lock(): It checks for the re-entrancy and acquires a lock based on fair or unfair sync feature.

tryLock(): This method provides mechanism to acquire a lock without waiting, if the lock is acquired return true otherwise false at the time of invocation. This method will not follow the fair sync feature.

tryLock(long time, TimeUnit unit) : This method causes the requesting thread to wait for specified time to obtain the lock, current thread resumes its processing either by getting the lock or wait until specified time whichever comes first. This method follows the fair sync feature.

lockInterruptibly(): This method will wait until the lock gets granted, to interrupt this waiting either lock should be granted or Thread interrupt method should be called any other thread than current thread. If the current thread is already acquired the lock, then lock count will be incremented and returns the lock.

getWaitQueueLength(Condition condition): It gives the result of number of threads waiting for the given condition associated with the lock.

unlock(): Releases the lock of current thread, In the case of re-entrancy the lock count will be reduced by 1. If lock count becomes zero then lock will be released.

ReentrantLock example :

CopiedCopy Code
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample extends Thread {
	public ReentrantLocks lock;
	public ReentrantLockExample(ReentrantLocks lock, String threadName) {
		super(threadName);
		this.lock = lock;
	}
	public static void main(String args[]) {
		ReentrantLocks locks = new ReentrantLocks();
		new ReentrantLockExample(locks, "T1").start();
		new ReentrantLockExample(locks, "T2").start();
	}
	public void run() {
		this.lock.increment();
		System.out.println(
				"Count Value : " + this.lock.getCount() + " returned by : " + Thread.currentThread().getName());
	}
	static class ReentrantLocks {
		private final ReentrantLock lock = new ReentrantLock(true);
		private int count;
		public void increment() {
			lock.lock();
			try {
				System.out.println("Locked by thread : " + Thread.currentThread().getName());
				count++;
			} finally {
				System.out.println("Locked released by thread : " + Thread.currentThread().getName());
				lock.unlock();
			}
		}
		public int getCount() {
			lock.lock();
			try {
				return count;
			} finally {
				lock.unlock();
			}
		}
	}
}

Output :

Locked by thread : T1
Locked released by thread : T1
Locked by thread : T2
Locked released by thread : T2
Count Value : 2 returned by : T1
Count Value : 2 returned by : T2

Explanation :

In the above example, we have created a ReentrantLock class object with fair sync by passing boolean flag as true inside a ReentrantLocks class. In the main class, we have initialized the ReentrantLocks object and passed the same object to the two threads. In the run method of ReentrantLockExample class invoked the methods of ReentrantLocks class.

If you see the output of the example, we first lock is acquired by T1 thread later it released and acquired by T2 and subsequently. Due to this fair sync as, true we got the count same for two threads.

Conclusion :

In this tutorial, we have covered about the ReentrantLock and its methods along with the working example of it.