Quick revision
The short answer
The JVM detects a deadlock by looking for a cycle of ownership: thread A waits for a lock owned by thread B, which waits for a lock owned by A. It checks both object monitors (synchronized) and ownable synchronizers such as ReentrantLock, and the same check is available in code through ThreadMXBean.findDeadlockedThreads(). It cannot see waits without an owner, such as a CountDownLatch never counted down or a Condition never signalled, nor resource deadlocks like thread pool starvation, connection pool exhaustion or database lock waits. The classic thread dump also omits virtual threads, for which we use jcmd <pid> Thread.dump_to_file. Lock deadlocks are fixed with a consistent lock order, tryLock with timeouts, or less shared locking.
What to remember
In this answer, we saw that the JVM reports a deadlock by following lock owners until it finds a cycle, covering both synchronized monitors and ownable synchronizers like ReentrantLock, and that ThreadMXBean.findDeadlockedThreads gives the same check in code. It cannot see waits without an owner, such as latches and conditions, or resource deadlocks like pool starvation, connection exhaustion and database lock waits. Virtual threads need Thread.dump_to_file, and lock deadlocks are fixed with a lock order, tryLock with timeouts, or less shared locking.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The JVM detects a deadlock by looking for a cycle of ownership: thread A waits for a lock owned by thread B, which waits for a lock owned by A. It checks both object monitors (synchronized) and ownable synchronizers such as ReentrantLock, and the same check is available in code through ThreadMXBean.findDeadlockedThreads(). It cannot see waits without an owner, such as a CountDownLatch never counted down or a Condition never signalled, nor resource deadlocks like thread pool starvation, connection pool exhaustion or database lock waits. The classic thread dump also omits virtual threads, for which we use jcmd <pid> Thread.dump_to_file. Lock deadlocks are fixed with a consistent lock order, tryLock with timeouts, or less shared locking.
Why this question is asked
Found one Java-level deadlock is one of the few messages where the JVM hands us the diagnosis directly. But many hung services print nothing of the kind, and engineers then conclude there is no deadlock when there is a hang of another kind. Interviewers ask this question to see whether we understand what the detector actually checks, and therefore what it cannot see. In this answer, we will reproduce a deadlock and read the report, explain how the JVM finds the cycle, use the same check from code, list the hangs it misses and why, deal with virtual threads, and fix lock deadlocks.
1Reproducing a Deadlock and Reading the Report
Two threads take two locks in opposite orders. Each gets its first lock and then waits forever for the other.
The report names each thread, the lock it waits for, and which thread holds it, followed by the stacks of the threads involved. Notice that it closes the circle: transfer waits for report, and report waits for transfer. That circle is exactly what the JVM searches for.
2How the JVM Finds the Cycle
For every thread that is blocked on a lock, the JVM knows two facts: which lock the thread is waiting for, and which thread currently owns that lock. That gives a graph in which each blocked thread points to the owner of the lock it wants. The JVM walks this graph from each blocked thread, following owner after owner. If the walk comes back to a thread it has already visited on the same path, it has found a cycle, and every thread on the cycle can never proceed, because each waits for another in the cycle.
- Thread blocked on a lock?YesFind the lock's ownerNoNot part of a deadlock
- Is the owner also blocked?YesFollow to the lock it waits forNoChain ends: no deadlock here
- Back at a thread on this path?YesCycle found: report deadlockNoKeep following owners
3 steps in order. Play them, or step through them one at a time.
This works for two kinds of locks. Object monitors, the locks behind synchronized, always have an owner. Ownable synchronizers, locks built on AbstractOwnableSynchronizer such as ReentrantLock and the write lock of ReentrantReadWriteLock, also record their owning thread, so the JVM can include them. That is why jstack -l or Thread.print -l also lists Locked ownable synchronizers for each thread.
3The Same Check From Code
The detector is also exposed through the management API, which is useful for a health check or a watchdog that logs a report automatically.
Running this periodically, for example every 30 seconds, turns a silent hang into a log entry with the exact stacks, which is much better than discovering the deadlock when customers complain.
4What the Detector Cannot See
The detector depends on owners. Many synchronizers have no owner at all. A CountDownLatch that nobody counts down, a Semaphore whose permits were never released, a Condition that is never signalled, a CompletableFuture that is never completed, or a BlockingQueue.take() on an empty queue all leave threads waiting with no thread to point at. In the dump these threads are WAITING (parking) with a stack in LockSupport.park, and there is no cycle to find.
Resource deadlocks are also invisible. In thread pool starvation, tasks in a fixed pool of 10 submit subtasks to the same pool and wait for them; all 10 threads wait, and the subtasks sit in the queue. In connection pool exhaustion, every thread holds one database connection and waits for a second. In a database lock wait, two transactions block each other inside the database, and the Java threads just show as RUNNABLE in a socket read. In all these cases the waiting is real, but the JVM does not see an owner chain.
| Hang | Reported? | How to recognise it |
|---|---|---|
| synchronized lock cycle | Yes | Found one Java-level deadlock |
ReentrantLock cycle | Yes | same report, ownable synchronizers |
CountDownLatch never counted down | No | WAITING in park, no owner |
| Condition never signalled | No | WAITING in await |
| Thread pool starvation | No | all pool threads wait on futures of the same pool |
| Connection pool exhausted | No | threads wait in the pool's getConnection |
| Database lock wait | No | RUNNABLE in socketRead; check the database |
5Virtual Threads Need a Different Dump
Since JDK 21, applications may run millions of virtual threads. The classic thread dump from jstack or Thread.print shows platform threads, and it does not list virtual threads that are not mounted on a carrier. So a virtual thread stuck forever may simply not appear. The newer dump command writes every thread, including virtual ones, in plain text or JSON.
Whether deadlock detection covers virtual threads in every case differs between releases, so check your version and inspect the full dump when a virtual thread application hangs.
6Fixing Lock Deadlocks
The standard fix is a consistent lock order: if every code path takes accounts before audit, the cycle cannot form. For locks chosen at runtime, such as two account objects, we order them by a stable key, for example the account id. When an order is hard to guarantee, ReentrantLock.tryLock with a timeout lets a thread give up, release what it holds, and retry, which turns a permanent deadlock into a short delay. The best fix is often less shared locking: immutable data, a single owner thread, or a concurrent data structure that does the coordination internally. For the hangs the detector cannot see, the fixes are timeouts on every wait (await with a timeout, get with a timeout), separate pools for dependent tasks, and limits that let a thread fail instead of waiting forever.
7Follow-up questions
Does a deadlock use CPU?
No. The threads are blocked and use no CPU, which is why a deadlocked service often shows low CPU with high latency and a full thread pool. A high-CPU hang is more likely a livelock or a busy loop.
Can the JVM recover from a deadlock by itself?
No. Monitors cannot be interrupted, and the JVM will not take a lock away. The only ways out are a restart or, for interruptible locks such as lockInterruptibly, interrupting a thread from outside.
Why does jstack sometimes say no deadlock when the service is clearly hung?
Because the hang is one of the kinds without an owner chain: a latch, a condition, a pool, or a database lock. Look at what the waiting threads are parked on and what should have released them.
8Conclusion
In this answer, we saw that the JVM reports a deadlock by following lock owners until it finds a cycle, covering both synchronized monitors and ownable synchronizers like ReentrantLock, and that ThreadMXBean.findDeadlockedThreads gives the same check in code. It cannot see waits without an owner, such as latches and conditions, or resource deadlocks like pool starvation, connection exhaustion and database lock waits. Virtual threads need Thread.dump_to_file, and lock deadlocks are fixed with a lock order, tryLock with timeouts, or less shared locking.
Open with
The JVM builds a wait-for graph. For every blocked thread it knows which lock it's waiting for and which thread owns that lock, so it follows owner to owner, and if it comes back to a thread already on the path, that's a cycle, and it prints Found one Java-level deadlock with each thread, the lock and the holder.
Then, if they want more
It covers synchronized monitors and ownable synchronizers, things built on AbstractOwnableSynchronizer like ReentrantLock and the read-write lock's write lock. The same check is in ThreadMXBean.findDeadlockedThreads, which I sometimes run from a watchdog. Because it depends on owners, it misses any wait without one: a CountDownLatch nobody counts down, a Condition never signalled, a future never completed.
Those threads just show as WAITING in park. It also misses resource deadlocks: a fixed pool whose tasks wait for subtasks queued in the same pool, every thread holding one database connection and waiting for a second, or two transactions blocking each other in the database, where the Java threads look RUNNABLE in a socket read.
And the classic thread dump doesn't show unmounted virtual threads, so for those I use jcmd Thread.dump_to_file, which writes all threads, optionally as JSON. To fix lock deadlocks, I use a consistent lock order, for example by account id, tryLock with a timeout so a thread can back off, or redesign to share fewer locks. For the other hangs, timeouts on every wait.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "jstack prints Found one Java-level deadlock with two threads. How does the JVM detect...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/7d94fb7852/