Java Code Examples for java.lang.management.ThreadInfo#getStackTrace()
The following examples show how to use
java.lang.management.ThreadInfo#getStackTrace() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadDumpAction.java From emissary with Apache License 2.0 | 6 votes |
public ThreadDumpInfo(ThreadInfo ti) { StringBuilder sb = new StringBuilder(); if (ti == null) { sb.append("A null thread?"); } else { sb.append("\"" + ti.getThreadName() + "\" tid=" + ti.getThreadId() + "\n"); sb.append(" thread state " + ti.getThreadState()); // no new line if (ti.getLockName() != null) { sb.append(" (on " + ti.getLockName() + " owned by " + ti.getLockOwnerId() + ")\n"); } if (ti.isSuspended()) { sb.append(" SUSPENDED\n"); } if (ti.isInNative()) { sb.append(" IN NATIVE CODE\n"); } for (StackTraceElement ste : ti.getStackTrace()) { sb.append(" " + ste.toString() + "\n"); } } stack = sb.toString(); }
Example 2
Source File: ZooKeeperHighAvailabilityITCase.java From flink with Apache License 2.0 | 6 votes |
private static String generateThreadDump() { final StringBuilder dump = new StringBuilder(); final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (ThreadInfo threadInfo : threadInfos) { dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append('"'); final Thread.State state = threadInfo.getThreadState(); dump.append(System.lineSeparator()); dump.append(" java.lang.Thread.State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append(System.lineSeparator()); dump.append(" at "); dump.append(stackTraceElement); } dump.append(System.lineSeparator()); dump.append(System.lineSeparator()); } return dump.toString(); }
Example 3
Source File: Watchdog.java From Nukkit with GNU General Public License v3.0 | 6 votes |
private static void dumpThread(ThreadInfo thread, Logger logger) { logger.emergency("Current Thread: " + thread.getThreadName()); logger.emergency("\tPID: " + thread.getThreadId() + " | Suspended: " + thread.isSuspended() + " | Native: " + thread.isInNative() + " | State: " + thread.getThreadState()); // Monitors if (thread.getLockedMonitors().length != 0) { logger.emergency("\tThread is waiting on monitor(s):"); for (MonitorInfo monitor : thread.getLockedMonitors()) { logger.emergency("\t\tLocked on:" + monitor.getLockedStackFrame()); } } logger.emergency("\tStack:"); for (StackTraceElement stack : thread.getStackTrace()) { logger.emergency("\t\t" + stack); } }
Example 4
Source File: TestRPC.java From hadoop with Apache License 2.0 | 6 votes |
/** * Count the number of threads that have a stack frame containing * the given string */ private static int countThreads(String search) { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); int count = 0; ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 20); for (ThreadInfo info : infos) { if (info == null) continue; for (StackTraceElement elem : info.getStackTrace()) { if (elem.getClassName().contains(search)) { count++; break; } } } return count; }
Example 5
Source File: TimedOutTestsListener.java From big-c with Apache License 2.0 | 6 votes |
private static void printThreadInfo(ThreadInfo ti, PrintWriter out) { // print thread information printThread(ti, out); // print stack trace with locks StackTraceElement[] stacktrace = ti.getStackTrace(); MonitorInfo[] monitors = ti.getLockedMonitors(); for (int i = 0; i < stacktrace.length; i++) { StackTraceElement ste = stacktrace[i]; out.println(INDENT + "at " + ste.toString()); for (MonitorInfo mi : monitors) { if (mi.getLockedStackDepth() == i) { out.println(INDENT + " - locked " + mi); } } } out.println(); }
Example 6
Source File: WatchdogThread.java From Kettle with GNU General Public License v3.0 | 6 votes |
private static void dumpThread(ThreadInfo thread, Logger log) { log.log(Level.SEVERE, "------------------------------"); // log.log(Level.SEVERE, "Current Thread: " + thread.getThreadName()); log.log(Level.SEVERE, "\tPID: " + thread.getThreadId() + " | Suspended: " + thread.isSuspended() + " | Native: " + thread.isInNative() + " | State: " + thread.getThreadState()); if (thread.getLockedMonitors().length != 0) { log.log(Level.SEVERE, "\tThread is waiting on monitor(s):"); for (MonitorInfo monitor : thread.getLockedMonitors()) { log.log(Level.SEVERE, "\t\tLocked on:" + monitor.getLockedStackFrame()); } } log.log(Level.SEVERE, "\tStack:"); // for (StackTraceElement stack : thread.getStackTrace()) { log.log(Level.SEVERE, "\t\t" + stack); } }
Example 7
Source File: ThreadDeadlockDetector.java From metrics with Apache License 2.0 | 6 votes |
/** * Returns a set of diagnostic stack traces for any deadlocked threads. If no threads are * deadlocked, returns an empty set. * * @return stack traces for deadlocked threads or an empty set */ public Set<String> getDeadlockedThreads() { final long[] ids = threads.findDeadlockedThreads(); if (ids != null) { final Set<String> deadlocks = new HashSet<String>(); for (ThreadInfo info : threads.getThreadInfo(ids, MAX_STACK_TRACE_DEPTH)) { final StringBuilder stackTrace = new StringBuilder(); for (StackTraceElement element : info.getStackTrace()) { stackTrace.append("\t at ") .append(element.toString()) .append(String.format("%n")); } deadlocks.add( String.format("%s locked on %s (owned by %s):%n%s", info.getThreadName(), info.getLockName(), info.getLockOwnerName(), stackTrace.toString() ) ); } return Collections.unmodifiableSet(deadlocks); } return Collections.emptySet(); }
Example 8
Source File: ThreadMonitor.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private void printThreadInfo(ThreadInfo ti) { // print thread information printThread(ti); // print stack trace with locks StackTraceElement[] stacktrace = ti.getStackTrace(); MonitorInfo[] monitors = ti.getLockedMonitors(); for (int i = 0; i < stacktrace.length; i++) { StackTraceElement ste = stacktrace[i]; System.out.println(INDENT + "at " + ste.toString()); for (MonitorInfo mi : monitors) { if (mi.getLockedStackDepth() == i) { System.out.println(INDENT + " - locked " + mi); } } } System.out.println(); }
Example 9
Source File: ThreadDump.java From cache2k with Apache License 2.0 | 6 votes |
public static String generateThredDump() { final StringBuilder sb = new StringBuilder(); final ThreadMXBean _threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] _infos = _threadMXBean.getThreadInfo(_threadMXBean.getAllThreadIds(), Integer.MAX_VALUE); for (ThreadInfo _info : _infos) { sb.append("Thread \""); sb.append(_info.getThreadName()); sb.append("\" "); final Thread.State _state = _info.getThreadState(); sb.append(_state); final StackTraceElement[] stackTraceElements = _info.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n at "); sb.append(stackTraceElement); } sb.append("\n\n"); } return sb.toString(); }
Example 10
Source File: TimedOutTestsListener.java From hadoop with Apache License 2.0 | 6 votes |
private static void printThreadInfo(ThreadInfo ti, PrintWriter out) { // print thread information printThread(ti, out); // print stack trace with locks StackTraceElement[] stacktrace = ti.getStackTrace(); MonitorInfo[] monitors = ti.getLockedMonitors(); for (int i = 0; i < stacktrace.length; i++) { StackTraceElement ste = stacktrace[i]; out.println(INDENT + "at " + ste.toString()); for (MonitorInfo mi : monitors) { if (mi.getLockedStackDepth() == i) { out.println(INDENT + " - locked " + mi); } } } out.println(); }
Example 11
Source File: LocalThread.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
private String generateThreadStack(ThreadInfo info) { //This is annoying, but the to string method on info sucks. StringBuilder result = new StringBuilder(); result.append(info.getThreadName()).append(" ID=") .append(info.getThreadId()).append(" state=") .append(info.getThreadState()); if(CFactory.getLockInfo(info) != null) { result.append("\n\twaiting to lock <" + CFactory.getLockInfo(info) + ">"); } for(StackTraceElement element : info.getStackTrace()) { result.append("\n\tat " + element); for(LI monitor: CFactory.getLockedMonitors(info)) { if(element.equals(monitor.getLockedStackFrame())) { result.append("\n\tlocked <" + monitor + ">"); } } } if(CFactory.getLockedSynchronizers(info).length > 0) { result.append("\nLocked synchronizers:"); for(LI sync : CFactory.getLockedSynchronizers(info)) { result.append("\n" + sync.getClassName() + "@" + sync.getIdentityHashCode()); } } return result.toString(); }
Example 12
Source File: ReflectionUtils.java From big-c with Apache License 2.0 | 5 votes |
/** * Print all of the thread's information and stack traces. * * @param stream the stream to * @param title a string title for the stack trace */ public synchronized static void printThreadInfo(PrintStream stream, String title) { final int STACK_DEPTH = 20; boolean contention = threadBean.isThreadContentionMonitoringEnabled(); long[] threadIds = threadBean.getAllThreadIds(); stream.println("Process Thread Dump: " + title); stream.println(threadIds.length + " active threads"); for (long tid: threadIds) { ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH); if (info == null) { stream.println(" Inactive"); continue; } stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":"); Thread.State state = info.getThreadState(); stream.println(" State: " + state); stream.println(" Blocked count: " + info.getBlockedCount()); stream.println(" Waited count: " + info.getWaitedCount()); if (contention) { stream.println(" Blocked time: " + info.getBlockedTime()); stream.println(" Waited time: " + info.getWaitedTime()); } if (state == Thread.State.WAITING) { stream.println(" Waiting on " + info.getLockName()); } else if (state == Thread.State.BLOCKED) { stream.println(" Blocked on " + info.getLockName()); stream.println(" Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName())); } stream.println(" Stack:"); for (StackTraceElement frame: info.getStackTrace()) { stream.println(" " + frame.toString()); } } stream.flush(); }
Example 13
Source File: ThreadDumpPro.java From baratine with GNU General Public License v2.0 | 4 votes |
protected void buildThread(StringBuilder sb, ThreadInfo info) { sb.append("\n\""); sb.append(info.getThreadName()); sb.append("\" id=" + info.getThreadId()); sb.append(" " + info.getThreadState()); if (info.isInNative()) sb.append(" (in native)"); if (info.isSuspended()) sb.append(" (suspended)"); String lockName = info.getLockName(); if (lockName != null) { sb.append("\n waiting on "); sb.append(lockName); if (info.getLockOwnerName() != null) { sb.append("\n owned by \""); sb.append(info.getLockOwnerName()); sb.append("\""); } } sb.append("\n"); NetworkSystemBartender networkSystem = NetworkSystemBartender.current(); if (networkSystem != null) { ConnectionTcp conn = networkSystem.findConnectionByThreadId(info.getThreadId()); if (conn != null && conn.connProtocol() instanceof RequestHttpBase) { RequestHttpBase req = (RequestHttpBase) conn.connProtocol(); if (true) throw new UnsupportedOperationException(); /* XXX: if (req.getRequestURI() != null) { sb.append(" ").append(req.getRequestURI()).append("\n"); } */ } } StackTraceElement []stackList = info.getStackTrace(); if (stackList == null) return; for (StackTraceElement stack : stackList) { sb.append(" at "); sb.append(stack.getClassName()); sb.append("."); sb.append(stack.getMethodName()); if (stack.getFileName() != null) { sb.append(" ("); sb.append(stack.getFileName()); if (stack.getLineNumber() > 0) { sb.append(":"); sb.append(stack.getLineNumber()); } sb.append(")"); } if (stack.isNativeMethod()) sb.append(" (native)"); sb.append("\n"); } }
Example 14
Source File: JmxSupport.java From visualvm with GNU General Public License v2.0 | 4 votes |
private void print16Thread(final StringBuilder sb, final ThreadMXBean threadMXBean, final ThreadInfo thread) { MonitorInfo[] monitors = null; if (threadMXBean.isObjectMonitorUsageSupported()) { monitors = thread.getLockedMonitors(); } sb.append("\n\"" + thread.getThreadName() + // NOI18N "\" - Thread t@" + thread.getThreadId() + "\n"); // NOI18N sb.append(" java.lang.Thread.State: " + thread.getThreadState()); // NOI18N sb.append("\n"); // NOI18N int index = 0; for (StackTraceElement st : thread.getStackTrace()) { LockInfo lock = thread.getLockInfo(); String lockOwner = thread.getLockOwnerName(); sb.append("\tat " + st.toString() + "\n"); // NOI18N if (index == 0) { if ("java.lang.Object".equals(st.getClassName()) && // NOI18N "wait".equals(st.getMethodName())) { // NOI18N if (lock != null) { sb.append("\t- waiting on "); // NOI18N printLock(sb,lock); sb.append("\n"); // NOI18N } } else if (lock != null) { if (lockOwner == null) { sb.append("\t- parking to wait for "); // NOI18N printLock(sb,lock); sb.append("\n"); // NOI18N } else { sb.append("\t- waiting to lock "); // NOI18N printLock(sb,lock); sb.append(" owned by \""+lockOwner+"\" t@"+thread.getLockOwnerId()+"\n"); // NOI18N } } } printMonitors(sb, monitors, index); index++; } StringBuilder jnisb = new StringBuilder(); printMonitors(jnisb, monitors, -1); if (jnisb.length() > 0) { sb.append(" JNI locked monitors:\n"); sb.append(jnisb); } if (threadMXBean.isSynchronizerUsageSupported()) { sb.append("\n Locked ownable synchronizers:"); // NOI18N LockInfo[] synchronizers = thread.getLockedSynchronizers(); if (synchronizers == null || synchronizers.length == 0) { sb.append("\n\t- None\n"); // NOI18N } else { for (LockInfo li : synchronizers) { sb.append("\n\t- locked "); // NOI18N printLock(sb,li); sb.append("\n"); // NOI18N } } } }
Example 15
Source File: SampledCPUSnapshot.java From visualvm with GNU General Public License v2.0 | 4 votes |
private void print16Thread(final StringBuilder sb, final ThreadInfo thread, boolean goToSourceAvailable) { MonitorInfo[] monitors = thread.getLockedMonitors(); sb.append(" <b>"); // NOI18N sb.append("\"").append(thread.getThreadName()).append("\" - Thread t@").append(thread.getThreadId()).append("<br>"); // NOI18N sb.append(" java.lang.Thread.State: ").append(thread.getThreadState()); // NOI18N sb.append("</b><br>"); // NOI18N int index = 0; for (StackTraceElement st : thread.getStackTrace()) { LockInfo lock = thread.getLockInfo(); String stackElementText = htmlize(st.toString()); String lockOwner = thread.getLockOwnerName(); String className = st.getClassName(); String method = st.getMethodName(); int lineNo = st.getLineNumber(); String stackEl = stackElementText; if (goToSourceAvailable) { String stackUrl = OPEN_THREADS_URL+className+"|"+method+"|"+lineNo; // NOI18N stackEl = "<a href=\""+stackUrl+"\">"+stackElementText+"</a>"; // NOI18N } sb.append("\tat ").append(stackEl).append("<br>"); // NOI18N if (index == 0) { if ("java.lang.Object".equals(st.getClassName()) && // NOI18N "wait".equals(st.getMethodName())) { // NOI18N if (lock != null) { sb.append("\t- waiting on "); // NOI18N printLock(sb,lock); sb.append("<br>"); // NOI18N } } else if (lock != null) { if (lockOwner == null) { sb.append("\t- parking to wait for "); // NOI18N printLock(sb,lock); sb.append("<br>"); // NOI18N } else { sb.append("\t- waiting to lock "); // NOI18N printLock(sb,lock); sb.append(" owned by \"").append(lockOwner).append("\" t@").append(thread.getLockOwnerId()).append("<br>"); // NOI18N } } } printMonitors(sb, monitors, index); index++; } StringBuilder jnisb = new StringBuilder(); printMonitors(jnisb, monitors, -1); if (jnisb.length() > 0) { sb.append(" JNI locked monitors:<br>"); sb.append(jnisb); } LockInfo[] synchronizers = thread.getLockedSynchronizers(); if (synchronizers != null) { sb.append("<br> Locked ownable synchronizers:"); // NOI18N if (synchronizers.length == 0) { sb.append("<br>\t- None\n"); // NOI18N } else { for (LockInfo li : synchronizers) { sb.append("<br>\t- locked "); // NOI18N printLock(sb,li); sb.append("<br>"); // NOI18N } } } sb.append("<br>"); }
Example 16
Source File: OSProcess.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
private static void formatThreadInfo(ThreadInfo t, PrintWriter pw) { // this is largely copied from the JDK's ThreadInfo.java, but it limits the // stacks to 8 elements pw.append("\"" + t.getThreadName() + "\"" + " tid=0x" + Long.toHexString(t.getThreadId())); // this is in the stack trace elements so we don't need to add it // if (t.getLockName() != null) { // pw.append(" "); // pw.append(StringUtils.toLowerCase(t.getThreadState().toString())); // pw.append(" on " + t.getLockName()); // } // priority is not known // daemon status is not known if (t.isSuspended()) { pw.append(" (suspended)"); } if (t.isInNative()) { pw.append(" (in native)"); } if (t.getLockOwnerName() != null) { pw.append(" owned by \"" + t.getLockOwnerName() + "\" tid=0x" + Long.toHexString(t.getLockOwnerId())); } pw.append('\n'); pw.append(" java.lang.Thread.State: " + t.getThreadState() + "\n"); int i = 0; StackTraceElement[] stackTrace = t.getStackTrace(); for (; i < stackTrace.length && i < MAX_STACK_FRAMES; i++) { StackTraceElement ste = stackTrace[i]; pw.append("\tat " + ste.toString()); pw.append('\n'); if (i == 0 && t.getLockInfo() != null) { Thread.State ts = t.getThreadState(); switch (ts) { case BLOCKED: pw.append("\t- blocked on " + t.getLockInfo()); pw.append('\n'); break; case WAITING: pw.append("\t- waiting on " + t.getLockInfo()); pw.append('\n'); break; case TIMED_WAITING: pw.append("\t- waiting on " + t.getLockInfo()); pw.append('\n'); break; default: } } for (MonitorInfo mi : t.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { pw.append("\t- locked " + mi); pw.append('\n'); } } } if (i < stackTrace.length) { pw.append("\t..."); pw.append('\n'); } LockInfo[] locks = t.getLockedSynchronizers(); if (locks.length > 0) { pw.append("\n\tNumber of locked synchronizers = " + locks.length); pw.append('\n'); for (LockInfo li : locks) { pw.append("\t- " + li); pw.append('\n'); } } pw.append('\n'); }
Example 17
Source File: AbstractConnectionManager.java From reladomo with Apache License 2.0 | 4 votes |
public static String getApplicationName() { if (APPLICATION_NAME == null) { String name = System.getProperty(APPLICATION_NAME_KEY); if (name == null) { name = "JDBC"; ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds()); long mainThreadNumber = -1; for(int i=0;i<threadInfos.length;i++) { if (threadInfos[i] != null && threadInfos[i].getThreadName().equals("main")) { mainThreadNumber = threadInfos[i].getThreadId(); break; } } if (mainThreadNumber >= 0) { ThreadInfo threadInfo = threadMXBean.getThreadInfo(mainThreadNumber, Integer.MAX_VALUE); StackTraceElement[] stackTrace = threadInfo.getStackTrace(); if (stackTrace.length > 0) { String cname = stackTrace[stackTrace.length - 1].getClassName(); name = cname.substring(Math.max(0, cname.lastIndexOf(".") + 1), cname.length()); } } if (name.equals("ApplicationRunner")) { name = System.getProperty("applicationrunner.properties"); } } if (name.length() > 30) { name = name.substring(0, 30); } APPLICATION_NAME = name; } return APPLICATION_NAME; }
Example 18
Source File: DiagnosticUtility.java From terracotta-platform with Apache License 2.0 | 4 votes |
/** * * This method was almost copied / pasted from * https://github.com/Terracotta-OSS/terracotta-core/blob/master/common/src/main/java/com/tc/util/runtime/ThreadDumpUtil.java * to keep formatting the same as in Terracotta core, without adding a dependency on it. * * * @return a thread dump */ public String getThreadDump() { final StringBuilder sb = new StringBuilder(100 * 1024); sb.append(new Date().toString()); sb.append('\n'); sb.append("Full thread dump "); sb.append(System.getProperty("java.vm.name")); sb.append(" ("); sb.append(System.getProperty("java.vm.version")); sb.append(' '); sb.append(System.getProperty("java.vm.info")); sb.append("):\n\n"); try { final ThreadInfo[] threadsInfo = threadMXBean.dumpAllThreads(threadMXBean.isObjectMonitorUsageSupported(), threadMXBean.isSynchronizerUsageSupported()); for (final ThreadInfo threadInfo : threadsInfo) { threadHeader(sb, threadInfo); final StackTraceElement[] stea = threadInfo.getStackTrace(); final MonitorInfo[] monitorInfos = threadInfo.getLockedMonitors(); for (StackTraceElement element : stea) { sb.append("\tat "); sb.append(element.toString()); sb.append('\n'); for (final MonitorInfo monitorInfo : monitorInfos) { final StackTraceElement lockedFrame = monitorInfo.getLockedStackFrame(); if (lockedFrame != null && lockedFrame.equals(element)) { sb.append("\t- locked <0x"); sb.append(Integer.toHexString(monitorInfo.getIdentityHashCode())); sb.append("> (a "); sb.append(monitorInfo.getClassName()); sb.append(")"); sb.append('\n'); } } } if (!threadMXBean.isObjectMonitorUsageSupported() && threadMXBean.isSynchronizerUsageSupported()) { sb.append(threadLockedSynchronizers(threadInfo)); } sb.append('\n'); } } catch (final Exception e) { LOGGER.error("Cannot take thread dumps - " + e.getMessage(), e); sb.append(e.toString()); } return sb.toString(); }
Example 19
Source File: CPUSampler.java From jvmtop with GNU General Public License v2.0 | 4 votes |
public void update() throws Exception { boolean samplesAcquired = false; for (ThreadInfo ti : threadMxBean_.dumpAllThreads(false, false)) { long cpuTime = threadMxBean_.getThreadCpuTime(ti.getThreadId()); Long tCPUTime = threadCPUTime.get(ti.getThreadId()); if (tCPUTime == null) { tCPUTime = 0L; } else { Long deltaCpuTime = (cpuTime - tCPUTime); if (ti.getStackTrace().length > 0 && ti.getThreadState() == State.RUNNABLE ) { for (StackTraceElement stElement : ti.getStackTrace()) { if (isReallySleeping(stElement)) { break; } if (isFiltered(stElement)) { continue; } String key = stElement.getClassName() + "." + stElement.getMethodName(); data_.putIfAbsent(key, new MethodStats(stElement.getClassName(), stElement.getMethodName())); data_.get(key).getHits().addAndGet(deltaCpuTime); totalThreadCPUTime_.addAndGet(deltaCpuTime); samplesAcquired = true; break; } } } threadCPUTime.put(ti.getThreadId(), cpuTime); } if (samplesAcquired) { updateCount_.incrementAndGet(); } }
Example 20
Source File: AEThreadMonitor.java From BiglyBT with GNU General Public License v2.0 | 4 votes |
private void dumpThreads( IndentWriter writer ) { final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long[] allThreadIds = threadBean.getAllThreadIds(); writer.println("Threads " + allThreadIds.length); writer.indent(); List<ThreadInfo> threadInfos = new ArrayList<>(allThreadIds.length); for (int i = 0; i < allThreadIds.length; i++) { ThreadInfo info = threadBean.getThreadInfo(allThreadIds[i], 32); if(info != null) threadInfos.add(info); } Collections.sort(threadInfos, new Comparator<ThreadInfo>() { @Override public int compare(ThreadInfo o1, ThreadInfo o2) { long diff = threadBean.getThreadCpuTime(o2.getThreadId()) - threadBean.getThreadCpuTime(o1.getThreadId()); if (diff == 0) { return o1.getThreadName().compareToIgnoreCase(o2.getThreadName()); } return diff > 0 ? 1 : -1; } }); for (int i = 0; i < threadInfos.size(); i++) { try { ThreadInfo threadInfo = threadInfos.get(i); long lCpuTime = threadBean.getThreadCpuTime(threadInfo.getThreadId()); if (lCpuTime == 0) break; String sState; switch (threadInfo.getThreadState()) { case BLOCKED: sState = "Blocked"; break; case RUNNABLE: sState = "Runnable"; break; case NEW: sState = "New"; break; case TERMINATED: sState = "Terminated"; break; case TIMED_WAITING: sState = "Timed Waiting"; break; case WAITING: sState = "Waiting"; break; default: sState = "" + threadInfo.getThreadState(); break; } String sName = threadInfo.getThreadName(); String sLockName = threadInfo.getLockName(); writer.println(sName + ": " + sState + ", " + (lCpuTime / 1000000) + "ms CPU, " + "B/W: " + threadInfo.getBlockedCount() + "/" + threadInfo.getWaitedCount() + (sLockName == null ? "" : "; Locked by " + sLockName + "/" + threadInfo.getLockOwnerName())); writer.indent(); try { StackTraceElement[] stackTrace = threadInfo.getStackTrace(); for (int j = 0; j < stackTrace.length; j++) { writer.println(stackTrace[j].toString()); } } finally { writer.exdent(); } } catch (Exception e) { // TODO: handle exception } } writer.exdent(); }