How to Generate Java Thread Dump Programmatically

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » How to Generate Java Thread Dump Programmatically

Generate Java Thread Dump Programmatically

What is Thread Dump?

The thread dump is a snapshot of exactly what’s executing at a moment in time in your Java Program.

While the thread dump format and content may vary between the different Java vendors, at the bare minimum it provides you a list of the stack traces for all Java threads in the Java Virtual Machine.

Using this information, you can either analyze the problem yourself, or work with those who wrote the running code to analyze the problem.

Thread dump is just a list of all threads and the full stack trace of code running on each thread. If you are a J2EE Application Server administrator and you’ve never done development before, the concept of a stack trace may be foreign to you.

A stack trace is a dump of the current execution stack that shows the method calls running on that thread from the bottom up.

Here is a sample Thread Dump:

   java.lang.Thread.State: RUNNABLE
        at Method)
        at com.crunchify.tutorials.CrunchifySynchronizeThread.getThreadDump(

   java.lang.Thread.State: BLOCKED

   java.lang.Thread.State: BLOCKED

"Signal Dispatcher" 
   java.lang.Thread.State: RUNNABLE

   java.lang.Thread.State: WAITING
        at java.lang.Object.wait(Native Method)
        at java.lang.ref.ReferenceQueue.remove(
        at java.lang.ref.ReferenceQueue.remove(
        at java.lang.ref.Finalizer$

"Reference Handler" 
   java.lang.Thread.State: WAITING
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(
        at java.lang.ref.Reference$

Java Method to Generate Thread Dump:

        * @author

    public static String crunchifyGenerateThreadDump() {
        final StringBuilder dump = new StringBuilder();
        final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
        for (ThreadInfo threadInfo : threadInfos) {
            dump.append("\" ");
            final Thread.State state = threadInfo.getThreadState();
            dump.append("\n   java.lang.Thread.State: ");
            final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
            for (final StackTraceElement stackTraceElement : stackTraceElements) {
                dump.append("\n        at ");
        return dump.toString();

In my previous Java Deadlock Example I’ve used the same method to generate thread dump. Here is a list of all Java Examples and Spring MVC Examples which you may be interested in.

Want to generate memory Heap Dump on OOM?

Please use -XX:+HeapDumpOnOutOfMemory parameter while starting java application.

4 thoughts on “How to Generate Java Thread Dump Programmatically”

  1. I was looking for a tool that can help me to understand the thread dumps in a graphical way and this tool fastthread has given the exact functionalities that I was looking for.


Leave a Comment