Deadlock Four Conditions in Java
Fast recall
Quick revision
Core answer
Whether the candidate knows the formal deadlock conditions — often asked in senior or strong fresher interviews
Describing deadlock only as two threads waiting for each other without naming the four Coffman conditions
Name all four Coffman conditions and explain which one is easiest to break in Java code — typically Circular Wait through lock ordering
Interview-ready guide
Interview answer
Deadlock is when two or more threads are permanently blocked, each waiting for a resource the other holds.
For deadlock to happen, you need all four Coffman conditions present at the same time. Let me name them. First, Mutual Exclusion — the resource can't be shared, like a synchronized lock that only one thread can hold.
Second, Hold and Wait — a thread holds one lock while trying to acquire another. Third, No Preemption — you can't forcibly take a lock away, it has to be released voluntarily. Fourth, Circular Wait — there's a circular chain where thread A waits for thread B's lock, and B waits for A's lock.
Here's the key insight: you only need to break one condition to prevent deadlock. In Java code, the easiest one to break is Circular Wait by using lock ordering. That means if you need to lock two accounts, always lock them in ascending ID order.
Thread one locks account ten then account twenty. Thread two also locks ten then twenty. Now there's no cycle, no deadlock.
Some freshers think synchronized alone prevents deadlock, but that's wrong — deadlock happens when you have multiple synchronized blocks acquired in inconsistent order. The interviewer is testing whether you know the formal conditions, not just the intuitive explanation.
Deadlock Four Conditions in Java
Direct Answer
Deadlock requires all four Coffman conditions: Mutual Exclusion (resources are non-shareable), Hold and Wait (holding one resource while waiting for another), No Preemption (resources cannot be forcibly taken), and Circular Wait (circular chain of waiting threads). Breaking any one condition prevents deadlock.
Common Mistakes & Pitfalls
- Only describing two threads waiting: saying "A waits for B and B waits for A" without naming the four Coffman conditions loses points in formal interviews.
- Thinking
synchronizedalone prevents deadlock: it prevents race conditions, but inconsistent lock order withsynchronizedis exactly what causes deadlock. - Not knowing which condition to break: in Java, breaking Circular Wait via lock ordering is the standard solution — many freshers do not know this.
Check your understanding
Answer these without looking back at the article.
- How do you detect deadlock in a running Java application?
- What is a thread dump and how do you generate one?
- What is livelock?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.