-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDeathLockExample.java
More file actions
54 lines (47 loc) · 1.51 KB
/
Copy pathDeathLockExample.java
File metadata and controls
54 lines (47 loc) · 1.51 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
package concurrent;
import java.util.concurrent.TimeUnit;
/**
* @author: mayuan
* @desc: 死锁的简单例子
* @date: 2018/09/16
*/
public class DeathLockExample {
public static void main(String[] args) {
final Object a = new Object();
final Object b = new Object();
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (a) {
try {
System.out.println("now in threadA lock a");
TimeUnit.SECONDS.sleep(2);
synchronized (b) {
System.out.println("now in threadA lock b");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread threadB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (b) {
try {
System.out.println("now in threadB lock b");
TimeUnit.SECONDS.sleep(2);
synchronized (a) {
System.out.println("now in threadB lock a");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
threadA.start();
threadB.start();
}
}