Understanding The Deadlock Concept In Java

Posted By : Ved Prakash | 30-Apr-2018

What is Deadlock :

 

Deadlock is a situation where if two threads are waiting on each other to complete their execution. Deadlock is occurred due to the wrong usage of synchronized keyword. If first thread is calling a synchronized method by using a locked object of the second thread, and second thread calling a synchronized method by using a locked object of the first thread then a deadlock occurs.

 

In another way, we can define Deadlock as If two threads are waiting for each other forever such type of infinite waiting is called Deadlock. Synchronized keyword is the only reason for deadlock situation hence while using synchronized keyword we have to special care.

 

Deadlock vs Starvation:

 

Long waiting of a Thread where waiting never ends is called deadlock, whereas long waiting of a thread where waiting ends at a certain point is called starvation.

 

Example: Low priority thread has to wait until completing all high priority thread, it may be long waiting but ends at a certain point, which is nothing but Starvation. 

 

Deadlock Related to Scheduling: 

 

Since there is no any predefined order in which locks are acquired, there is another problem in this type of programming is that a particular thread never holds a lock, even though it seems that is should hold. 


Example-
Below application implements the above condition to show deadlock situation:

FirstClass.java

class FirstClass{
synchronized void firstClassMethod(SecondClass sc){
Strinh name = Thread.currentThread().getName();
Sysytem.out.println(name+"entered into FC.firstClassMethod()");
try{ 
thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
Sysytem.out.println(name+"is trying to call sc.lastMethod()");
sc.lastMethod();
}
synchronized void lastMethod(){
Sysytem.out.println("Inside FC.lastMethod()");
}
}              
SecondClass.java
class SecondClass{
synchronized void secondClassMethod(FirstClass fc){
Strinh name = Thread.currentThread().getName();
Sysytem.out.println(name+"is entered into sc.secondClassMethod()");
try{ 
thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
Sysytem.out.println(name+"is trying to call fc.lastMethod()");
fc.lastMethod();
}
synchronized void lastMethod(){
Sysytem.out.println("Inside sc.lastMethod()");
}
}    
DeadlockDemo.java
public class DeadlockDemo implements Runnable{
FirstClass fc = new FirstClass();
SecondClass sc = new SecondClass();
DeadlockDemo(){
Thread th = new Thread(this,"racing Thread");
th.start();
fc.firstClassMethod(sc);//main thread locked fc object
Sysytem.out.println("Back in main thread");
}
public void run(){
sc.secondClassMethod(fc);//racing thread locked sc object
Sysytem.out.println("Back in other thread");
}
} 
   

Test_DeadlockDemo.java

class Test_DeadlockDemo{
public static void main(String[] args){
new DeadlockDemo();
}
}
     

About Author

Author Image
Ved Prakash

Ved Prakash have good knowledge in core java(oops , Collections,Exception Handling,String Handling ) and basic knowledge of J2ee(Servlets and Jsp).

Request for Proposal

Name is required

Comment is required

Sending message..