MediumπŸ‘€ 3-5 yearsπŸ‘€ 8-15 years 1 min read

What causes a deadlock, and how do you prevent it in a real codebase?

Asked inAmazonMicrosoftDeloitteGoldman-style banking
#deadlock#lock ordering#trylock#concurrency#production
Report issue

⚑ 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.

β˜•Coffee Chat Question

Concept Made Simple

β€œWhat causes a deadlock, and how do you prevent it in a real codebase?”

🧠Mind Map Answer

Remember It Faster

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

⌨️Hands-on Keyboard

Learn by Doing

java
// 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); } }

πŸ”₯What If?

Think Beyond the Expected

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

  • 1How does the JVM detect deadlocks in a thread dump?
  • 2How does tryLock break a potential deadlock?
  • 3What's the difference between deadlock and livelock?

🧩Related Technologies

jstackThreadMXBeanReentrantLock

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions