×

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

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.