Skip to content

Instantly share code, notes, and snippets.

@cwilper
Created July 1, 2011 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwilper/1058407 to your computer and use it in GitHub Desktop.
Save cwilper/1058407 to your computer and use it in GitHub Desktop.
Java utility class for doing in-VM thread dumps. Was useful for tracking down the cause of FCREPO-946.
import java.util.Map;
/**
* Static methods for dumping current thread names, states, and stack
* traces, in the context of the currently executing JVM.
*/
public class ThreadDumper {
public static void dump(String name) {
dump(name, false);
}
public static void dump(String name, boolean stackTrace) {
System.out.println("[Thread Dump " + name + "]");
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry: map.entrySet()) {
Thread thread = entry.getKey();
System.out.println(" " + thread.getName() + " (" + thread.getState() + ")");
if (stackTrace) {
for (StackTraceElement e: entry.getValue()) {
System.out.println(" " + e.toString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment