List of usage examples for java.lang.management ThreadInfo getStackTrace
public StackTraceElement[] getStackTrace()
From source file:org.apache.hadoop.hbase.GenericTestUtils.java
/** * Assert that there are no threads running whose name matches the * given regular expression.//from w w w . ja va 2 s . c o m * @param regex the regex to match against */ public static void assertNoThreadsMatching(String regex) { Pattern pattern = Pattern.compile(regex); ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 20); for (ThreadInfo info : infos) { if (info == null) continue; if (pattern.matcher(info.getThreadName()).matches()) { Assert.fail("Leaked thread: " + info + "\n" + Joiner.on("\n").join(info.getStackTrace())); } } }
From source file:org.apache.hadoop.util.TestShell.java
private static int countTimerThreads() { 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("Timer")) { count++;/*www . j a v a 2 s . c o m*/ break; } } } return count; }
From source file:Main.java
/** Builds the stack traces of all the given ThreadInfos in the buffer. Returns the stack trace of the first thread. */ private static StackTraceElement[] buildTrace(ThreadInfo[] allThreadInfo, StringBuilder buffer) { StackTraceElement[] firstStackTrace = null; for (ThreadInfo info : allThreadInfo) { buffer.append("\"" + info.getThreadName() + "\" (id=" + info.getThreadId() + ")"); buffer.append(" " + info.getThreadState() + " on " + info.getLockName() + " owned by "); buffer.append("\"" + info.getLockOwnerName() + "\" (id=" + info.getLockOwnerId() + ")"); if (info.isSuspended()) buffer.append(" (suspended)"); if (info.isInNative()) buffer.append(" (in native)"); buffer.append("\n"); StackTraceElement[] trace = info.getStackTrace(); if (firstStackTrace == null) firstStackTrace = trace;/* www. j a v a2 s . c o m*/ for (int i = 0; i < trace.length; i++) { buffer.append("\tat " + trace[i].toString() + "\n"); if (i == 0) addLockInfo(info, buffer); addMonitorInfo(info, buffer, i); } addLockedSynchronizers(info, buffer); buffer.append("\n"); } return firstStackTrace; }
From source file:EventDispatchThreadHangMonitor.java
private static void checkForDeadlock() { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long[] threadIds = threadBean.findMonitorDeadlockedThreads(); if (threadIds == null) { return;/*from w w w .j a v a2s. c om*/ } 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: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 w w w. j ava 2s .com } } return (result.toString()); }
From source file:Main.java
private static String threadInfoToString(ThreadInfo threadInfo) { StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " " + threadInfo.getThreadState()); if (threadInfo.getLockName() != null) { sb.append(" on " + threadInfo.getLockName()); }/*from w w w. java 2 s .c o m*/ if (threadInfo.getLockOwnerName() != null) { sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId()); } if (threadInfo.isSuspended()) { sb.append(" (suspended)"); } if (threadInfo.isInNative()) { sb.append(" (in native)"); } sb.append('\n'); int i = 0; for (; i < threadInfo.getStackTrace().length; i++) { StackTraceElement ste = threadInfo.getStackTrace()[i]; sb.append("\tat " + ste.toString()); sb.append('\n'); if (i == 0 && threadInfo.getLockInfo() != null) { Thread.State ts = threadInfo.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + threadInfo.getLockInfo()); sb.append('\n'); break; case WAITING: sb.append("\t- waiting on " + threadInfo.getLockInfo()); sb.append('\n'); break; case TIMED_WAITING: sb.append("\t- waiting on " + threadInfo.getLockInfo()); sb.append('\n'); break; default: } } for (MonitorInfo mi : threadInfo.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { sb.append("\t- locked " + mi); sb.append('\n'); } } } LockInfo[] locks = threadInfo.getLockedSynchronizers(); if (locks.length > 0) { sb.append("\n\tNumber of locked synchronizers = " + locks.length); sb.append('\n'); for (LockInfo li : locks) { sb.append("\t- " + li); sb.append('\n'); } } sb.append('\n'); return sb.toString(); }
From source file:Main.java
/** * Turns the given {@link ThreadInfo} into a {@link String}. */// ww w. j av a 2 s. co m public static String toString(ThreadInfo[] threads) { StringBuilder buffer = new StringBuilder(); for (ThreadInfo info : threads) { buffer.append("\"").append(info.getThreadName()).append("\" (id=").append(info.getThreadId()) .append(")"); buffer.append(" ").append(info.getThreadState()).append(" on ").append(info.getLockName()) .append(" owned by "); buffer.append("\"").append(info.getLockOwnerName()).append("\" (id=").append(info.getLockOwnerId()) .append(")"); if (info.isSuspended()) { buffer.append(" (suspended)"); } if (info.isInNative()) { buffer.append(" (is native)"); } buffer.append("\n"); StackTraceElement[] trace = info.getStackTrace(); for (int i = 0; i < trace.length; i++) { buffer.append("\tat ").append(trace[i]).append("\n"); if (i == 0) { buffer.append(getLockState(info)); } buffer.append(getLockedMonitors(info, i)); } buffer.append("\n"); } return buffer.toString(); }
From source file:Main.java
static public String toString(ThreadInfo threadInfo) { StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " " + threadInfo.getThreadState()); if (threadInfo.getLockName() != null) { sb.append(" on " + threadInfo.getLockName()); }// w w w. j a v a2 s . co m if (threadInfo.getLockOwnerName() != null) { sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId()); } if (threadInfo.isSuspended()) { sb.append(" (suspended)"); } if (threadInfo.isInNative()) { sb.append(" (in native)"); } sb.append('\n'); int i = 0; StackTraceElement[] stackTrace = threadInfo.getStackTrace(); for (StackTraceElement ste : stackTrace) { // for (; i < threadInfo.getStackTrace().length && i < 64; i++) { // StackTraceElement ste = stackTrace[i]; sb.append("\tat " + ste.toString()); sb.append('\n'); if (i == 0 && threadInfo.getLockInfo() != null) { Thread.State ts = threadInfo.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + threadInfo.getLockInfo()); sb.append('\n'); break; case WAITING: sb.append("\t- waiting on " + threadInfo.getLockInfo()); sb.append('\n'); break; case TIMED_WAITING: sb.append("\t- waiting on " + threadInfo.getLockInfo()); sb.append('\n'); break; default: } } for (MonitorInfo mi : threadInfo.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { sb.append("\t- locked " + mi); sb.append('\n'); } } } if (i < stackTrace.length) { sb.append("\t..."); sb.append('\n'); } LockInfo[] locks = threadInfo.getLockedSynchronizers(); if (locks.length > 0) { sb.append("\n\tNumber of locked synchronizers = " + locks.length); sb.append('\n'); for (LockInfo li : locks) { sb.append("\t- " + li); sb.append('\n'); } } sb.append('\n'); return sb.toString(); }
From source file:rpc.TestRPC.java
/** * Count the number of threads that have a stack frame containing the given * string//from ww w .j ava 2s . com */ 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; }
From source file:com.l2jfree.lang.L2Thread.java
public static List<String> getStats(Thread t) { List<String> list = new FastList<String>(); list.add(t.toString() + " - ID: " + t.getId()); list.add(" * State: " + t.getState()); list.add(" * Alive: " + t.isAlive()); list.add(" * Daemon: " + t.isDaemon()); list.add(" * Interrupted: " + t.isInterrupted()); for (ThreadInfo info : ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true, true)) {//w ww . j a v a 2 s . c om for (MonitorInfo monitorInfo : info.getLockedMonitors()) { list.add("=========="); list.add(" * Locked monitor: " + monitorInfo); list.add("\t[" + monitorInfo.getLockedStackDepth() + ".]: at " + monitorInfo.getLockedStackFrame()); } for (LockInfo lockInfo : info.getLockedSynchronizers()) { list.add("=========="); list.add(" * Locked synchronizer: " + lockInfo); } list.add("=========="); for (StackTraceElement trace : info.getStackTrace()) list.add("\tat " + trace); } return list; }