-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeadLockDemo.java
55 lines (44 loc) · 961 Bytes
/
DeadLockDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class A{
public synchronized void d1(B b){
System.out.println("Thread-1 starts execution");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e){}
System.out.println("Thread-1 trying to call B's last method");
b.last();
}
public synchronized void last(){
System.out.println("Inside A, This is last method");
}
}
class B{
public synchronized void d2(A a){
System.out.println("Thread-2 starts execution");
try{
Thread.sleep(2000);
}
catch(InterruptedException e){}
System.out.println("Thread-2 trying to call A's last method");
a.last();
}
public synchronized void last(){
System.out.println("Inside B, This is last method");
}
}
class DeadLockDemo extends Thread{
A a=new A();
B b=new B();
void m1(){
this.start();
a.d1(b);//This line is executed by main thread.
}
public void run(){
b.d2(a);
}
public static void main(String[] args) {
DeadLockDemo d=new DeadLockDemo();
d.m1();
}
}