How to Avoid Deadlock in Java Threads

How to Avoid Deadlock in Java Threads

In this article, we will learn how to avoid deadlock in Java? The question of avoiding deadlock is one of the popular Java Interview questions which mostly asked to experience developer, with multi-threading being asked mostly at a senior level interview with lots of follow up questions. Even though the question looks very basic, most of the Java developers get stuck once you start going deeper.

Here I have divided above article into…

1) What is a Deadlock in Java?

2) How Do you Detect Deadlock in Java?

3) Write a Java Program That Will Result in Deadlock

1) What is a Deadlock in Java?

Deadlock in Java is a condition when two or more threads try to access the same resources at the same time. Then these threads can never access the resource and eventually go into the waiting state forever.

So, the deadlock condition arises when there are more than two threads and two or more than two resources. Basically, a deadlock occurs when multiple threads request for the same resource but they are received in a different order.

Eventually, they get stuck for an infinite period of time and cause a deadlock.

2) How Do you Detect Deadlock in Java?

There are several ways to define the Deadlock in java. first, we will look at the code to see if a nested synchronized block is calling a synchronized method from another or if it is trying to get a lock on a different object. If that is the case, there is a good chance of deadlock, if the developer is not careful.

Another way to determine deadlock risks is when you actually get dead-locked while running the application. If this happens, try to take a thread dump, in Linux you can do this by the command “kill -3.” This will print the status of all threads in an application log file, and you can see which thread is locked on which object.

Another way is to use the jConsole/VisualVM. It will show you exactly which threads are getting locked and on which object.

How to Avoid Deadlock in Java Threads

3) Write a Java Program That Will Result in Deadlock

Once you answer the earlier question, they may ask you how to write code that will result in a deadlock in Java.

Here is one way of doing it:

Example:

Deadlock example

public class DeadLockDemo {

public class DeadLockDemo {

    /*
     * This method request two locks, first String and then Integer
     */
    public void method1() {
        synchronized (String.class) {
            System.out.println("Aquired lock on String.class object");

            synchronized (Integer.class) {
                System.out.println("Aquired lock on Integer.class object");
            }
        }
    }

    /*
     * This method also requests same two lock but in exactly
     * Opposite order i.e. first Integer and then String. 
     * This creates potential deadlock, if one thread holds String lock
     * and other holds Integer lock and they wait for each other, forever.
     */
    public void method2() {
        synchronized (Integer.class) {
            System.out.println("Aquired lock on Integer.class object");

            synchronized (String.class) {
                System.out.println("Aquired lock on String.class object");
            }
        }
    }
}

If method1()  and method2()  are both called by two or more threads, there is a good chance of deadlock, because if Thread 1 acquires a lock on a String object while executing method1()  and Thread 2 acquires a lock on the Integer object while executing method2() , both will be waiting for each other to release a lock on the Integer and String to proceed further, which will never work.

This diagram effectively demonstrates our program, where one thread holds a lock on one object and is waiting for the other object to lock, which is held by the other thread.

How to Avoid Deadlock in Java Threads

You can see that Thread 1 wants the lock on Object 2, which is held by Thread 2, and Thread 2 wants a lock on Object 1, which is held by Thread 1. Since no thread is willing to give up, there is a deadlock and the Java program is stuck.

How to Avoid Deadlock in Java Threads – How to Avoid Deadlock in Java Threads – How to Avoid Deadlock in Java Threads – How to Avoid Deadlock in Java Threads