×

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the Lock interface. There are two primary methods: lock() and unlock (). The Lock can be obtained by calling the Lock () method whenever we wish to guarantee that only one thread at a time can access the code. Using the unlock() method, we can release the Lock once the procedure is finished.

Lock vs Synchronized Block

There are a few distinctions between a lock and a synchronised block, as seen below.

LockSynchronized block
There are two methods, Lock () and unlock(), in the Lock interfaces we can employ.Inside a method, synchronised blocks are present at all times.
By defining the fairness characteristic, Lock ensures fairness.Doesn't advocate justice
Locks have a tryLock() mechanism that determines whether it can obtain the Lock ()If a process cannot acquire a synchronised block, it is stopped.
To disrupt a waiting process, use the Interrupibilty() function.A thread in the ready queue cannot be interrupted to reach the synchronised blocks.

Java Interfaces Mechanisms for Locks

The Lock interface's methods are outlined below.

MethodDescription
Lock void ()the void lock is acquired
Lock void Interruptible()obtains the Lock till the interruption of the active process.
Condition newCondition()Returning a new condition linked to the active lock instances is a condition().
booleantryLock()obtains the Lock if it is available at the moment given.
boolean tryLock (long time, TimeUnit unit)  The Lock is only acquired if available during the stated time period and remains unbroken until it is released.
Void unlockReturns the Lock with void unlock()

Implementing Locks

As shown below, JDK provides many Java implementations of the Lock interface.

Reentrant Lock

The Java Lock interface is used by the reentrant properties of the ReentrantLock object. The unlock() method removes the Lock it just acquired that uses the Lock () method. To prevent deadlock, utilising the open () function inside the block header is crucial. Deadlock circumstances Only one process at the moment can execute the synchronised code thanks towards this thread-safe locked.

Code:

ReentrantLock l = newReentrantLock();
intcnt = 0;


voidcounter(){
l.lock();


try{
cnt++;
}
finally{
l.unlock();
}
}

ReentrantReadWriteLock

Some other Java class that supports the Lock protocol is the ReentrantReadWriteLock. It has two locks on it for read-only and write-only access. As soon as nobody is concurrently writing to the variables, this sort of Lock guarantees the security of reading them. As long as there is no writing access to the item, several threads can maintain read-lock access to it. In circumstances where a read process is much more complex than a write operation, this enhances performance.

It is crucial to abide by the following guideline:

  • ReadLock: When no thread possesses or demands a reading, many processes may retain access permission.
  • WriteLock: When there is neither read/write accessibility to a specific item, only one process is permitted to register permission.

Below is a piece of code that demonstrates the working of ReentrantReadWriteLock.

Code:

ExecutorService exec = Executors.newFixedThreadPool(2);
    Map<Integer, String> m = new HashMap<Integer,String>();
ReadWriteLockrwl = newReentrantReadWriteLock();


exec.submit(() ->{
rwl.write-lock().lock();
try{
Thread.sleep(1000);
m.put(1,"Java");
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
rwl.writeLock().unlock();
}


});


    Runnable read = () ->{
rwl.readLock().lock();


try{
System.out.println(m.get(1));
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
rwl.readLock().unlock();
}
};


exec.submit(read);
exec.submit(read);

StampedLock

StampedLock is comparable to ReentrantReadWriteLock, with the exception that ReadLocks and WriteLocks both support long-type timestamps. We can provide this timeframe when calling the unlock() technique to remove the Lock. As of Java 8, the StampedLock is accessible. The timestamp can also be used to determine whether a lock is still in effect.

Example of a Java Lock Interface

The Java Lock interface's Lock () and unlock() methods are demonstrated in the straightforward example below, as they operate with many threads. Thus, three lines are created, each of which calls the incCounter() method to increase the counter. Because we used the Lock interface, we can observe that although all three threads have begun, only one is executed at the moment. The Lock ()is called when the current thread acquires the Lock, increments the counter value, and releases it using the unlock() method. This way, it prevents deadlock.

Code:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
publicclassLockDemo{
publicstaticvoidmain(String[]args){
ExecuteTask e = newExecuteTask();
    Thread t[] = new Thread[3];


for(inti=0;i<3;i++){
      t[i] = newThread(newCounterDemo(e), "Thread " + i);
}
for(inti=0;i<3;i++){
      t[i].start();
}


}
}
classCounterDemoimplements Runnable {
ExecuteTask exec;


publicCounterDemo(ExecuteTask exec){
this.exec = exec;
}


publicvoidrun(){
System.out.println("Call executeTask to increment counter: " + Thread.currentThread().getName());
exec.incCounter();


}


}
classExecuteTask{
intcnt = 0;
  Lock l = newReentrantLock();


voidencounter(){
l.lock();
try{
cnt++;
}
finally{
System.out.println("Executing " + Thread.currentThread().getName() + ":" + " Counter value: " + cnt);
l.unlock();
}
}
}

Output:

Call executeTask to increment the counter: Thread 1
Call executeTask to increment the counter: Thread 0
Call executeTask to increment the counter: Thread 2
Executing Thread 1: Counter value: 1
Executing Thread 0: Counter value: 2
Running Thread 2: Counter value: 3

Related Topics

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.