Java tutorial
//package com.java2s; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import com.google.common.base.Preconditions; public class Main { private static final String lineSeperator = System.getProperty("line.separator"); public static String gerThreadDump(Thread thread) { Preconditions.checkNotNull(thread); 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(); } }