Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – DeadLock

Java Programming Language DeadLock State in Multithreading

Thread1 has acquired monitor on object Obj-1Thread2 has acquired monitor on object Obj-2.

Thread-1 is trying to get monitor acquired by Thread-2 whereas Thread-2 is trying to get monitor acquired by Thread-1.

But according to synchronization rule, one thread can get a monitor of another thread only if another thread has released its monitor.

Here Thread-1 is waiting to release monitor by Thread-2 whereas Thread-2 is waiting to release monitor by Thread-1.

In this situation, none of the thread is able to release its own monitor or get another’s, monitor.

Therefore they remain forever in waiting state. Therefore program is not able to execute further.

This situation is known as Deal-lock situation.

Example

 class XYZ
 {
     public synchronized void m1(ABC a)
     {
         System.out.println(Thread.currentThread().getName()+" is Executing");
                                                
         try
         {
             Thread.sleep(10);
         }
         catch(InterruptedException e)
         {
             e.printStackTrace();
         }
         System.out.println(Thread.currentThread().getName()+" Calls ABC class m() method");
                                           
         a.m();
     }
     
     public synchronized void m()
     {
         System.out.println("Inside XYZ class m() method");
     }
 }

 class ABC
 {
     public synchronized void m2(XYZ x)
     {
         System.out.println(Thread.currentThread().getName()+" is Executing");
                                               
         try
         {
             Thread.sleep(10);
         }
         catch(InterruptedException e)
         {
             e.printStackTrace();
         }
         System.out.println(Thread.currentThread().getName()+" Calls XYZ class m() method");
                                    
        x.m();      
     }
     
     public synchronized void m()
     {
         System.out.println("Inside ABC class m() method");
     }
 }

 class Thread1 extends Thread
 {
     XYZ x;
     ABC a;
  
     public Thread1(XYZ x, ABC a)
     {
         this.x = x;
         this.a = a;
     }
     
      public void run()
      {
          Thread.currentThread().setName("Thread1");
          x.m1(a);  
      }
 }

 class Thread2 extends Thread
 {
     XYZ x;
     ABC a;
     
     public Thread2(XYZ x,ABC a)
     {
         this.x = x;
         this.a = a;
     }
     
     public void run()
     {
         Thread.currentThread().setName("Thread2");
         a.m2(x);  
     }
 }

 class DeadLockDemo
 {
     public static void main(String args[])
     {
         XYZ x = new XYZ();
         ABC a = new ABC();
         Thread1 t1 = new Thread1(x,a);
         Thread2 t2 = new Thread2(x,a);
         t1.start();
         t2.start();
     }
 }

Output

Thread2 is Executing
Thread1 is Executing
Thread2 Calls XYZ class m() method
Thread1 Calls ABC class m() method

Try to run this program by removing synchronized keyword from any one or both the m() methods from class XYZ & class ABC.

 

Leave a reply