Showing posts with label Concurrency. Show all posts
Showing posts with label Concurrency. Show all posts

Monday, September 7, 2026

How to Generate a Thread Dump in Java (Quick Guide)

A Thread Dump helps you track the exact activity of every running thread in your Java application at a specific point in time. It is essential for diagnosing high CPU usage, hanging tasks, and deadlocks.

How to Generate a Thread Dump

Method 1: Using the Command Line (Recommended)

You don't need keyboard shortcuts. Use the built-in JDK tool jcmd:

  1. Find the Java process ID:
    1.  jps -l
  2. 2. Print the thread dump to a file:
    1. jcmd <PID> Thread.print > threaddump.txt 

Method 2: Keyboard Shortcut (Console Window Only)

If you run the application directly inside an interactive command terminal:

  • Windows: Press Ctrl + Break (or Ctrl + Fn + B on modern laptops).

  • Linux / Mac: Press Ctrl + \ (or run kill -3 <PID>).

Simple Test Program

Run this program in your terminal, then trigger a thread dump using either method above to view the active thread stack traces:


Program :- LoopTest.java

public class LoopTest {
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            System.out.println("Running...");
            Thread.sleep(1000); // Prevents console spamming
        }
    }
}