Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/FullThreadDump/Deadlock.java
38829 views
1
/*
2
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
*
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
*
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* - Neither the name of Oracle nor the names of its
16
* contributors may be used to endorse or promote products derived
17
* from this software without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/*
33
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
/*
42
*/
43
44
import java.util.concurrent.CyclicBarrier;
45
import java.util.concurrent.BrokenBarrierException;
46
import java.util.concurrent.locks.Lock;
47
import java.util.concurrent.locks.ReentrantLock;
48
import java.io.IOException;
49
50
/**
51
* This Deadlock class demonstrates the capability of performing
52
* deadlock detection programmatically within the application using
53
* the java.lang.management API.
54
*
55
* See ThreadMonitor.java for the use of java.lang.management.ThreadMXBean
56
* API.
57
*/
58
public class Deadlock {
59
public static void main(String[] argv) {
60
new Deadlock();
61
62
// Now find deadlock
63
ThreadMonitor monitor = new ThreadMonitor();
64
boolean found = false;
65
while (!found) {
66
found = monitor.findDeadlock();
67
try {
68
Thread.sleep(500);
69
} catch (InterruptedException e) {
70
System.exit(1);
71
}
72
}
73
74
System.out.println("\nPress <Enter> to exit this Deadlock program.\n");
75
waitForEnterPressed();
76
}
77
78
79
private CyclicBarrier barrier = new CyclicBarrier(6);
80
public Deadlock() {
81
DeadlockThread[] dThreads = new DeadlockThread[6];
82
83
Monitor a = new Monitor("a");
84
Monitor b = new Monitor("b");
85
Monitor c = new Monitor("c");
86
dThreads[0] = new DeadlockThread("MThread-1", a, b);
87
dThreads[1] = new DeadlockThread("MThread-2", b, c);
88
dThreads[2] = new DeadlockThread("MThread-3", c, a);
89
90
Lock d = new ReentrantLock();
91
Lock e = new ReentrantLock();
92
Lock f = new ReentrantLock();
93
94
dThreads[3] = new DeadlockThread("SThread-4", d, e);
95
dThreads[4] = new DeadlockThread("SThread-5", e, f);
96
dThreads[5] = new DeadlockThread("SThread-6", f, d);
97
98
// make them daemon threads so that the test will exit
99
for (int i = 0; i < 6; i++) {
100
dThreads[i].setDaemon(true);
101
dThreads[i].start();
102
}
103
}
104
105
class DeadlockThread extends Thread {
106
private Lock lock1 = null;
107
private Lock lock2 = null;
108
private Monitor mon1 = null;
109
private Monitor mon2 = null;
110
private boolean useSync;
111
112
DeadlockThread(String name, Lock lock1, Lock lock2) {
113
super(name);
114
this.lock1 = lock1;
115
this.lock2 = lock2;
116
this.useSync = true;
117
}
118
DeadlockThread(String name, Monitor mon1, Monitor mon2) {
119
super(name);
120
this.mon1 = mon1;
121
this.mon2 = mon2;
122
this.useSync = false;
123
}
124
@Override
125
public void run() {
126
if (useSync) {
127
syncLock();
128
} else {
129
monitorLock();
130
}
131
}
132
private void syncLock() {
133
lock1.lock();
134
try {
135
try {
136
barrier.await();
137
} catch (InterruptedException e) {
138
e.printStackTrace();
139
System.exit(1);
140
} catch (BrokenBarrierException e) {
141
e.printStackTrace();
142
System.exit(1);
143
}
144
goSyncDeadlock();
145
} finally {
146
lock1.unlock();
147
}
148
}
149
private void goSyncDeadlock() {
150
try {
151
barrier.await();
152
} catch (InterruptedException e) {
153
e.printStackTrace();
154
System.exit(1);
155
} catch (BrokenBarrierException e) {
156
e.printStackTrace();
157
System.exit(1);
158
}
159
lock2.lock();
160
throw new RuntimeException("should not reach here.");
161
}
162
private void monitorLock() {
163
synchronized (mon1) {
164
try {
165
barrier.await();
166
} catch (InterruptedException e) {
167
e.printStackTrace();
168
System.exit(1);
169
} catch (BrokenBarrierException e) {
170
e.printStackTrace();
171
System.exit(1);
172
}
173
goMonitorDeadlock();
174
}
175
}
176
private void goMonitorDeadlock() {
177
try {
178
barrier.await();
179
} catch (InterruptedException e) {
180
e.printStackTrace();
181
System.exit(1);
182
} catch (BrokenBarrierException e) {
183
e.printStackTrace();
184
System.exit(1);
185
}
186
synchronized (mon2) {
187
throw new RuntimeException(getName() + " should not reach here.");
188
}
189
}
190
}
191
192
class Monitor {
193
String name;
194
Monitor(String name) {
195
this.name = name;
196
}
197
}
198
199
private static void waitForEnterPressed() {
200
try {
201
boolean done = false;
202
while (!done) {
203
char ch = (char) System.in.read();
204
if (ch<0||ch=='\n') {
205
done = true;
206
}
207
}
208
}
209
catch (IOException e) {
210
e.printStackTrace();
211
System.exit(0);
212
}
213
}
214
}
215
216