Skip to content

Instantly share code, notes, and snippets.

@kenwdelong
Created May 31, 2017 01:01
Show Gist options
  • Save kenwdelong/02e1a4cd26ef697cde77533002badaa5 to your computer and use it in GitHub Desktop.
Save kenwdelong/02e1a4cd26ef697cde77533002badaa5 to your computer and use it in GitHub Desktop.
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