Home » Lock in Java

Lock in Java

In Java, Lock is an interface available in the Java.util.concurrent.locks package. Java lock acts as thread synchronization mechanisms that are similar to the synchronized blocks. After some time, a new locking mechanism was introduced. It is very flexible and provides more options in comparison to the Synchronized block.

These are some of the following differences between a Synchronized block and a Lock:

S.N. Factor Synchronized block Lock
1. Sequence guarantee It doesn’t provide any guarantee of sequence in which the waiting thread will be access. Unlike Synchronized block, the Lock interface handles it.
2. No timeout It doesn’t have any options of time when the lock is not granted. The Lock interface provides such options at the time of granting the lock.
3. Single method It can be contained within a single method. The lock() and unlock() methods of the interface can be called in different methods.

In Java, the Lock interface basically provides six methods which are as follows:

The lock() method

The lock() method is one of the most important methods of the Lock interface. It is used for acquiring the lock. For thread scheduling purposes, the current thread becomes disabled when the lock is not available. The lock() method is a public method that returns void.

Syntax:

Note: The lock implementation can be used for finding an incorrect use of a lock. In our code, the invocation can create a deadlock and can throw an unchecked exception.

The lockInterruptibly() method

The lockInterruptibly() method is another important method of Lock interface that is used for acquiring the lock unless the current thread is interrupted. It acquires and returns a lock immediately if it is available. Just like the lock() method, the current thread becomes disabled for thread scheduling purposes only when the lock is not available. At this time, the thread remains idle until the lock is not acquired or some other thread will not interrupt the current thread.

It throws the InterruptedException when the current thread is interrupted at the time of acquiring the lock.

The lockInterruptibly() method is a public method that returns void and takes no parameters:

Syntax:

The tryLock() method

The tryLock() method is mainly used at the time of invocation for acquiring the lock. It returns the lock immediately with the Boolean value true when the lock is available. It returns the Boolean value false when the lock is not available.

The tryLock() method take no parameters and return the Boolean value.

Syntax:

We use the tryLock() method in the following way:

The tryLock(long time, TimeUnit unit) method

It is another variation of the tryLock() method which is used for acquiring the lock when:

  1. In the given waiting time, the lock will be free.
  2. The current thread will not be interrupted.

It acquires and returns a lock immediately if it is available. Just like the lock() method, the current thread becomes disabled for thread scheduling purposes only when the lock is not available. At this time, the thread remains idle until the lock is not acquired by the current thread. Some other threads will not interrupt the current thread, or the specified waiting time elapses.

Syntax:

Parameters:

time: The time parameter defines the maximum time to wait for the lock.

unit: The unit parameter defines the time unit of the time argument.

Note: The tryLock(long time, TimeUnit unit) throws InterruptedExeption when the current thread is interrupted at the time of acquiring the lock.

The unlock() method

The unlock() method is another most common method which is used for releasing the lock. The unlock() method is a public method that returns nothing and takes no parameter.

Syntax:

The newCondition() method

The newCondition() method is used for getting a new Condition instance that is bound to this Lock instance.

The lock must be held by the current thread before waiting on the condition. A call to the condition.wait() will atomically release the lock before the wait and re-acquire the lock before the wait returns.

Syntax:

Note: The newCondition() throws UnsupportedOperationExeption when the lock implementation doesn’t support conditions.

LockExample.java

Output:

Lock in Java


You may also like