Deadlock in Java
Fast recall
Quick revision
Core answer
BLOCKED, WAITING, or parked depending on the synchronization primitive, and unrelated parts of the application may continue.Understanding of deadlock as the main pitfall of synchronized programming and multi-threaded systems.
Describing deadlock vaguely as threads waiting for each other without showing the concrete lock acquisition order that causes it.
Show the classic two-lock example with bank account transfers and explain the lock ordering prevention technique—always acquire locks in a consistent global order.
Interview-ready guide
Interview answer
Deadlock happens when two or more threads are stuck waiting for each other forever.
Neither can move forward. The classic example is two threads each holding one lock and trying to get the other's lock. Thread A locks Account X and tries to lock Account Y.
At the same time, Thread B locks Account Y and tries to lock Account X. Thread A can't proceed until it gets Y, but Thread B holds Y and won't release it until it gets X. Thread B can't proceed until it gets X, but Thread A holds X.
They're stuck in a circle. Your application just freezes. The JVM doesn't detect or break deadlocks automatically—you have to prevent them in your code.
The standard way to prevent deadlock is consistent lock ordering. Always acquire locks in the same order everywhere in your code. For bank accounts, you might sort by account ID and always lock the lower ID first, then the higher ID.
That way, no matter which direction you're transferring money, you acquire locks in the same sequence. No circular wait, no deadlock. Another approach is using ReentrantLock with tryLock.
You can specify a timeout—if you don't get the lock in say one second, tryLock returns false. Then you release any locks you're holding, wait a bit, and retry. That breaks the circular wait.
Freshers often nest synchronized blocks without thinking about order. They lock object A then B in one method, and B then A in another method. That's a deadlock waiting to happen.
Always be aware of what locks you hold and what order you acquire them in.
Deadlock in Java
Direct Answer
Deadlock occurs when a set of threads cannot progress because each is waiting for an action—often a lock release—that another thread in the set cannot perform. A two-lock cycle is the classic example. The waiting state may be BLOCKED, WAITING, or parked depending on the synchronization primitive, and unrelated parts of the application may continue.
Common Mistakes & Pitfalls
- Nested synchronized blocks on different objects: Freshers write code that locks object A, then inside that block locks object B, without realizing another method locks B then A—this creates the circular wait condition.
- Ignoring lock acquisition order: Not establishing a consistent global order for acquiring locks across all code paths that touch the same resources.
- Thinking deadlock only happens with explicit locks: Deadlock can occur with any blocking resource—database row locks, file locks, even joining threads in circular dependency patterns.
- No timeout mechanism: Using plain synchronized instead of ReentrantLock with tryLock means threads wait forever—there's no way to detect or recover from deadlock automatically.
Check your understanding
Answer these without looking back at the article.
- What are the four conditions for deadlock to occur?
- How do you detect a deadlock in a running Java application?
- What is a livelock and how is it different from deadlock?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.