Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadDump.java
41152 views
/*1* Copyright (c) 2005, 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* Thread Dump utility class for printing25* @author Mandy Chung26*/2728import java.lang.management.*;29import java.util.*;3031public class ThreadDump {32private static final String INDENT = " ";3334public static void printThreadInfo(ThreadInfo ti) {35StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +36(ti.isDaemon() ? " daemon" : "") +37" Id=" + ti.getThreadId() +38" in " + ti.getThreadState());39if (ti.getLockName() != null) {40sb.append(" on lock=" + ti.getLockName());41}42if (ti.isSuspended()) {43sb.append(" (suspended)");44}45if (ti.isInNative()) {46sb.append(" (running in native)");47}48System.out.println(sb.toString());49if (ti.getLockOwnerName() != null) {50System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +51" Id=" + ti.getLockOwnerId());52}53StackTraceElement[] stacktrace = ti.getStackTrace();54MonitorInfo[] monitors = ti.getLockedMonitors();55for (int i = 0; i < stacktrace.length; i++) {56StackTraceElement ste = stacktrace[i];57System.out.println(INDENT + "at " + ste.toString());5859for (MonitorInfo mi : monitors) {60if (mi.getLockedStackDepth() == i) {61System.out.println(INDENT + " - locked " + mi);62}63}64}65System.out.println();66}6768public static void printStack(StackTraceElement[] stack) {69System.out.println(INDENT + "Stack: (length = " + stack.length + ")");70for (int j = 0; j < stack.length; j++) {71System.out.println(INDENT + INDENT + stack[j]);72}73System.out.println();74}7576public static void dumpStacks() {77// Get stack traces of all Threads78Map m = Thread.getAllStackTraces();79Set s = m.entrySet();80Iterator iter = s.iterator();8182Map.Entry entry;83while (iter.hasNext()) {84entry = (Map.Entry) iter.next();85Thread t = (Thread) entry.getKey();86StackTraceElement[] stack = (StackTraceElement[]) entry.getValue();87System.out.println(t);88printStack(stack);89}90}9192public static void printLockInfo(LockInfo[] locks) {93System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);94for (LockInfo li : locks) {95System.out.println(INDENT + " - " + li);96}97}9899static ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();100public static void threadDump() {101System.out.println("Full Java thread dump");102ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);103for (ThreadInfo ti : tinfos) {104printThreadInfo(ti);105LockInfo[] syncs = ti.getLockedSynchronizers();106printLockInfo(syncs);107System.out.println();108}109}110111}112113114