Video in progress
On prod the app "hung". How do you determine whether it is a deadlock?
I take a thread dump: jstack <pid>, jcmd <pid> Thread.print, or Actuator's /threaddump endpoint. The JVM finds monitor and ReentrantLock deadlocks on its own and writes "Found one Java-level deadlock" at the end of the dump.
📖 Answer
My order of steps:
- I look at the signs: CPU is close to zero, requests are ending in timeouts, and the number of active threads in the pool is stuck at the maximum.
- I take 2–3 dumps 5–10 seconds apart. A single dump can mislead; if several dumps show the same threads stuck in the same state, that is the real problem.
jcmd 12345 Thread.print -l > dump1.txt
The -l flag also shows "ownable synchronizers" like ReentrantLock.
- In the dump I look for a piece like this:
"payment-4":
waiting to lock <0x000000076b2c1a10> (a com.app.Account)
- locked <0x000000076b2c1b88> (a com.app.Account)
"payment-7":
waiting to lock <0x000000076b2c1b88> (a com.app.Account)
- locked <0x000000076b2c1a10> (a com.app.Account)
Two threads are waiting for each other's locks. From the stack trace I see exactly which lines are involved.
Another variant: it is not a real deadlock but "resource exhaustion". For example, threads that took every connection in the HikariCP pool are waiting for another connection. The JVM doesn't report this as a deadlock, but in the dump everything is sitting in HikariPool.getConnection.
For a programmatic check there is ThreadMXBean.findDeadlockedThreads(), which you can add to a health check.
Sign up to mark studied questions and save your progress.