Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ResetPeakThreadCount.java
41152 views
1
/*
2
* Copyright (c) 2003, 2021, 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 4892507 8020875 8021335
27
* @summary Basic Test for the following reset methods:
28
* - ThreadMXBean.resetPeakThreadCount()
29
* @author Mandy Chung
30
* @author Jaroslav Bachorik
31
*
32
* @build ResetPeakThreadCount
33
* @build ThreadDump
34
* @run main/othervm ResetPeakThreadCount
35
*/
36
37
import java.lang.management.*;
38
import java.util.Iterator;
39
import java.util.LinkedList;
40
import java.util.List;
41
42
public class ResetPeakThreadCount {
43
// initial number of new threads started
44
private static final int DAEMON_THREADS_1 = 80;
45
46
// Terminate half of the threads started
47
private static final int TERMINATE_1 = 40;
48
49
// start new threads but expected the peak unchanged
50
private static final int DAEMON_THREADS_2 = 20;
51
52
// peak thread count reset before starting new threads
53
private static final int DAEMON_THREADS_3 = 20;
54
55
// barrier for threads communication
56
private static final Barrier barrier = new Barrier(DAEMON_THREADS_1);
57
58
private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
59
private static volatile boolean testFailed = false;
60
61
private static final List<MyThread> threads = new LinkedList<>();
62
private static final Object liveSync = new Object();
63
64
public static void main(String[] argv) throws Exception {
65
// System threads can be started/terminated during the test execution,
66
// and they affect resetPeakThreadCount result.
67
resetPeak();
68
69
startThreads(DAEMON_THREADS_1);
70
71
int beforeTerminate = checkPeakThreadCount(threads.size() + 1, -1); // + 1 for the current thread
72
73
terminateThreads(TERMINATE_1);
74
75
// the value should not decrease
76
int afterTerminate = checkPeakThreadCount(beforeTerminate, -1);
77
78
startThreads(DAEMON_THREADS_2);
79
80
// expected peak is unchanged
81
checkPeakThreadCount(-1, afterTerminate);
82
83
// reset peak and ensure new threads increase the value
84
int beforeThreads3 = resetPeak();
85
startThreads(DAEMON_THREADS_3);
86
87
checkPeakThreadCount(threads.size() + 1, -1); // + 1 for the current thread
88
checkPeakThreadCount(beforeThreads3, -1);
89
90
if (testFailed) {
91
throw new RuntimeException("TEST FAILED.");
92
}
93
94
System.out.println("Test passed");
95
}
96
97
private static void startThreads(int count) throws InterruptedException {
98
// get current peak thread count
99
int peak1 = mbean.getPeakThreadCount();
100
101
// Start threads and wait to be sure they all are alive
102
System.out.println("Starting " + count + " threads....");
103
barrier.set(count);
104
synchronized (liveSync) {
105
for (int i = 0; i < count; i++) {
106
MyThread newThread = new MyThread();
107
threads.add(newThread);
108
newThread.start();
109
}
110
}
111
// wait until all threads have started.
112
barrier.await();
113
114
// get peak thread count after daemon threads have started
115
int peak2 = mbean.getPeakThreadCount();
116
117
System.out.println(" Current = " + mbean.getThreadCount() +
118
" Peak before = " + peak1 + " after: " + peak2);
119
120
int current = mbean.getThreadCount();
121
System.out.println(" Live thread count before returns " + current);
122
}
123
124
private static void terminateThreads(int count) throws InterruptedException {
125
// get current peak thread count
126
int peak1 = mbean.getPeakThreadCount();
127
128
// Stop daemon threads and wait to be sure they all are dead
129
System.out.println("Terminating " + count + " threads....");
130
barrier.set(count);
131
synchronized(liveSync) {
132
Iterator<MyThread> iter = threads.iterator();
133
for (int i = 0; i < count; i++) {
134
MyThread thread = iter.next();
135
thread.live = false;
136
}
137
liveSync.notifyAll();
138
}
139
// wait until daemon threads terminated.
140
barrier.await();
141
142
// get peak thread count after daemon threads have terminated
143
int peak2 = mbean.getPeakThreadCount();
144
// assuming no system thread is added
145
if (peak2 != peak1) {
146
throw new RuntimeException("Current Peak = " + peak2 +
147
" Expected to be = previous peak = " + peak1);
148
}
149
150
for (int i = 0; i < count; i++) {
151
MyThread thread = threads.remove(0);
152
thread.join();
153
}
154
155
int current = mbean.getThreadCount();
156
System.out.println(" Live thread count before returns " + current);
157
}
158
159
// Returns peak thread value after reset.
160
private static int resetPeak() {
161
int peak3 = mbean.getPeakThreadCount();
162
int current = mbean.getThreadCount();
163
164
// Reset peak thread count
165
mbean.resetPeakThreadCount();
166
167
int afterResetPeak = mbean.getPeakThreadCount();
168
int afterResetCurrent = mbean.getThreadCount();
169
System.out.println("Reset peak before = " + peak3 +
170
" current = " + current +
171
" after reset peak = " + afterResetPeak +
172
" current = " + afterResetCurrent);
173
return afterResetPeak;
174
}
175
176
private static void fail(String msg) {
177
ThreadDump.threadDump();
178
throw new RuntimeException(msg);
179
}
180
181
private static int checkPeakThreadCount(int min, int max) {
182
int value = mbean.getPeakThreadCount();
183
if (min > 0 && value < min) {
184
fail("***** Unexpected thread count: " + value + ", minimum expected " + min + " *****");
185
}
186
if (max > 0 && value > max) {
187
fail("***** Unexpected thread count: " + value + ", maximum expected " + max + " *****");
188
}
189
return value;
190
}
191
192
193
// The MyThread thread lives as long as correspondent its live value is true.
194
private static class MyThread extends Thread {
195
volatile boolean live;
196
197
MyThread() {
198
live = true;
199
setDaemon(true);
200
}
201
202
public void run() {
203
// signal started
204
barrier.signal();
205
synchronized(liveSync) {
206
while (live) {
207
try {
208
liveSync.wait(100);
209
} catch (InterruptedException e) {
210
System.out.println("Unexpected exception is thrown.");
211
e.printStackTrace(System.out);
212
testFailed = true;
213
}
214
}
215
}
216
// signal about to exit
217
barrier.signal();
218
}
219
}
220
221
}
222
223