Introduction To Deadlock In Java

In this article, we will discuss Deadlock in Java. Deadlock is a condition where two or more threads are blocked forever, waiting for each other to release the resource (for execution) but never get the proper resource to execute.

Introduction
 
In this article we will discuss a common problem; "Deadlock in Java" and also discuss the necessary conditions, deadlock handling, an example that shows a deadlock and describe some points of how to overcome a deadlock.
 
What is deadlock?
 
Deadlock means a lock that has no key. Deadlock is a condition where two or more threads are blocked forever, waiting for each other to release the resource (for execution) but never get the proper resource to execute. The following figure illustrates the problem.
 
fig2.jpg
 
In this figure we have two threads (thread 1 and thread 2). Thread1 is for a printing operation and thread2 is for I/O operations. Now thread1 (the printing operation) needs the keyboard to get the command for printing some data or an image and thread2 (I/O operations) needs a printer to show the output. So, in this example  the problem exists that both needs a resource that the other needs to execute properly, but they can never reconcile the problem so the result is called a deadlock. A deadlock is a condition in which a process waits for a resource they can never get.
 
Deadlock requirements
 
The following conditions are required for a deadlock:
  1. Mutual Exclusion
  2. Hold and Wait
  3. No Preemption
  4. Circular wait
Mutual exclusion
 
In this condition a minimum of one resource is a non-shareable type; a resource that only one process can use at a time.
 
Hold and Wait
 
In this, a process is already holding a resource and then demands another resource that is already used by another process.
 
No Preemption
 
When one process is allocated to hold the resource until the work is not finished. So no re-allocation is allowed.
 
Circular wait
 
A process must wait for a resource held by another process that is in turn waiting for another process to release that resource. In general, there is a set of waiting processes, Pr = {Pr1, Pr2.. PrN}, such that Pr1 is waiting for a resource held by Pr2, Pr2 is waiting for a resource held by Pr3 and so on until PrN is waiting for a resource held by Pr1.
 
Deadlock Handling
  • Ignoring deadlock
  • Detection
  • Prevention
       1. Removing the mutual exclusion condition
       2. The hold and wait or resource holding conditions may be removed
       3. Remove no preemption condition
       4. Remove circular wait condition.
  • Avoidance
  • Livelock
Example
 
To proper understand the deadlock condition we must see an example of what happens in a deadlock. In this example we create a class DeadlockEx, that contains a runnable interface and use another thread pokedBack( ) for the deadlock condition to occur.
 
When pokedBack starts running, meaning the deadlock starts running, then both the other threads stop working since they tried to invoke pokedBack. Hence both are blocked and will never exit because each thread is waiting for the other to exit poke (i.e both threads goes into a deadlock state).
  1. public class DeadlockEx  
  2. {  
  3.     static class Group   
  4.     {  
  5.       private final String name;  
  6.       public Group(String name)  
  7.       {  
  8.           this.name = name;  
  9.       }  
  10.       public String getName()  
  11.       {  
  12.           return this.name;  
  13.       }  
  14.       public synchronized void poke(Group poked)  
  15.       {  
  16.           System.out.format("%s: %s" + " poked me !%n"this.name, poked.getName());  
  17.           poked.pokedBack(this);  
  18.       }  
  19.       public synchronized void pockedBack(Group poked)   
  20.       {  
  21.           System.out.format("%s: %s" + " pocked me !%n"this.name, poked.getName());  
  22.       }  
  23.     }  
  24.     public static void main(String[] args)   
  25.     {  
  26.       final Group john =new Group("John");  
  27.       final Group sam =new Group("Sam");  
  28.       new Thread(new Runnable()  
  29.       {  
  30.           public void run()   
  31.           {   
  32.              john.poke(sam);   
  33.           }  
  34.       }).start();  
  35.       new Thread(new Runnable()   
  36.       {  
  37.           public void run()   
  38.           {  
  39.               sam.poke(john);  
  40.           }  
  41.       }).start();  
  42.     }    
  43. }
Output
 
fig-1.jpg
 
Note: We are making a deadlocked program, so we need to press "CTRL+C" to close (end) the program.
 
How to overcome a deadlock
  • If you provide the method in a proper order then the problem will be resolved.
  • Usually, the simplest and most efficient way to avoid deadlock is to ensure that resources are always acquired in some well-defined order.
  • Make synchronization's scope to as small as possible.
  • Maintain a proper documentation about the order, and other locking strategy if used.
  • Avoid multiple object locking
  • Use a consistent lock order, by which if multiple locks need to be acquired then they will be acquired in a particular predefined order.