×

Reentrant Lock in Java

The Lock interface is implemented by the ReentrantLock class, which also provides methods for synchronising access to shared resources. The code that controls the resource has calls to the lock and unlock functions all over it. This gives the active thread a lock on the resource and prohibits any other threads from attempting to do so.

ReentrantLock, as its name suggests, enables multiple entries by a thread into the lock on a resource. When the thread initially enters the lock, a hold count is initiated at one. Each time a thread enters lock before being released, the  count goes up by one. When the hold count reaches zero, the resource is released. The count drops by one for each unlocking request.

When a resource is unlocked, reentrant locks also include an option that guarantees that the lock will always allow access to the thread which is waiting. This option is set up by passing true to the function Object() in native code.

Synchronization vs Reentrant Lock:

The synchronised keyword is typically used in Java to create thread synchronisation. The synchronised keyword is rather rigorous in its application, despite the fact that it offers some basic synchronisation. For instance, a thread can only accept a lock once. Because synchronised blocks lack a waiting queue mechanism, any thread can take the lock once a thread exits. This could cause some other thread to run out of resources for a very long time.

Java has reentrant locks to offer synchronisation with more flexibility.

  • Fairness is another important distinction between ReentrantLock and the synchronised keyword. The term "synchronised" opposes fairness. No preference may be provided, and once the lock is released it can be acquired by any thread. ReentrantLock can be made fair by supplying the fairness property when creating an instance of the lock. If there is a dispute, the fairness property grants a lock to the thread that has been waiting for the longest.
  • TryLock() method is the second distinction between synchronised and reentrant locks. A handy tryLock() function is provided by ReentrantLock, which only gets lock if it is available and not being held by another thread. This lessens the blocking caused by threads waiting for Java apps to lock in.
  • Another distinction between ReentrantLock and Java's synchronised keyword is the latter's ability to stop threads while they are waiting for locks. There was no way to prevent a thread from becoming stuck waiting for a lock in the case of a synchronised keyword for an endless amount of time. When a thread is waiting for a lock, ReentrantLock's lockInterruptibly() function allows for interruption. If the lock is unavailable for a predetermined amount of time, tryLock() with timeout can be used to time out.

Thus, it is clear that there are several major distinctions between Java's ReentrantLock and the synchronised keyword. In summary, the Lock interface offers a great deal of flexibility and power and gives users considerable control over the lock acquisition process. This feature may be used to create extremely scalable Java applications.

The following are some significant Java Lock API interfaces and classes:

  • Lock: This is the Lock API's fundamental interface. It offers all the benefits of a synchronised keyword along with extra options to define unique locking conditions and a timeout functionality for threads to wait for a lock. A few crucial methods are lock(), which acquires the lock, unlock(), tryLock(), which waits for the lock for a predetermined amount of time, and newCondition(), which creates the condition, among others.
  • Condition: Condition objects have the added capability of creating several sets of wait, comparable to the Object wait-notify model. Lock objects always produce a Condition object. Await(), which is comparable to wait() and signal(), signalAll(), which is comparable to notify() and notifyAll() techniques, are a few of the crucial methods.
  • ReadWriteLock: One lock is for read-only activities, and the other is for writing. This lock is known as the ReadWriteLock. As long as there are no writer threads, several reader threads may hold the read lock concurrently. There is an exclusive write lock.
  • ReentrantLock: The Lock interface's most popular implementation class. The Lock interface is implemented by this class in a manner comparable to the synchronised keyword. ReentrantLock has several utility methods to access the threads holding the lock, threads waiting to acquire the lock, etc. in addition to implementing the Lock interface. Because synchronised blocks must have the lock on the same monitor object in order to function, a thread can enter a synchronised block if it has the monitor object lock and another synchronised block needs it. ReentrantLock, I believe, is the name of the class for this reason.

Program:

Import java.io.*;
Import java.util.*;
class ReentrantLock {


ReentrantLock lock = new ReentrantLock();
 int c = 0;


     
     int Count() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " Count: " + count);
            return c++;
        } finally {
            lock.unlock();
        }
     }
     int CountTwo() {
            return c++;
     }


    public static void main(String args[]) {
        ThreadTest cn = new ThreadTest();
        Thread t3 = new Thread() {
            public void run() {
                while (cn.Count() < 6) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();                    }
                }
            }
        };
        Thread t4 = new Thread() {
            public void run() {
                while (counter.getCount() < 4) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
      
        t3.start();
        t4.start();
    }}

Output:

Thread-0 Count: 0
Thread-1 Count: 1
Thread-1 Count: 2
Thread-0  Count: 3

Related Topics

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.