-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeadlockDemo.java
More file actions
64 lines (58 loc) · 1.62 KB
/
Copy pathDeadlockDemo.java
File metadata and controls
64 lines (58 loc) · 1.62 KB
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
56
57
58
59
60
61
62
63
64
/* Run this application once on my pc, deadlock occured when count == 80.
Then the inner critical section can not be accessed by both threads while the app still on.
You can press control and 'c' key simultaneously to force terminating the app. */
public class DeadlockDemo{
private Object lock1 = new Object();
private Object lock2 = new Object();
public static volatile int count = 0;
public static final int MAX_COUNT = 10000;
public void instanceMethod1(){
synchronized(lock1){
synchronized(lock2){
System.out.println(Thread.currentThread().getName() + " in instanceMethod1." + count);
// Critical section guarded first by lock1 and then by lock2.
}
}
}
public void instanceMethod2(){
synchronized(lock2){
synchronized(lock1){
System.out.println(Thread.currentThread().getName() + " in instanceMethod2." + count);
// Critical section guarded first by lock2 and then by lock1.
}
}
}
public static void main(String[] args){
final DeadlockDemo dld = new DeadlockDemo();
Thread threadA = new Thread(new Runnable(){
@Override
public void run(){
while(count < MAX_COUNT){
dld.instanceMethod1();
try{
Thread.sleep(50);
}catch(InterruptedException ie){
ie.printStackTrace();
}
count++;
}
}
}, "Thread-A");
Thread threadB = new Thread(new Runnable(){
@Override
public void run(){
while(count < MAX_COUNT){
dld.instanceMethod2();
try{
Thread.sleep(50);
}catch(InterruptedException ie){
ie.printStackTrace();
}
count++;
}
}
}, "Thread-B");
threadA.start();
threadB.start();
}
}