×

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language.

Introduction

Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs when threads are always waiting. This can happen from time to time.

A stalemate occurs when more than two threads attempt to access an object that another thread has already acquired. Deadlock is the term for the situation because the threads are waiting for the object to be released. There are more than two threads involved in the scenario. The most significant and frequently requested question in a Java interview is how to prevent deadlock.

Definition:

Every object in the thread has a lock. Java offers a synchronization to lock a block of the code or a method in order to obtain a lock. It permits just one Thread at a time to access that method.

However, a thread initially tries to obtain a lock if it wishes to run a synchronized method. The Thread (which acquires the lock) will have to be patient until the earlier Thread doesn’t let go of the lock if it is feasible that another thread has already obtained the lock.

Example

Let's say there are thread1 and thread2, two threads. The locks of objects obj1 and obj2 were obtained by threads 1 and 3, respectively. Assume that thread2 already has a lock on object 2 and that Thread1 is performing procedure 1 and attempting to acquire the lock on obj2. While thread1 has a lock on obj1, thread2 simultaneously attempts to gain a lock on obj2. In this case, neither thread will finish running, and has to be patient for the lock to be let go. The circumstance is referred to as a deadlock.

Detection of Deadlock in Java

The following techniques can be used to spot a deadlock:

First, we inspect and understand the program to see whether there are any nested synchronized blocks, try to release a lock on different objects, or call synchronized methods from various synchronized methods which could result in a deadlock.

Using the IO gateway is an alternate method of recognizing deadlock. It enables us to upload and examine a thread dump.

Deadlock detection can also be done with jConsole or VisualVM. It displays which threads are locking up concerning which item.

Avoidance of Deadlock in Java

Although avoiding deadlock conditions is impossible, we can do so by employing the following strategies:

Avoid Using Locks: We should only use locks on the members for whom they are necessary. Deadlock results from unnecessary lock usage. The use of a lock-free data structure is advised. Keep our code as clear of locks as we can. For instance, use ConcurrentLinkedQueue rather than synchronized ArrayList.

Avoid Nested Locks: Giving locks to many threads when one Thread already has a lock is another technique to prevent deadlock since we must prevent giving a lock to more than one Thread.

A deadlock can result when two threads wait endlessly for each other to finish using the Thread.join() method. It's always suggestible to utilize the enter with the longest wait time possible if our Thread needs to wait for an alternate or waited for a thread to finish.

Ordering for Use Locks: Each lock should always be given a numerical value. Purchase the locks with the lower number value first, then the lock with the higher numeric value.

Lock Time-out: We can assume the limit of a thread and how long takes to obtain a lock. A thread must wait a predetermined time before trying to place a lock again if it fails to do so the first time.

Let's understand it through an example.

Example:

class Jtp
{
    // Sleep method
    static void sleep(long mls)
    {
        try
        {
            Thread.sleep(mls);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
 
class SharedThread
{
    // synchronized method
    synchronized void test1(SharedThread st2)
    {
        System.out.println("test -1 starts !!");
        Jtp.sleep(1000);


        st2.test2();
        System.out.println("test-1 – end !!!");
    }
 
    // another synchronized method
    synchronized void test2()
    {
        System.out.println("test-2 Starts !!");
        Jtp.sleep(1000);
        System.out.println("test2-end");
    }
}
 
 
class Thread1 extends Thread
{
    private SharedThread st1;
    private SharedThread st2;


    public Thread1(SharedThread st1, SharedThread st2)
    {
        this.st1 = st1;
        this.st2 = st2;
    }
 
    // run method to start a thread
    @Override
    public void run()
    {
        st1.test1(st2);
    }
}
 
class Thread2 extends Thread
{
    private SharedThread st1;
    private SharedThread st2;
    public Thread2(SharedThread st1, SharedThread st2)
    {
        this.st1 = st1;
        this.st2 = st2;
    }


    @Override
    public void run()
    {
        st2.test1(st1);
    }
}
 
public class DeadlockTest
{
    public static void main(String s[])
    {
        SharedThread st1 = new SharedThread();
         SharedThread st2 = new SharedThread();
 
        Thread1 t1 = new Thread1(st1, st2);
        t1.start();
 
        Thread2 t2 = new Thread2(st1, st2);
        t2.start();
 
        // sleeping main Thread
        Jtp.sleep(2000);
    }


}  

Output:

Deadlock Prevention and Avoidance in Java

Conclusion

If we are dealing with several locks, it is advised to always buy the locks with the lower numerical value before buying the ones with the higher numerical value.


Related Topics

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...

4 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

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.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 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.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.