author | hb |
Tue, 22 Mar 2016 01:13:06 -0700 | |
changeset 37319 | 98886757c5aa |
parent 30376 | 2ccf2cf7ea48 |
child 44423 | 306c020eb154 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2004, 2015, 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 5047639 |
|
27 |
* @summary Check that the "java-level" APIs provide a consistent view of |
|
28 |
* the thread list |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
29 |
* @modules java.management |
2 | 30 |
*/ |
31 |
import java.lang.management.ManagementFactory; |
|
32 |
import java.lang.management.ThreadMXBean; |
|
33 |
import java.util.Map; |
|
34 |
||
35 |
public class ThreadLists { |
|
36 |
public static void main(String args[]) { |
|
37 |
||
37319
98886757c5aa
8151797: java/lang/management/ThreadMXBean/ThreadLists.java : inconsistent results
hb
parents:
30376
diff
changeset
|
38 |
// Bug id : JDK-8151797 |
98886757c5aa
8151797: java/lang/management/ThreadMXBean/ThreadLists.java : inconsistent results
hb
parents:
30376
diff
changeset
|
39 |
// Use a lambda expression so that call-site cleaner thread is started |
98886757c5aa
8151797: java/lang/management/ThreadMXBean/ThreadLists.java : inconsistent results
hb
parents:
30376
diff
changeset
|
40 |
Runnable printLambda = () -> {System.out.println("Starting Test");}; |
98886757c5aa
8151797: java/lang/management/ThreadMXBean/ThreadLists.java : inconsistent results
hb
parents:
30376
diff
changeset
|
41 |
printLambda.run(); |
98886757c5aa
8151797: java/lang/management/ThreadMXBean/ThreadLists.java : inconsistent results
hb
parents:
30376
diff
changeset
|
42 |
|
2 | 43 |
// get top-level thread group |
44 |
ThreadGroup top = Thread.currentThread().getThreadGroup(); |
|
45 |
ThreadGroup parent; |
|
46 |
do { |
|
47 |
parent = top.getParent(); |
|
48 |
if (parent != null) top = parent; |
|
49 |
} while (parent != null); |
|
50 |
||
51 |
// get the thread count |
|
52 |
int activeCount = top.activeCount(); |
|
53 |
||
54 |
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); |
|
55 |
||
56 |
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); |
|
57 |
int threadCount = threadBean.getThreadCount(); |
|
58 |
long[] threadIds = threadBean.getAllThreadIds(); |
|
59 |
||
60 |
System.out.println("ThreadGroup: " + activeCount + " active thread(s)"); |
|
61 |
System.out.println("Thread: " + stackTraces.size() + " stack trace(s) returned"); |
|
62 |
System.out.println("ThreadMXBean: " + threadCount + " live threads(s)"); |
|
63 |
System.out.println("ThreadMXBean: " + threadIds.length + " thread Id(s)"); |
|
64 |
||
65 |
// check results are consistent |
|
66 |
boolean failed = false; |
|
67 |
if (activeCount != stackTraces.size()) failed = true; |
|
68 |
if (activeCount != threadCount) failed = true; |
|
69 |
if (activeCount != threadIds.length) failed = true; |
|
70 |
||
71 |
if (failed) { |
|
72 |
throw new RuntimeException("inconsistent results"); |
|
73 |
} |
|
74 |
} |
|
75 |
} |