Take a thread dump with jstack or jcmd, ideally two a few seconds apart, and look at the stuck threads. The JVM detects lock cycles and prints a Java level deadlock section outright. If there is no cycle, threads are probably blocked on a resource such as a connection pool or a socket read with no timeout. Fix by ordering locks consistently, shrinking the critical section, or adding timeouts.
Why interviewers ask this
The interviewer wants to know you can debug a live JVM rather than only read code. Reaching for a thread dump immediately, and knowing that the JVM names the deadlock for you, is the credibility signal. They are also probing prevention: consistent lock ordering, tryLock with a timeout, and never making a remote call while holding a lock, which is how most real deadlocks are actually created.
How to structure your answer
- Take multiple thread dumps and compare what is stuck.
- Look for the deadlock section, then for blocked and waiting states.
- Distinguish a lock cycle from resource starvation or a missing timeout.
- Give the prevention rules you apply in code.
Example answer
First thing is a thread dump, and I take two or three a few seconds apart so I can tell what is genuinely stuck from what is merely busy. The JVM does a lot of the work here: if two threads hold each other's monitors it prints a Java level deadlock section naming both threads and both locks, and then the stack traces tell me exactly which methods to look at. If there is no cycle, the pattern is usually different: dozens of threads all in a waiting state on the same connection pool, which means something is holding connections rather than a classic deadlock, or threads blocked in a socket read with no timeout configured, which hangs indefinitely when a peer stops responding. For prevention I have a few rules. Locks are acquired in a consistent order, and I document that order when there are two. I keep the critical section as small as possible and never make a network call or publish a message while holding a lock. And where the design allows it I use tryLock with a timeout, so contention degrades into a handled error rather than a hang.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- How do you get a thread dump from a container without a shell?
- What is the difference between blocked, waiting and timed waiting in a dump?
- How would you reproduce a suspected deadlock in a test?
Related java developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions