Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/SynchronizerDeadlock.java
41152 views
1
/*
2
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @summary SynchronizerDeadlock creates threads that are deadlocked
26
* waiting for JSR-166 synchronizers.
27
* @author Mandy Chung
28
* @build Barrier
29
*/
30
31
import java.lang.management.*;
32
import java.util.*;
33
import java.util.concurrent.locks.*;
34
35
public class SynchronizerDeadlock {
36
37
private Lock a = new ReentrantLock();
38
private Lock b = new ReentrantLock();
39
private Lock c = new ReentrantLock();
40
private final int EXPECTED_THREADS = 3;
41
private Thread[] dThreads = new Thread[EXPECTED_THREADS];
42
private Barrier go = new Barrier(1);
43
private Barrier barr = new Barrier(EXPECTED_THREADS);
44
45
public SynchronizerDeadlock() {
46
dThreads[0] = new DeadlockingThread("Deadlock-Thread-1", a, b);
47
dThreads[1] = new DeadlockingThread("Deadlock-Thread-2", b, c);
48
dThreads[2] = new DeadlockingThread("Deadlock-Thread-3", c, a);
49
50
// make them daemon threads so that the test will exit
51
for (int i = 0; i < EXPECTED_THREADS; i++) {
52
dThreads[i].setDaemon(true);
53
dThreads[i].start();
54
}
55
}
56
57
void goDeadlock() {
58
// Wait until all threads have started
59
barr.await();
60
61
// reset for later signals
62
barr.set(EXPECTED_THREADS);
63
64
while (go.getWaiterCount() != EXPECTED_THREADS) {
65
synchronized(this) {
66
try {
67
wait(100);
68
} catch (InterruptedException e) {
69
// ignore
70
}
71
}
72
}
73
74
// sleep a little so that all threads are blocked before notified.
75
try {
76
Thread.sleep(100);
77
} catch (InterruptedException e) {
78
// ignore
79
}
80
go.signal();
81
82
}
83
84
void waitUntilDeadlock() {
85
barr.await();
86
87
for (int i=0; i < 100; i++) {
88
// sleep a little while to wait until threads are blocked.
89
try {
90
Thread.sleep(100);
91
} catch (InterruptedException e) {
92
// ignore
93
}
94
boolean retry = false;
95
for (Thread t: dThreads) {
96
if (t.getState() == Thread.State.RUNNABLE) {
97
retry = true;
98
break;
99
}
100
}
101
if (!retry) {
102
break;
103
}
104
}
105
}
106
107
private class DeadlockingThread extends Thread {
108
private final Lock lock1;
109
private final Lock lock2;
110
111
DeadlockingThread(String name, Lock lock1, Lock lock2) {
112
super(name);
113
this.lock1 = lock1;
114
this.lock2 = lock2;
115
}
116
public void run() {
117
f();
118
}
119
private void f() {
120
lock1.lock();
121
try {
122
barr.signal();
123
go.await();
124
g();
125
} finally {
126
lock1.unlock();
127
}
128
}
129
private void g() {
130
barr.signal();
131
lock2.lock();
132
throw new RuntimeException("should not reach here.");
133
}
134
}
135
136
void checkResult(long[] threads) {
137
if (threads.length != EXPECTED_THREADS) {
138
ThreadDump.threadDump();
139
throw new RuntimeException("Expected to have " +
140
EXPECTED_THREADS + " to be in the deadlock list");
141
}
142
boolean[] found = new boolean[EXPECTED_THREADS];
143
for (int i = 0; i < threads.length; i++) {
144
for (int j = 0; j < dThreads.length; j++) {
145
if (dThreads[j].getId() == threads[i]) {
146
found[j] = true;
147
}
148
}
149
}
150
boolean ok = true;
151
for (int j = 0; j < found.length; j++) {
152
ok = ok && found[j];
153
}
154
155
if (!ok) {
156
System.out.print("Returned result is [");
157
for (int j = 0; j < threads.length; j++) {
158
System.out.print(threads[j] + " ");
159
}
160
System.out.println("]");
161
162
System.out.print("Expected result is [");
163
for (int j = 0; j < threads.length; j++) {
164
System.out.print(dThreads[j] + " ");
165
}
166
System.out.println("]");
167
throw new RuntimeException("Unexpected result returned " +
168
" by findMonitorDeadlockedThreads method.");
169
}
170
}
171
}
172
173