Java Concurrency - Lock Interface

A java.util.concurrent.locks.Lock interface is used to as a thread synchronization mechanism similar to synchronized blocks. New Locking mechanism is more flexible and provides more options than a synchronized block. Main differences between a Lock and a synchronized block are following −

  • Guarantee of sequence − Synchronized block does not provide any guarantee of sequence in which waiting thread will be given access. Lock interface handles it.

  • No timeout − Synchronized block has no option of timeout if lock is not granted. Lock interface provides such option.

  • Single method − Synchronized block must be fully contained within a single method whereas a lock interface's methods lock() and unlock() can be called in different methods.

Lock Methods

Following is the list of important methods available in the Lock class.

Sr.No. Method & Description

public void lock()

Acquires the lock.


public void lockInterruptibly()

Acquires the lock unless the current thread is interrupted.


public Condition newCondition()

Returns a new Condition instance that is bound to this Lock instance.


public boolean tryLock()

Acquires the lock only if it is free at the time of invocation.


public boolean tryLock(long time, TimeUnit unit)

Acquires the lock if it is free within the given waiting time and the current thread has not been interrupted.


public void unlock()

Releases the lock.


The following TestThread program demonstrates some of these methods of the Lock interface. Here we've used lock() to acquire the lock and unlock() to release the lock.

Live Demo
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class PrintDemo {
   private final Lock queueLock = new ReentrantLock();

   public void print() {

      try {
         Long duration = (long) (Math.random() * 10000);
            + "  Time Taken " + (duration / 1000) + " seconds.");
      } catch (InterruptedException e) {
      } finally {
            "%s printed the document successfully.\n", Thread.currentThread().getName());

class ThreadDemo extends Thread {
   PrintDemo  printDemo;

   ThreadDemo(String name,  PrintDemo printDemo) {
      this.printDemo = printDemo;

   public void run() {
         "%s starts printing a document\n", Thread.currentThread().getName());

public class TestThread {

   public static void main(String args[]) {
      PrintDemo PD = new PrintDemo();

      ThreadDemo t1 = new ThreadDemo("Thread - 1 ", PD);
      ThreadDemo t2 = new ThreadDemo("Thread - 2 ", PD);
      ThreadDemo t3 = new ThreadDemo("Thread - 3 ", PD);
      ThreadDemo t4 = new ThreadDemo("Thread - 4 ", PD);


This will produce the following result.


Thread - 1  starts printing a document
Thread - 4  starts printing a document
Thread - 3  starts printing a document
Thread - 2  starts printing a document
Thread - 1   Time Taken 4 seconds.
Thread - 1  printed the document successfully.
Thread - 4   Time Taken 3 seconds.
Thread - 4  printed the document successfully.
Thread - 3   Time Taken 5 seconds.
Thread - 3  printed the document successfully.
Thread - 2   Time Taken 4 seconds.
Thread - 2  printed the document successfully.

We've use ReentrantLock class as an implementation of Lock interface here. ReentrantLock class allows a thread to lock a method even if it already have the lock on other method.