Video in progress

What does a thread dump give you, and how do you take one?

A thread dump is the state and stack trace of all the threads in the JVM at that moment. When the application freezes, the CPU is at 100% or there is a deadlock, it shows what each thread is doing; you take it with jstack <pid> or jcmd <pid> Thread.print.

📖 Answer

Unlike a heap dump, a thread dump is a small text file and almost free to take.

jcmd 12345 Thread.print > threads.txt
# or
jstack 12345 > threads.txt
# On Linux: kill -3 12345 — the dump is written to the application's stdout

For every thread you see: its name, its state (RUNNABLE, BLOCKED, WAITING, TIMED_WAITING) and which line of code it is at.

"http-nio-8080-exec-7" #45 ... BLOCKED
   at com.shop.StockService.reserve(StockService.java:58)
   - waiting to lock <0x000000071a3c2e10> (a java.lang.Object)

A practical approach: take 3 dumps 5-10 seconds apart. If the same threads are at the same place each time, that place is the problem. If there is a deadlock, jstack writes "Found one Java-level deadlock" at the end by itself.

If many threads sit in WAITING at HikariCP's getConnection, the connection pool is exhausted.