×

How to avoid deadlock in java

Deadlock:

A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple threads sequentially for multitasking. In some cases, the different threads themselves will be waiting and it lasts for a forever state which is called deadlock.

In the thread, every block has a lock. To make a lock, it provides synchronization to lock to a method or block of code.

How to avoid deadlock in java

How to avoid deadlock in java:

  • Avoid Unnecessary Locks: We should assign the locks for only those blocks on which it is going to perform the operations. The unnecessarily using of locks will leads to a deadlock condition. We suggest that you use a lock-free data structure. otherwise, keep your code free from locks. For example, use the concurrent linked queue instead of using a synchronized array list.
  • Avoid Nested Locks: The other possible way to avoid deadlock is to avoid assigning a lock to multiple threads at a time if we have already assigned a lock to one thread then we must avoid assigning a lock to multiple threads otherwise it leads to a deadlock.
  • Using Thread.join() Method: we get a deadlock if we use two threads or more threads waiting for each other to complete by using another thread to join method. If our thread has to wait for another thread to complete, then it's always the best way to use the join method with the maximum time you want to wait for the other thread to finish.
  • Use Lock Ordering: We should assign a numeric value to each lock when we are performing the operation. Before assigning the lock with a high numeric value, then we should acquire the locks with the least numeric value.
  • Lock Time-out: We can also specify the time for a thread to perform a lock. If a thread does not assign a lock, then that thread must wait for a particular time before retrying to acquire a lock.

Example:

public class DeadlockTest {
   public static void main(String[] args) throws InterruptedException {
      Demo obj1 = new       Demo ();
            Demo obj2 = new       Demo ();
           Demo obj3 = new       Demo ();
      Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1");
      Thread t2 = new Thread(new SyncThread(obj2, obj3), "t2");
      t1.start();
      Thread.sleep(2000);
      t2.start();
      Thread.sleep(2000);
   }
}
class SyncThread implements Runnable {
   private       Demo obj1;
   private      Demo obj2;
   public SyncThread(      Demo o1,       Demo o2){
      this.obj1=o1;
      this.obj2=o2;
   }
   @Override
   public void run() {
      String name = Thread.currentThread().getName();
      System.out.println(name + " allocating lock on " + obj1);
      synchronized (obj1) {
         System.out.println(name + " allocated lock on " + obj1);
         work();
      }
      System.out.println(name + " removed lock on " + obj1);
      System.out.println(name + " allocating lock on " + obj2);
      synchronized (obj2) {
         System.out.println(name + " allocating lock on " + obj2);
         work();
      }
      System.out.println(name + " removed lock on " + obj2);
      System.out.println(name + " completed execution.");
   }
   private void work() {
      try {
         Thread.sleep(5000);
      } catch (InterruptedException ie) {
         ie.printStackTrace();
      }
   }
}

Output:

t1 allocated g a lock on java. lang.Demo@917d8d4
t1 allocated lock on java. lang. Demo@ 917d8d4
t2 allocating lock on java. lang. Demo@ 5c4b42fb
t2 allocated lock on java. lang. Demo@ 5c4b42fb
t1 released the lock on java. lang. Demo@917d8d4
t1 allocating a lock on java. lang. Demo@ 5c4b42fb
t1 allocated lock on java. lang. Demo@ 5c4b42fb
t2 released the lock on java. lang.Object@5c4b42fb
t2 allocating lock on java. lang. Demo@ 528cb702
t2 allocated lock on java. lang. Demo@ 528cb702
t1 removed the lock on java. lang. Demo@ 5c4b42fb
t2 removed the lock on java. lang. Demo@ 528cb702
t1 completed execution.
t2 completed execution.

Deadlock occurs

Example:

// Importing the required packages
import java.io.*;
import java.util.*;
 
// Class 1
class Demo {
 
    public synchronized void last()
    {
 
        // Print and display the statement
        System. out.println("Inside Demo, last() method");
    }
    
    public synchronized void d1(B b)
    {
        System.out.println(
            "Thread1 start execution of d1() method");
        try {
 
            // Putting the existing thread to sleep for
            // specific time using sleep() method
            Thread.sleep(2000);
        }
 
        // Catch block to handle the exceptions
        catch (InterruptedException e) 
            System.out.println(e);
        }
 
        System.out.println(
            "Thread trying to call B's last() method");
 
        // Calling method 1 of this class as created
        // above
        b.last();
    }
}
 
// Class 2
// Helper class Sample
class Sample {
 
    // Method 1 of this class
    public synchronized void last()
    {
 
        // Display statement only
        System.out.println("Inside Sample, last() method");
    }
    public synchronized void d2(Demo a)
    {
 
        // Display message only
        System.out.println(
            "Thread2 start execution of d2() method");
 
        try {
            Thread.sleep(2000);
         }
        catch (InterruptedException e) {
             System.out.println(e);
        }


        System.out.println(
            "Thread2  trying to call A's last method");
        a.last();
    }
}
 
// Deadlock class which is extending Thread class
class Simple extends Thread {
 
    // Creating object of type class Demo
    Demo a = new Demo();
 
    // Creating object of type class Sample
    Sample b = new Sample();
 
    public void m1()
    {
 
        // Starting the thread
        this.start();
 
        // Calling d1 method of class A
        a.d1(b);
    }
 
    // run() method for the thread
    public void run()
    {
 
        // Calling d2 method of class B
        b.d2(a);
    }
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating object of this class
        Simple deadlock = new Simple();
 
        // Calling the m1 method
        deadlock.m1();
    }
}

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.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

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

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.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.