forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT31.java
More file actions
59 lines (51 loc) · 1.68 KB
/
T31.java
File metadata and controls
59 lines (51 loc) · 1.68 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
package com.basic; /**
* @program JavaBooks
* @description: 死锁的模拟
* @author: mf
* @create: 2020/01/09 13:57
*/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 用线程池模拟,比较方便一些
*/
public class T31 {
private static Object res1 = new Object(); // 资源1
private static Object res2 = new Object(); // 资源2
public void m1() {
synchronized (res1) {
// 占用res1的锁
System.out.println(Thread.currentThread().getName() + " get res1");
try {
Thread.sleep(1000); // 延时一会,等待m2的方法占用res2
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("waiting get res2");
synchronized (res2) {
System.out.println(Thread.currentThread().getName() + " get res2");
}
}
}
public void m2() {
synchronized (res2) {
// 占用res2的锁
System.out.println(Thread.currentThread().getName() + " get res2");
try {
Thread.sleep(1000); //
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("waiting get res1");
synchronized (res1) {
System.out.println(Thread.currentThread().getName() + " get res1");
}
}
}
public static void main(String[] args) {
T31 t31 = new T31();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(() -> t31.m1());
service.execute(() -> t31.m2());
}
}