forked from CMPundhir/JavaExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynchronizationDemo4DeadLock.java
37 lines (36 loc) · 1.07 KB
/
SynchronizationDemo4DeadLock.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
class SynchronizationDemo4DeadLock{
public static void main(String[] args){
final String res1 = "Icecream";
final String res2 = "Chocolate";
Thread t1 = new Thread(){
public void run(){
synchronized(res1){
System.out.println(res1+" is locked by "+getName());
try{Thread.sleep(200);}catch(Exception e){}
synchronized(res2){
System.out.println(res2+" is locked by "+getName());
try{Thread.sleep(200);}catch(Exception e){}
System.out.println(res2+" is released by "+getName());
}
System.out.println(res1+" is released by "+getName());
}
}
};
t1.start();
Thread t2 = new Thread(){
public void run(){
synchronized(res2){
System.out.println(res2+" is locked by "+getName());
try{Thread.sleep(200);}catch(Exception e){}
synchronized(res1){
System.out.println(res1+" is locked by "+getName());
try{Thread.sleep(200);}catch(Exception e){}
System.out.println(res1+" is released by "+getName());
}
System.out.println(res2+" is released by "+getName());
}
}
};
t2.start();
}
}