Java | Deadlock

05 min read

Deadlock:

It occurs when two threads have a circular dependency on a pair of synchronized objects. Eg., If the thread in X tries to call any synchronized method on object Y, it will block as expected. However, if the thread in Y, in turn, 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 would complete. If your multithreaded program locks up occasionally, deadlock is one of the first conditions that you should check for.

 

Deadlocked program:

class A
{
void method 1 (String s)
{
System.out.println(s+" is executing method1 now");
}
void method2(String s)
{
System.out.println(s+ " is executing method2 now");
}
}
class thread1 extends Thread
{
A obj1,obj2; thread1(A obj1,A obj2)
{
this.obj 1=obj 1;
this.obj2=obj2;
}
public void run( )
{
synchronized(obj 1)
{
String s=getName();
System.out.println(s+ " is trying to access method1");
obj 1 .method 1(s);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(s+ " is trying to access method2");
synchronized(obj2)
{
obj2.method2(s);
}
} //while holding lock on obj 1, it tries to acquire lock on obj2 also. Same way, other }thread too will try to get lock on objl, while already holding lock on obj2. This leads to deadlock.
}
}
class thread2 extends Thread
{
A objl,obj2;
thread2(A obj 1,A obj2)
{
this. obj 1=obj 1;
this.obj2=obj2;
}
public void run()
{
synchronized(obj2)
{
String s=getName();
System.out.println(s+ " is trying to access method2");
obj2.method2(s);
try
{
Thread.sleep(2000);
}
catch(lnterruptedException e)
{
System .out.println(e);
}
System.out.println(s+" is trying to access method 1 ");
synchronized(obj1)
{
obj1.method1(s);
}
}
}
}
class deadlockdemo
{
public static void main(String[] args)
{
A obj1=new A( );
A obj2=new A( );
thread1 t1 =new thread 1 (obj1,obj2);
thread2 t2=new thread2(obj1,obj2);
t1 .start( );
t2.start( );
}
}

Output:

Thread-0 is trying to access method1
Thread-1 is trying to access method1
Thread-1 is executing method2 now
Thread-0 is executing method1 now
Thread-0 is trying to access method2 
Thread-1 is trying to access method1
The two threads will be blocked for ever without getting terminated

 

Program with no deadlock:

class A
{
void method 1(String s)
{
System.out.println(s+" is executing method 1 now");
}
void method2(String s)
{
System.out.println(s+ " is executing method2 now");
}
}
class thread 1 extends Thread
{
A obj1,obj2;
thread1(A obj1, A obj2)
this.obj 1=obj 1;
this.obj2=obj2;
}
public void run( )
{
synchronized(obj1)
{
String s = getName( );
System.out.println(s+ "is trying to access method1");
obj1.method1(s);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(s+"is trying to access method2");
} ---> Note the lock on obj1 by thread1 is released before getting lock on obj2.
synchronized(obj2)
{
String s = getName( );
obj2.method2(s);
}
}
}
class thread2 extends Thread
{
A obj1,obj2;
thread2(A obj1, A obj2)
{
this.obj1=obj1;
this.obj2=obj2;
}
public void run( )
{
synchronized(obj 2)
{
String s = getName( );
System.out.println(s+"is trying to access method2");
obj2.method2(s);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(s+"trying to access method1");
} ---> Note the lock on obj2 by thread2 is released before getting lock on obj1.
synchronized(obj 1 )
{
String s=getName();
obj 1.method 1(s);
}
}
}
class nodeadlock
{
public static void main(String[] args)
A obj 1=new A();
A obj2=new A();
thread1 t1=new thread1 (obj 1 ,obj2);
thread2 t2=new thread2(obj2,obj 1);
t1 .start();
t2. start();
}
}

Output:

Thread-0 is trying to access method1
Thread-1 is trying to access method2
Thread-1 is executing method2 now
Thread-0 is executing method1 now
Thread-1 is trying to access method1
Thread-0 is trying to access method2
Thread-0 is executing method2 now
Thread-1 is executing method1 now
Then the 2 threads terminate their execution.
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error