Skip to content

Instantly share code, notes, and snippets.

@kenwdelong
Created May 31, 2017
Embed
What would you like to do?
Programmatic Thread Dump in Java
StringBuilder dump = new StringBuilder();
ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMxBean.getThreadInfo(threadMxBean.getAllThreadIds(), 150);
for(ThreadInfo info : threadInfos)
{
dump.append('"').append(info.getThreadName()).append('"').append("\n");
Thread.State state = info.getThreadState();
dump.append(state);
StackTraceElement[] stes = info.getStackTrace();
for(StackTraceElement ste : stes)
{
dump.append("\n at ").append(ste);
}
dump.append("\n\n");
}
System.out.println(dump.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment