Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
109 lines (90 loc) · 2.54 KB

File metadata and controls

109 lines (90 loc) · 2.54 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author xjn
* @since 2020-05-22
*/
public class DeadLock2 {
public static void main1(String[] args) throws Exception {
ReentrantLock lockA = new ReentrantLock();
ReentrantLock lockB = new ReentrantLock();
Runnable runnable1 = () -> {
lockB.lock();
try {
Thread.sleep(100);
} catch (Exception e) {
}
lockA.lock();
System.out.println("获取锁A..释放锁B");
lockA.unlock();
lockB.unlock();
};
Runnable runnable2 = () -> {
lockA.lock();
lockB.lock();
try {
Thread.sleep(100);
} catch (Exception e) {
}
System.out.println("获取锁B..释放锁A");
lockB.unlock();
lockA.unlock();
;
};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
}
//A
//B
//C
//AB异步 C在AB执行完成之后执行
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private volatile int flag = 0;
public void printA() throws Exception {
System.out.println("A");
flag++;
}
public void printB() throws Exception {
System.out.println("B");
flag++;
}
public void printC() {
while (flag != 2) {
}
System.out.println("C");
}
public static void main(String[] args) {
DeadLock2 deadLock2 = new DeadLock2();
Runnable a = () -> {
try {
deadLock2.printA();
} catch (Exception e) {
}
};
Runnable b = () -> {
try {
deadLock2.printB();
} catch (Exception e) {
}
};
Runnable c = () -> {
try {
deadLock2.printC();
} catch (Exception e) {
}
};
// Thread threadA = new Thread(a);
// Thread threadB = new Thread(b);
// Thread threadC = new Thread(c);
// threadA.start();
// threadB.start();
// threadC.start();
CompletableFuture.allOf(CompletableFuture.runAsync(a), CompletableFuture.runAsync(b))
.thenRun(c);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.