Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadLists.java
41152 views
/*1* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 504763926* @summary Check that the "java-level" APIs provide a consistent view of27* the thread list28*/29import java.lang.management.ManagementFactory;30import java.lang.management.ThreadMXBean;31import java.util.Map;3233public class ThreadLists {34public static void main(String args[]) {3536// Bug id : JDK-815179737// Use a lambda expression so that call-site cleaner thread is started38Runnable printLambda = () -> {System.out.println("Starting Test");};39printLambda.run();4041// get top-level thread group42ThreadGroup top = Thread.currentThread().getThreadGroup();43ThreadGroup parent;44do {45parent = top.getParent();46if (parent != null) top = parent;47} while (parent != null);4849// get the thread count50int activeCount = top.activeCount();5152Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();5354ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();55int threadCount = threadBean.getThreadCount();56long[] threadIds = threadBean.getAllThreadIds();5758System.out.println("ThreadGroup: " + activeCount + " active thread(s)");59System.out.println("Thread: " + stackTraces.size() + " stack trace(s) returned");60System.out.println("ThreadMXBean: " + threadCount + " live threads(s)");61System.out.println("ThreadMXBean: " + threadIds.length + " thread Id(s)");6263// check results are consistent64boolean failed = false;65if (activeCount != stackTraces.size()) failed = true;66if (activeCount != threadCount) failed = true;67if (activeCount != threadIds.length) failed = true;6869if (failed) {70throw new RuntimeException("inconsistent results");71}72}73}747576