Example of deadlock in Java Multi-threading

This is a simple example in multithreading demonstrating deadlock.

//deadlock
class ABC{
    String name;
    ABC(String name){
        this.name=name;
    }
    String getName(){
        return this.name;
    }
    synchronized void call(ABC caller){
        System.out.println(caller.getName()+” has called me”);
        System.out.println(“In call “+this);
        caller.callBack(this);
    }
    synchronized void callBack(ABC caller){
        System.out.println(caller.getName()+” has called back me”);
    }
   
}
class A implements Runnable{
    ABC ab, ac;
    A(ABC ab, ABC ac){
     this.ab=ab;
     this.ac=ac;
    }
    public void run(){
        System.out.println(“In Class A : run()”);
        ab.call(ac);
    }
}
class B implements Runnable{
    ABC ab, ac;
    B(ABC ab, ABC ac){
     this.ab=ab;
     this.ac=ac;
    }
    public void run(){
        System.out.println(“In Class B : run()”);
        ac.call(ab);
    }

}
class H{
    public static void main(String[] args){
        ABC a=new ABC(“One”);
        ABC b=new ABC(“Two”);
        A obj1=new A(a, b);
        B obj2=new B(a, b);
       
        Thread t1=new Thread(obj1);
        Thread t2=new Thread(obj2);
        t1.start();
   /*     try{
            Thread.sleep(2000);
        }
        catch(Exception e){
       
        }
*/
//enable the above block of try-catch code statements to avoid deadlock
        t2.start();
    }
}