What Causes a Deadlock and How to Prevent It
Reviewed by Gurusankar M. · Updated Aug 24, 2026
Short Answer
Deadlock needs four conditions (mutual exclusion, hold-and-wait, no preemption, circular wait). The practical fix is to break circular wait with a global lock-ordering convention; tryLock-with-timeout and reducing lock scope also help.
On this page
Concept Made Simple
Coffee Chat Question
“What causes a deadlock, and how do you prevent it in a real codebase?”
Remember It Faster
Mind Map Answer
- Cause
- two threads lock A,B in opposite order
- Fix #1
- consistent global lock ordering
- Fix #2
- tryLock with timeout + backoff
- Fix #3
- shrink/avoid nested locks
Learn by Doing
Hands-on Keyboard
// Always lock accounts in id order → no circular wait
Account first = a.id() < b.id() ? a : b;
Account second = a.id() < b.id() ? b : a;
synchronized (first) { synchronized (second) { transfer(a, b); } }Think Beyond the Expected
What If?
How do you confirm a deadlock in production?
Take a thread dump (jstack) — the JVM explicitly reports 'Found one Java-level deadlock' and lists the threads and the locks each holds/waits-for, showing the cycle. ThreadMXBean.findDeadlockedThreads() can detect it programmatically too.
Real World
The money-transfer 'lock both accounts' example is the textbook deadlock; the standard fix — order locks by a stable key (account id) — appears throughout real banking/ledger code.
Interviewer's Expectation
Keywords they're listening for:
- four Coffman conditions
- circular wait
- global lock ordering
- tryLock timeout
- thread-dump deadlock detection
Common Mistakes
- Acquiring multiple locks in inconsistent order
- Holding locks across external/blocking calls
- Large nested critical sections
Best Practices
- Define and enforce a global lock order
- Prefer tryLock with timeout for multi-lock code
- Keep critical sections small; avoid I/O under locks
Follow-up Questions
- How does the JVM detect deadlocks in a thread dump?
- How does tryLock break a potential deadlock?
- What's the difference between deadlock and livelock?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Deadlocks (Multithreading) Interview question: "What causes a deadlock, and how do you prevent it in a real codebase?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair