Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are the four conditions that must all hold for a deadlock, and which one is usually easiest to break in application code?

A deadlock needs four conditions at the same time: mutual exclusion (a resource can be held by only one thread), hold and wait (a thread holds one resource while waiting for another), no preemption (a resource cannot be taken away from its holder), and circular wait (a cycle of threads, each waiting for a resource the next one holds). Breaking any one prevents deadlock, and in application code circular wait is usually the easiest to break, by always acquiring locks in one fixed order.

Why this question is asked

Deadlocks are rare, hard to reproduce and dramatic when they happen: the service does not crash, it just stops. Knowing the four conditions, often called the Coffman conditions after the 1971 paper that described them, turns deadlock from a mystery into a checklist, because each condition is a lever we can pull. Interviewers ask about them to see whether we can reason about prevention rather than just recognise a deadlock after the fact. In this answer, we will recap what a deadlock is, go through each condition with a Java example, see how each could be broken in principle, and then explain why lock ordering is the practical choice.

1A Quick Recap of Deadlock

A deadlock is a state in which two or more threads wait for each other forever. Each one holds something the other needs and none will let go, so no thread can make progress. In Java the resources are usually monitors (the locks behind synchronized), ReentrantLocks, database row locks or connections from a pool.

The classic example is two bank transfers running in opposite directions. Transfer one locks account A, then wants account B. Transfer two locks account B, then wants account A. If both take their first lock before either takes its second, they wait on each other forever.

Java
void transfer(Account from, Account to, long amount) {
synchronized (from) { // thread 1: locks A thread 2: locks B
synchronized (to) { // thread 1: waits for B thread 2: waits for A
from.withdraw(amount); // never reached
to.deposit(amount);
}
}
}
// thread 1: transfer(a, b, 100) thread 2: transfer(b, a, 50)

2Condition 1 and 2: Mutual Exclusion and Hold and Wait

Mutual exclusion means the resource cannot be shared: while one thread holds account A's monitor, no other thread can hold it. That is the whole point of a lock, so in practice we rarely break this condition. Where we can, the result is excellent: immutable objects need no lock at all, and a ReadWriteLock lets many readers share a resource. But for state that is written, exclusive access is usually what we want.

Hold and wait means a thread keeps the locks it already has while waiting for more. In the transfer, thread 1 keeps A while waiting for B. We can break this by acquiring everything at once or nothing: for example, with tryLock on both locks and releasing the first if the second is not available, or by restructuring so that a thread holds only one lock at a time. That works, but it makes code more complex and can lead to livelock if many threads keep backing off together.

3Condition 3 and 4: No Preemption and Circular Wait

No preemption means the system cannot take a lock away from the thread that holds it. Java never forcibly releases a monitor. We can emulate preemption with timeouts: a thread that waits too long for a ReentrantLock with tryLock(timeout) gives up and releases what it holds. Databases do a form of this too: when they detect a deadlock among transactions, they abort one of them, which is preemption by the database.

Circular wait means there is a cycle: thread 1 waits for thread 2, which waits for thread 1, or a longer chain that closes on itself. This is the condition that actually describes the shape of a deadlock, and it is the one we can most often remove with a simple rule.

The four conditions and how to break each
ConditionIn the transfer exampleHow to break it
Mutual exclusionOnly one thread holds an account monitorImmutable data, read-sharing (rarely possible)
Hold and waitHolds A while waiting for BTake all locks at once, or hold one at a time
No preemptionA monitor cannot be taken awaytryLock with timeout, then release and retry
Circular wait1 waits for 2, 2 waits for 1Acquire locks in one fixed global order
Removing any single row makes deadlock impossible.

4Why Circular Wait Is the Easiest to Break

If every thread acquires locks in the same global order, a cycle cannot form. Suppose we order accounts by id. A thread that holds the lock on the lower id may wait for the higher one, but no thread ever holds the higher one while waiting for the lower one. So the waiting relation only ever points one way, and a chain of waits always ends at a thread that can proceed.

Java
void transfer(Account from, Account to, long amount) {
Account first = from.id() < to.id() ? from : to; // always lock the lower id first
Account second = from.id() < to.id() ? to : from;
synchronized (first) {
synchronized (second) {
from.withdraw(amount);
to.deposit(amount);
}
}
}
// thread 1: transfer(a, b, 100) locks a then b
// thread 2: transfer(b, a, 50) also locks a then b, so it waits for thread 1 and then proceeds

Notice that the code change is small and local, it adds no timeouts or retries, and the locks keep their simple synchronized form. That is why lock ordering is the usual answer. Its weakness is that the order must be respected everywhere, including in code written by other people later, so it is worth documenting it next to the lock and keeping the set of nested locks small. When objects have no natural id, System.identityHashCode can provide an order, with a tie-breaking lock for the rare case of equal hashes.

5Applying the Conditions to Other Resources

The same four conditions apply beyond monitors. Two database transactions that update the same rows in opposite order form a cycle of row locks; updating rows in a consistent order, such as by primary key, prevents it. A request that holds one pool connection and waits for a second is hold and wait on a pool; holding only one at a time breaks it. Two services calling each other synchronously with bounded thread pools form a circular wait across processes. Reading a hang through the four conditions tells us which one is easiest to break in that situation.

6Follow-up questions

Does Java detect deadlocks automatically?

The JVM can find deadlocks among monitors and java.util.concurrent locks when asked, for example by jstack, jcmd Thread.print or ThreadMXBean.findDeadlockedThreads(). It does not resolve them; the threads stay blocked until the process is restarted. That is why prevention matters more than detection.

Is tryLock with a timeout a better fix than lock ordering?

It is a useful fallback when a fixed order is impossible, for example when the set of locks is only known at run time. But it adds retry logic, makes failures possible under contention, and can livelock if threads retry in lockstep, so it needs random backoff. Lock ordering is simpler when it is possible.

Can a single thread deadlock itself?

Not with Java's monitors or ReentrantLock, because both are reentrant: a thread that already holds the lock can acquire it again. It can with a non-reentrant lock, or by waiting on a future of a task that can only run on its own thread, such as in a single-threaded executor.

7Conclusion

In this answer, we saw the four conditions that must all hold for a deadlock: mutual exclusion, hold and wait, no preemption and circular wait. Breaking any one prevents it. Mutual exclusion is usually what we want, hold and wait and no preemption can be broken with all-or-nothing acquisition or timeouts at the cost of retries, and circular wait is usually the easiest to break, by acquiring locks in one fixed global order such as by account id. The same reasoning applies to database rows, connection pools and calls between services.

What to say in the interviewAbout 90 seconds aloud

Open with

A deadlock needs four conditions together. Mutual exclusion: a resource can only be held by one thread at a time, like a monitor. Hold and wait: a thread keeps what it has while waiting for something else.

Then, if they want more

No preemption: nobody can take a resource away from the thread holding it; Java never forces a monitor open. And circular wait: there is a cycle, thread one waits for thread two, which waits for thread one. If any one of the four is missing, a deadlock cannot happen, so each condition is a lever.

Mutual exclusion is usually the point of the lock, so we rarely remove it, except with immutable data. We can break hold and wait by taking all locks at once or only one at a time, and no preemption with tryLock and a timeout, but both bring retries and possibly livelock.

In application code the easiest one to break is circular wait: define a global order and always acquire locks in that order. For a bank transfer, I lock the account with the lower id first, whichever direction the money goes, so two opposite transfers queue up instead of waiting on each other.

It is a small, local change with no retries. The same thinking applies to database row locks and connection pools.

About this answer

Last updated
12 November 2026
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 "What are the four conditions that must all hold for a deadlock, and which one is usually...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/194d2ce07c/