Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/ThreadLists.java
38821 views
/*1* Copyright (c) 2004, 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// get top-level thread group37ThreadGroup top = Thread.currentThread().getThreadGroup();38ThreadGroup parent;39do {40parent = top.getParent();41if (parent != null) top = parent;42} while (parent != null);4344// get the thread count45int activeCount = top.activeCount();4647Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();4849ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();50int threadCount = threadBean.getThreadCount();51long[] threadIds = threadBean.getAllThreadIds();5253System.out.println("ThreadGroup: " + activeCount + " active thread(s)");54System.out.println("Thread: " + stackTraces.size() + " stack trace(s) returned");55System.out.println("ThreadMXBean: " + threadCount + " live threads(s)");56System.out.println("ThreadMXBean: " + threadIds.length + " thread Id(s)");5758// check results are consistent59boolean failed = false;60if (activeCount != stackTraces.size()) failed = true;61if (activeCount != threadCount) failed = true;62if (activeCount != threadIds.length) failed = true;6364if (failed) {65throw new RuntimeException("inconsistent results");66}67}68}697071