author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5808 | 3a1f603c5ca7 |
child 29495 | e0d12d371446 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4530538 |
|
27 |
* @summary Basic unit test of ThreadMXBean.getAllThreadIds() |
|
28 |
* @author Alexei Guibadoulline and Mandy Chung |
|
29 |
* |
|
30 |
* @run build Barrier |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
31 |
* @run main/othervm AllThreadIds |
2 | 32 |
*/ |
33 |
||
34 |
import java.lang.management.*; |
|
35 |
import java.util.*; |
|
36 |
||
37 |
public class AllThreadIds { |
|
38 |
final static int DAEMON_THREADS = 20; |
|
39 |
final static int USER_THREADS = 5; |
|
40 |
final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS; |
|
41 |
private static volatile boolean live[] = new boolean[ALL_THREADS]; |
|
42 |
private static Thread allThreads[] = new Thread[ALL_THREADS]; |
|
43 |
private static ThreadMXBean mbean |
|
44 |
= ManagementFactory.getThreadMXBean(); |
|
45 |
private static boolean testFailed = false; |
|
46 |
private static boolean trace = false; |
|
47 |
||
48 |
private static long prevTotalThreadCount = 0; |
|
49 |
private static int prevLiveThreadCount = 0; |
|
50 |
private static int prevPeakThreadCount = 0; |
|
51 |
private static long curTotalThreadCount = 0; |
|
52 |
private static int curLiveThreadCount = 0; |
|
53 |
private static int curPeakThreadCount = 0; |
|
54 |
||
55 |
// barrier for threads communication |
|
56 |
private static Barrier barrier = new Barrier(ALL_THREADS); |
|
57 |
||
58 |
private static void printThreadList() { |
|
59 |
if (!trace) return; |
|
60 |
||
61 |
long[] list = mbean.getAllThreadIds(); |
|
62 |
for (int i = 1; i <= list.length; i++) { |
|
63 |
System.out.println(i + ": Thread id = " + list[i-1]); |
|
64 |
} |
|
65 |
for (int i = 0; i < ALL_THREADS; i++) { |
|
66 |
Thread t = allThreads[i]; |
|
67 |
System.out.println(t.getName() + " Id = " + t.getId() + |
|
68 |
" die = " + live[i] + |
|
69 |
" alive = " + t.isAlive()); |
|
70 |
} |
|
71 |
} |
|
72 |
||
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
73 |
private static void fail(String msg) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
74 |
trace = true; |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
75 |
printThreadList(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
76 |
throw new RuntimeException(msg); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
77 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
78 |
|
2 | 79 |
private static void checkThreadCount(int numNewThreads, |
80 |
int numTerminatedThreads) |
|
81 |
throws Exception { |
|
82 |
prevTotalThreadCount = curTotalThreadCount; |
|
83 |
prevLiveThreadCount = curLiveThreadCount; |
|
84 |
prevPeakThreadCount = curPeakThreadCount; |
|
85 |
curTotalThreadCount = mbean.getTotalStartedThreadCount(); |
|
86 |
curLiveThreadCount = mbean.getThreadCount(); |
|
87 |
curPeakThreadCount = mbean.getPeakThreadCount(); |
|
88 |
||
89 |
if ((curLiveThreadCount - prevLiveThreadCount) != |
|
90 |
(numNewThreads - numTerminatedThreads)) { |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
91 |
fail("Unexpected number of live threads: " + |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
92 |
" Prev live = " + prevLiveThreadCount + |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
93 |
" Current live = " + curLiveThreadCount + |
2 | 94 |
" Threads added = " + numNewThreads + |
95 |
" Threads terminated = " + numTerminatedThreads); |
|
96 |
} |
|
97 |
if (curPeakThreadCount - prevPeakThreadCount != numNewThreads) { |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
98 |
fail("Unexpected number of peak threads: " + |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
99 |
" Prev peak = " + prevPeakThreadCount + |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
100 |
" Current peak = " + curPeakThreadCount + |
2 | 101 |
" Threads added = " + numNewThreads); |
102 |
} |
|
103 |
if (curTotalThreadCount - prevTotalThreadCount != numNewThreads) { |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
104 |
fail("Unexpected number of total threads: " + |
2 | 105 |
" Prev Total = " + prevTotalThreadCount + |
106 |
" Current Total = " + curTotalThreadCount + |
|
107 |
" Threads added = " + numNewThreads); |
|
108 |
} |
|
109 |
long[] list = mbean.getAllThreadIds(); |
|
110 |
if (list.length != curLiveThreadCount) { |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
111 |
fail("Array length returned by " + |
2 | 112 |
"getAllThreadIds() = " + list.length + |
113 |
" not matched count = " + curLiveThreadCount); |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
public static void main(String args[]) throws Exception { |
|
118 |
if (args.length > 0 && args[0].equals("trace")) { |
|
119 |
trace = true; |
|
120 |
} |
|
121 |
||
122 |
curTotalThreadCount = mbean.getTotalStartedThreadCount(); |
|
123 |
curLiveThreadCount = mbean.getThreadCount(); |
|
124 |
curPeakThreadCount = mbean.getPeakThreadCount(); |
|
125 |
checkThreadCount(0, 0); |
|
126 |
||
127 |
||
128 |
// Start all threads and wait to be sure they all are alive |
|
129 |
barrier.set(ALL_THREADS); |
|
130 |
for (int i = 0; i < ALL_THREADS; i++) { |
|
131 |
live[i] = true; |
|
132 |
allThreads[i] = new MyThread(i); |
|
133 |
allThreads[i].setDaemon( (i < DAEMON_THREADS) ? true : false); |
|
134 |
allThreads[i].start(); |
|
135 |
} |
|
136 |
// wait until all threads are started. |
|
137 |
barrier.await(); |
|
138 |
||
139 |
||
140 |
checkThreadCount(ALL_THREADS, 0); |
|
141 |
printThreadList(); |
|
142 |
||
143 |
// Check mbean now. All threads must appear in getAllThreadIds() list |
|
144 |
long[] list = mbean.getAllThreadIds(); |
|
145 |
||
146 |
for (int i = 0; i < ALL_THREADS; i++) { |
|
147 |
long expectedId = allThreads[i].getId(); |
|
148 |
boolean found = false; |
|
149 |
||
150 |
if (trace) { |
|
151 |
System.out.print("Looking for thread with id " + expectedId); |
|
152 |
} |
|
153 |
for (int j = 0; j < list.length; j++) { |
|
154 |
if (expectedId == list[j]) { |
|
155 |
found = true; |
|
156 |
break; |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
if (!found) { |
|
161 |
testFailed = true; |
|
162 |
} |
|
163 |
if (trace) { |
|
164 |
if (!found) { |
|
165 |
System.out.print(". TEST FAILED."); |
|
166 |
} |
|
167 |
System.out.println(); |
|
168 |
} |
|
169 |
} |
|
170 |
if (trace) { |
|
171 |
System.out.println(); |
|
172 |
} |
|
173 |
||
174 |
// Stop daemon threads, wait to be sure they all are dead, and check |
|
175 |
// that they disappeared from getAllThreadIds() list |
|
176 |
barrier.set(DAEMON_THREADS); |
|
177 |
for (int i = 0; i < DAEMON_THREADS; i++) { |
|
178 |
live[i] = false; |
|
179 |
} |
|
180 |
// wait until daemon threads are terminated. |
|
181 |
barrier.await(); |
|
182 |
||
183 |
// give chance to threads to terminate |
|
184 |
pause(); |
|
185 |
checkThreadCount(0, DAEMON_THREADS); |
|
186 |
||
187 |
// Check mbean now |
|
188 |
list = mbean.getAllThreadIds(); |
|
189 |
||
190 |
for (int i = 0; i < ALL_THREADS; i++) { |
|
191 |
long expectedId = allThreads[i].getId(); |
|
192 |
boolean found = false; |
|
193 |
boolean live = (i >= DAEMON_THREADS); |
|
194 |
||
195 |
if (trace) { |
|
196 |
System.out.print("Looking for thread with id " + expectedId + |
|
197 |
(live ? " expected alive." : " expected terminated.")); |
|
198 |
} |
|
199 |
for (int j = 0; j < list.length; j++) { |
|
200 |
if (expectedId == list[j]) { |
|
201 |
found = true; |
|
202 |
break; |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
if (live != found) { |
|
207 |
testFailed = true; |
|
208 |
} |
|
209 |
if (trace) { |
|
210 |
if (live != found) { |
|
211 |
System.out.println(" TEST FAILED."); |
|
212 |
} else { |
|
213 |
System.out.println(); |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
// Stop all threads and wait to be sure they all are dead |
|
219 |
barrier.set(ALL_THREADS - DAEMON_THREADS); |
|
220 |
for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) { |
|
221 |
live[i] = false; |
|
222 |
} |
|
223 |
// wait until daemon threads are terminated . |
|
224 |
barrier.await(); |
|
225 |
||
226 |
// give chance to threads to terminate |
|
227 |
pause(); |
|
228 |
checkThreadCount(0, ALL_THREADS - DAEMON_THREADS); |
|
229 |
||
230 |
if (testFailed) |
|
231 |
throw new RuntimeException("TEST FAILED."); |
|
232 |
||
233 |
System.out.println("Test passed."); |
|
234 |
} |
|
235 |
||
236 |
// The MyThread thread lives as long as correspondent live[i] value is true |
|
237 |
private static class MyThread extends Thread { |
|
238 |
int id; |
|
239 |
||
240 |
MyThread(int id) { |
|
241 |
this.id = id; |
|
242 |
} |
|
243 |
||
244 |
public void run() { |
|
245 |
// signal started |
|
246 |
barrier.signal(); |
|
247 |
while (live[id]) { |
|
248 |
try { |
|
249 |
sleep(100); |
|
250 |
} catch (InterruptedException e) { |
|
251 |
System.out.println("Unexpected exception is thrown."); |
|
252 |
e.printStackTrace(System.out); |
|
253 |
testFailed = true; |
|
254 |
} |
|
255 |
} |
|
256 |
// signal about to exit |
|
257 |
barrier.signal(); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
private static Object pauseObj = new Object(); |
|
262 |
private static void pause() { |
|
263 |
// Enter lock a without blocking |
|
264 |
synchronized (pauseObj) { |
|
265 |
try { |
|
266 |
// may need to tune this timeout for different platforms |
|
267 |
pauseObj.wait(50); |
|
268 |
} catch (Exception e) { |
|
269 |
System.err.println("Unexpected exception."); |
|
270 |
e.printStackTrace(System.err); |
|
271 |
} |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
} |