List of usage examples for java.lang.management ThreadMXBean getThreadInfo
public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth);
From source file:Main.java
protected static void _printThreads(ThreadMXBean bean, long[] ids, StringBuilder sb) { ThreadInfo[] threads = bean.getThreadInfo(ids, 20); for (ThreadInfo info : threads) { if (info == null) continue; sb.append(info.getThreadName()).append(":\n"); StackTraceElement[] stack_trace = info.getStackTrace(); for (StackTraceElement el : stack_trace) { sb.append(" at ").append(el.getClassName()).append(".").append(el.getMethodName()); sb.append("(").append(el.getFileName()).append(":").append(el.getLineNumber()).append(")"); sb.append("\n"); }// www.j a va 2 s . co m sb.append("\n\n"); } }
From source file:Main.java
public static String getThreadHeadline(long threadID) { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE); return getThreadHeadline(threadInfo); }
From source file:Main.java
public static String gerThreadDump(Thread thread) { Preconditions.checkNotNull(thread);//from w ww . j av a2 s. com StringBuffer dump = new StringBuffer(200); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo threadInfo = threadMXBean.getThreadInfo(thread.getId(), 100); dump.append(lineSeperator); dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append("\" "); final Thread.State state = threadInfo.getThreadState(); dump.append(lineSeperator); dump.append(" State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append(lineSeperator); dump.append(" at "); dump.append(stackTraceElement); } dump.append(lineSeperator); return dump.toString(); }
From source file:Main.java
public static String getAllThreadDumps() { final StringBuilder dump = new StringBuilder(); final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100); dump.append(lineSeperator);/*from ww w . j a va 2s . com*/ for (ThreadInfo threadInfo : threadInfos) { dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append("\" "); final Thread.State state = threadInfo.getThreadState(); dump.append(lineSeperator); dump.append(" State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append(lineSeperator); dump.append(" at "); dump.append(stackTraceElement); } dump.append(lineSeperator); dump.append(lineSeperator); } return dump.toString(); }
From source file:Main.java
public static String getThreadStack(long id) { ThreadMXBean tmb = ManagementFactory.getThreadMXBean(); ThreadInfo f = tmb.getThreadInfo(id, 500); if (f == null) return null; return getStackTrace(f.getStackTrace()); }
From source file:Main.java
public static String[] getStackTrace(long threadID) { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE); List<String> buf = new ArrayList<String>(); if (threadInfo == null) { return buf.toArray((String[]) Array.newInstance(String.class, buf.size())); }/*ww w. j a v a2 s . c om*/ StackTraceElement[] stackTrace = threadInfo.getStackTrace(); for (int i = 0; i < stackTrace.length; i++) { StackTraceElement ste = stackTrace[i]; buf.add("\t" + ste.toString()); if (i == 0 && threadInfo.getLockInfo() != null) { Thread.State ts = threadInfo.getThreadState(); switch (ts) { case BLOCKED: buf.add("\t- blocked on " + threadInfo.getLockInfo()); break; case WAITING: buf.add("\t- waiting on " + threadInfo.getLockInfo()); break; case TIMED_WAITING: buf.add("\t- waiting on " + threadInfo.getLockInfo()); break; default: } } for (MonitorInfo mi : threadInfo.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { buf.add("\t- locked " + mi); } } } LockInfo[] locks = threadInfo.getLockedSynchronizers(); if (locks.length > 0) { buf.add("\n\tNumber of locked synchronizers = " + locks.length); for (LockInfo li : locks) { buf.add("\t- " + li); } } return buf.toArray((String[]) Array.newInstance(String.class, buf.size())); }
From source file:EventDispatchThreadHangMonitor.java
private static void checkForDeadlock() { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long[] threadIds = threadBean.findMonitorDeadlockedThreads(); if (threadIds == null) { return;/* ww w . ja va2s. c o m*/ } Log.warn("deadlock detected involving the following threads:"); ThreadInfo[] threadInfos = threadBean.getThreadInfo(threadIds, Integer.MAX_VALUE); for (ThreadInfo info : threadInfos) { Log.warn("Thread #" + info.getThreadId() + " " + info.getThreadName() + " (" + info.getThreadState() + ") waiting on " + info.getLockName() + " held by " + info.getLockOwnerName() + stackTraceToString(info.getStackTrace())); } }
From source file:backtype.storm.utils.Utils.java
public static String threadDump() { final StringBuilder dump = new StringBuilder(); final java.lang.management.ThreadMXBean threadMXBean = java.lang.management.ManagementFactory .getThreadMXBean();// www. ja v a2 s . c o m final java.lang.management.ThreadInfo[] threadInfos = threadMXBean .getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (java.lang.management.ThreadInfo threadInfo : threadInfos) { dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append("\" "); final Thread.State state = threadInfo.getThreadState(); dump.append("\n java.lang.Thread.State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append("\n at "); dump.append(stackTraceElement); } dump.append("\n\n"); } return dump.toString(); }
From source file:morphy.service.ThreadService.java
/** * Dumps stack traces of all threads to threaddump.txt. *///from ww w .j a va 2 s . com public void threadDump() { LOG.error("All threads are in use. Logging the thread stack trace to threaddump.txt and exiting."); final ThreadMXBean threads = ManagementFactory.getThreadMXBean(); long[] threadIds = threads.getAllThreadIds(); PrintWriter printWriter = null; try { printWriter = new PrintWriter(new FileWriter(THREAD_DUMP_FILE_PATH, false)); printWriter.println("Morphy ThreadService initiated dump " + new Date()); for (long threadId : threadIds) { ThreadInfo threadInfo = threads.getThreadInfo(threadId, 10); printWriter.println("Thread " + threadInfo.getThreadName() + " Block time:" + threadInfo.getBlockedTime() + " Block count:" + threadInfo.getBlockedCount() + " Lock name:" + threadInfo.getLockName() + " Waited Count:" + threadInfo.getWaitedCount() + " Waited Time:" + threadInfo.getWaitedTime() + " Is Suspended:" + threadInfo.isSuspended()); StackTraceElement[] stackTrace = threadInfo.getStackTrace(); for (StackTraceElement element : stackTrace) { printWriter.println(element); } } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (printWriter != null) { try { printWriter.flush(); printWriter.close(); } catch (Exception e2) { } } } }
From source file:org.alfresco.extension.bulkimport.util.LogUtils.java
public final static String dumpThread(final String threadName) { final StringBuilder result = new StringBuilder(); final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] threadsInfo = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (final ThreadInfo threadInfo : threadsInfo) { if (threadName == null || threadName.equals(threadInfo.getThreadName())) { result.append("\nName: "); result.append(threadInfo.getThreadName()); result.append("\nState: "); result.append(threadInfo.getThreadState()); result.append("\nStack Trace:"); for (final StackTraceElement stackTraceElement : threadInfo.getStackTrace()) { result.append("\n\t\tat "); result.append(stackTraceElement); }//from ww w . j a v a 2 s . c om } } return (result.toString()); }