Deadlock in Java

In this article, we will learn about the deadlock concept in Java. Let’s start!!!

What is a deadlock?

As mentioned earlier, deadlock comes with multi-threading. It occurs when multiple threads request for the same resource and the order of allocation is improper. This leads to an infinite time period and creates a deadlock.

The figure shown below portrays the scenario of Deadlock in Java:

what is deadlock

Sample program to implement deadlock in Java:

public class FirstCode{
public static void main(String args[]){
final String resource1 = “Tutorial”;
final String resource2 = “Course”;
Thread t = new Thread(){
public void run(){
synchronized(resource1){
System.out.println(“Thread 1: Resource 1 under lock”);
try{
Thread.sleep(150);
}
catch(Exception e){}
synchronized(resource2){
System.out.println(“Thread 1: Resource 2 under lock”);
}
}
}
} ;
Thread t2 = new Thread(){
public void run(){
synchronized(resource2){
System.out.println(“Thread 2: Resource 2 under lock”);
try{
Thread.sleep(150);} catch(Exception e){}
synchroized(resource1){
System.out.println(“Thread 2: Resource 1 under lock”)
}
}
}
};
t1.start();
t2.start();
}
}

Output:

Thread 1: Resource 1 under lock
Thread 2: Resource 2 under lock

More complicated deadlocks in Java:

When more than two threads are there in the deadlock, the complexity increases; here is a small depiction of it:

  • Thread 1 locks A, waits for B
  • Thread 2 locks B, waits for C
  • Thread 3 locks C, waits for D
  • Thread 4 locks D, waits for A
  • Thread 1 waits for Thread 2, Thread 2 waits for Thread 3, Thread 3 waits for Thread 4, and Thread 4 waits for Thread 1.

Detecting Deadlock in Java:

We came to know that deadlock causes many issues to the system. Therefore, avoiding it is necessary. But before jumping into the process of avoiding deadlocks, we need to first recognize if a deadlock is present. This process is termed as deadlock detection.

The above program we saw helps us detect deadlock in Java. You can run that program in the command prompt and notice the Java thread Dump of the application to find deadlocks.

  • Type jps command to view the PID of the running process. Then type jmcd PID Thread.dump command to detect the deadlock. This dump file can also be retrieved in the form of a text file.
  • Deadlock cannot be completely avoided, yet, we can try our best to minimize it.

Ways to minimize Deadlocks in Java

Here are a few ways to minimize deadlocks:

  • Avoid Nested locks
  • Avoid Unnecessary locks
  • Use Thread join

1. Avoid Nested locks:

The presence of nested locks is the main cause of deadlock occurrence. Once we avoid nested locks, deadlocks can be prevented to a greater extent.

Therefore, we must avoid giving access to many threads at the same time.

2. Avoid Unnecessary locks:

Providing access to threads that do not require the resources should be avoided. Giving such permissions to unwanted threads will prevent important threads from gaining access.

As this state also leads to a deadlock, unnecessary locks should be strictly avoided.

3. Using thread Joins:

Deadlock usually occurs when a thread is waiting for another thread to release the resource. In such circumstances, we can mention the maximum time in the thread.join() method. We can mention the approximate maximum time that it might take to complete the execution. This helps in avoiding the deadlock.

Conclusion:

Deadlocks cause destruction to business logic and break the code during the run time. Therefore, it is necessary to detect and avoid them in Java programming. This article must have given you various insights about deadlocks in Java. You can now try to avoid those in the multi-threaded environment.

Leave a Reply

Your email address will not be published. Required fields are marked *