Saturday 7 April 2012

Thread Deadlock in Java

Thread Deadlock is a special type of error that every Java programmer should try to avoid . Thread deadlock is specially related to multitasking ( i.e. Multithreaded programming ) in java . For example, suppose one thread enters the monitor on object X and another thread enters the monitor on object Y. If the thread in X tries to call any synchronized method on Y, it will be blocked . However, if the thread in Y at the same time tries to call any synchronized method on X, the thread waits forever, because to access X, it would have to release its own lock on Y so that the first thread could complete. Deadlock is a difficult error to debug.


Though in general, deadlock occurs rarely, when the two threads try to access synchronized methods defined by each other. It may involve more than two threads and two synchronized objects.


The below java code creates two classes, A and B, with methods dl1( ) and dl2( ), respectively, which pause briefly before trying to call a method in the other class. The main class, named Deadlock, creates instances of A and a B and then starts threads to set up the deadlock condition. The dl1( ) and dl2( ) methods use Thread.sleep()  as a way to force the deadlock condition to occur.



//Program to demonstrate Thread Deadlock

class A{
     synchronized void dl1(B b){
            String name=Thread.currentThread().getName();
            System.out.println(name +" entered A.");
            try{
                 Thread.sleep(1000);  // Stops the executing thread for 1000 ms
                }catch(InterruptedException e){
                        System.out.println(" A Interrupted.");
                    }
                b.last();
                System.out.println(name+"Trying to call last of B.");
       }
        public synchronized void last(){
                System.out.println("Inside A's last.");
            }
        }
class B{
        synchronized void dl2(A a){
             String name=Thread.currentThread().getName();
             System.out.println(name+" entered B");
             try{
                    Thread.sleep(1000);
             }catch(InterruptedException e){
                  System.out.println("B Interrupted.");
              }
              a.last();
              System.out.println(name+" trying to call last of A.");
            }
          public synchronized void last(){
                System.out.println("Inside B's last.");
            }
        }
public class deadlock implements Runnable{
        A a=new A();
        B b=new B();
        deadlock() {
                Thread.currentThread().setName("Main Thread.");
                Thread t=new Thread(this,"Racing Thread.");
                t.start();
                a.dl1(b);
                System.out.println("Back in Main Thread.");
            }
            public void run(){
                    b.dl2(a);
                    System.out.println("Back in other thread.");
                }
            public static void main(String args[]){
                    new deadlock();
                }
            }


P.S. : When running this code in Blu-J ( Win 7 x86 ) your system may be in the 'Stopped Responding' state. :P  Then you'll have to manually kill the process......... :)              


Hope this helps ....... :)


2 comments:

  1. There is no need to create a seperate thread in constructor of deadlock class. This is because it already has a main thread running. So there's no need of thread object. It can be done directly.

    ReplyDelete
  2. hmm !!!! but newbees may not be able to understand the anonymous main thread running !!!!! so I explicitly created the 'Main Thread' .........

    ReplyDelete