Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
How do you troubleshoot a deadlock in a production Java application?
The first step is capturing a thread dump of the running JVM at the moment it's hung, since a deadlock leaves the involved threads permanently blocked, so the dump captures the exact stuck state.
jstack <pid> # or: kill -3 <pid> and read stdout/log jcmd <pid> Thread.print
jstack's output is especially useful here because the HotSpot JVM actively detects cycles among threads waiting on monitor locks and prints a section literally titled "Found one Java-level deadlock", listing exactly which threads are blocked waiting for which locks and which threads currently hold them, so you often don't need to manually trace it.
Tools like VisualVM, Java Mission Control, or an APM agent can capture the same information with a UI and can also take periodic thread dumps automatically, which helps if the hang is intermittent and you can't attach at the exact right moment.
Once you've identified the competing lock-acquisition order from the dump, the fix is almost always the same: restructure the code so every code path acquires the same set of locks in the same fixed order, or replace nested locking with a single lock, a higher-level concurrent collection, or a tryLock(timeout) that backs off instead of blocking forever, so the same class of deadlock can't recur.
More Related questions...