Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/AllThreadIds.java
41152 views
1
/*
2
* Copyright (c) 2003, 2020, 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
* @test
26
* @bug 4530538
27
* @key intermittent
28
* @summary Basic unit test of ThreadMXBean.getAllThreadIds()
29
* @author Alexei Guibadoulline and Mandy Chung
30
*
31
* @run main/othervm AllThreadIds
32
*/
33
34
import java.lang.management.*;
35
import java.text.MessageFormat;
36
import java.util.Arrays;
37
import java.util.HashSet;
38
import java.util.Set;
39
import java.util.concurrent.Phaser;
40
import java.util.function.Supplier;
41
42
public class AllThreadIds {
43
/**
44
* A supplier wrapper for the delayed format printing.
45
* The supplied value will have to be formatted as <em>$s</em>
46
* @param <T> The wrapped type
47
*/
48
private static final class ArgWrapper<T> {
49
private final Supplier<T> val;
50
51
public ArgWrapper(Supplier<T> val) {
52
this.val = val;
53
}
54
55
@Override
56
public String toString() {
57
T resolved = val.get();
58
return resolved != null ? resolved.toString() : null;
59
}
60
}
61
62
static final int DAEMON_THREADS = 20;
63
static final int USER_THREADS = 5;
64
static final int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
65
private static final boolean live[] = new boolean[ALL_THREADS];
66
private static final Thread allThreads[] = new Thread[ALL_THREADS];
67
private static final Set<Long> allThreadIds = new HashSet<>();
68
69
private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
70
private static boolean testFailed = false;
71
private static boolean trace = false;
72
73
private static long prevTotalThreadCount = 0;
74
private static long prevLiveTestThreadCount = 0;
75
private static int prevPeakThreadCount = 0;
76
77
private static final Phaser startupCheck = new Phaser(ALL_THREADS + 1);
78
79
private static void printThreadList() {
80
long[] list = mbean.getAllThreadIds();
81
for (int i = 1; i <= list.length; i++) {
82
System.out.println(i + ": Thread id = " + list[i-1]);
83
}
84
for (int i = 0; i < ALL_THREADS; i++) {
85
Thread t = allThreads[i];
86
System.out.println(t.getName() + " Id = " + t.getId() +
87
" die = " + live[i] +
88
" alive = " + t.isAlive());
89
}
90
}
91
92
private static void checkInitialState() throws Exception {
93
updateCounters();
94
checkThreadCount(0, 0);
95
}
96
97
private static void checkAllThreadsAlive() throws Exception {
98
updateCounters();
99
100
// Start all threads and wait to be sure they all are alive
101
for (int i = 0; i < ALL_THREADS; i++) {
102
setLive(i, true);
103
Thread thread = new MyThread(i);
104
thread.setDaemon(i < DAEMON_THREADS);
105
thread.start();
106
allThreadIds.add(thread.getId());
107
allThreads[i] = thread;
108
}
109
// wait until all threads are started.
110
startupCheck.arriveAndAwaitAdvance();
111
112
checkThreadCount(ALL_THREADS, 0);
113
if (trace) {
114
printThreadList();
115
}
116
// Check mbean now. All threads must appear in getAllThreadIds() list
117
Set<Long> currentThreadIds = new HashSet<>();
118
Arrays.stream(mbean.getAllThreadIds()).forEach(currentThreadIds::add);
119
120
if (!currentThreadIds.containsAll(allThreadIds)) {
121
testFailed = true;
122
if (trace) {
123
System.out.print(". TEST FAILED.");
124
}
125
}
126
if (trace) {
127
System.out.println();
128
}
129
}
130
131
private static void checkDaemonThreadsDead() throws Exception {
132
updateCounters();
133
134
// Stop daemon threads, wait to be sure they all are dead, and check
135
// that they disappeared from getAllThreadIds() list
136
for (int i = 0; i < DAEMON_THREADS; i++) {
137
setLive(i, false);
138
}
139
140
// make sure the daemon threads are completely dead
141
joinDaemonThreads();
142
143
// and check the reported thread count
144
checkThreadCount(0, DAEMON_THREADS);
145
146
// Check mbean now
147
long[] list = mbean.getAllThreadIds();
148
149
for (int i = 0; i < ALL_THREADS; i++) {
150
long expectedId = allThreads[i].getId();
151
boolean found = false;
152
boolean alive = (i >= DAEMON_THREADS);
153
154
if (trace) {
155
System.out.print("Looking for thread with id " + expectedId +
156
(alive ? " expected alive." : " expected terminated."));
157
}
158
for (int j = 0; j < list.length; j++) {
159
if (expectedId == list[j]) {
160
found = true;
161
break;
162
}
163
}
164
165
if (alive != found) {
166
testFailed = true;
167
}
168
if (trace) {
169
if (alive != found) {
170
System.out.println(" TEST FAILED.");
171
} else {
172
System.out.println();
173
}
174
}
175
}
176
}
177
178
private static void checkAllThreadsDead() throws Exception {
179
updateCounters();
180
181
// Stop all threads and wait to be sure they all are dead
182
for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
183
setLive(i, false);
184
}
185
186
// make sure the non-daemon threads are completely dead
187
joinNonDaemonThreads();
188
189
// and check the thread count
190
checkThreadCount(0, ALL_THREADS - DAEMON_THREADS);
191
}
192
193
private static void checkThreadCount(int numNewThreads,
194
int numTerminatedThreads) {
195
checkLiveThreads(numNewThreads, numTerminatedThreads);
196
checkPeakThreads(numNewThreads);
197
checkTotalThreads(numNewThreads);
198
checkThreadIds(numNewThreads, numTerminatedThreads);
199
}
200
201
private static void checkLiveThreads(int numNewThreads,
202
int numTerminatedThreads) {
203
int diff = numNewThreads - numTerminatedThreads;
204
long threadCount = mbean.getThreadCount();
205
long expectedThreadCount = prevLiveTestThreadCount + diff;
206
// Check that number of live test threads is no less
207
// than number of all threads returned by mbean.getThreadCount()
208
if (threadCount < expectedThreadCount) {
209
testFailed = true;
210
System.err.println(MessageFormat.format("Unexpected number of threads count %d." +
211
"The expected number is %d or greater", threadCount, expectedThreadCount));
212
}
213
}
214
215
private static void checkPeakThreads(int numNewThreads) {
216
long peakThreadCount = mbean.getPeakThreadCount();
217
long expectedThreadCount = Math.max(prevPeakThreadCount, numNewThreads);
218
if (peakThreadCount < expectedThreadCount) {
219
testFailed = true;
220
System.err.println(MessageFormat.format("Unexpected number of peak threads count %d." +
221
"The expected number is %d or greater", peakThreadCount, expectedThreadCount));
222
}
223
}
224
225
private static void checkTotalThreads(int numNewThreads) {
226
long totalThreadCount = mbean.getTotalStartedThreadCount();
227
long expectedThreadCount = prevTotalThreadCount + numNewThreads;
228
if (totalThreadCount < expectedThreadCount) {
229
testFailed = true;
230
System.err.println(MessageFormat.format("Unexpected number of total threads %d." +
231
"The expected number is %d or greater", totalThreadCount, expectedThreadCount));
232
}
233
}
234
235
private static void checkThreadIds(int numNewThreads, int numTerminatedThreads) {
236
int threadCount = mbean.getAllThreadIds().length;
237
int expectedThreadCount = numNewThreads - numTerminatedThreads;
238
if (threadCount < expectedThreadCount) {
239
testFailed = true;
240
System.err.println(MessageFormat.format("Unexpected number of threads %d." +
241
"The expected number is %d or greater", threadCount, expectedThreadCount));
242
}
243
}
244
245
private static long getTestThreadCount() {
246
return Thread.getAllStackTraces().keySet().stream().filter(
247
thread -> thread.isAlive() && allThreadIds.contains(thread.getId())).count();
248
}
249
250
private static void updateCounters() {
251
prevTotalThreadCount = mbean.getTotalStartedThreadCount();
252
prevLiveTestThreadCount = getTestThreadCount();
253
prevPeakThreadCount = mbean.getPeakThreadCount();
254
}
255
256
public static void main(String args[]) throws Exception {
257
if (args.length > 0 && args[0].equals("trace")) {
258
trace = true;
259
}
260
261
checkInitialState();
262
checkAllThreadsAlive();
263
checkDaemonThreadsDead();
264
checkAllThreadsDead();
265
266
if (testFailed)
267
throw new RuntimeException("TEST FAILED.");
268
269
System.out.println("Test passed.");
270
}
271
272
private static void joinDaemonThreads() throws InterruptedException {
273
for (int i = 0; i < DAEMON_THREADS; i++) {
274
allThreads[i].join();
275
}
276
}
277
278
private static void joinNonDaemonThreads() throws InterruptedException {
279
for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
280
allThreads[i].join();
281
}
282
}
283
284
private static void setLive(int i, boolean val) {
285
synchronized(live) {
286
live[i] = val;
287
}
288
}
289
290
private static boolean isLive(int i) {
291
synchronized(live) {
292
return live[i];
293
}
294
}
295
296
// The MyThread thread lives as long as correspondent live[i] value is true
297
private static class MyThread extends Thread {
298
int id;
299
300
MyThread(int id) {
301
this.id = id;
302
}
303
304
public void run() {
305
// signal started
306
startupCheck.arrive();
307
while (isLive(id)) {
308
try {
309
sleep(100);
310
} catch (InterruptedException e) {
311
System.out.println("Unexpected exception is thrown.");
312
e.printStackTrace(System.out);
313
testFailed = true;
314
}
315
}
316
}
317
}
318
}
319
320