A deadlock is a state where two or more threads each wait forever for a resource the others hold, so none can proceed. It requires four conditions to hold simultaneously (Coffman conditions).
A deadlock is a state where two or more threads each wait forever for a resource the others hold, so none can proceed. It requires four conditions to hold simultaneously (Coffman conditions).
Break any one and deadlock is impossible.
Thread 1 holds A, wants B ─┐
├─ circular wait → deadlock
Thread 2 holds B, wants A ─┘
# DEADLOCK: threads lock in opposite orders
def t1():
with lockA:
with lockB: ... # T1: A then B
def t2():
with lockB:
with lockA: ... # T2: B then A ← cycle!
# FIX: impose a global lock ordering (always A before B)
def safe():
with lockA:
with lockB: ... # everyone acquires in the same order
tryAcquire(timeout), back off and retry — breaks hold-and-wait.Deadlocks freeze systems silently — no crash, just hung requests. Naming the four conditions shows you can reason about why it happens and pick a targeted prevention (usually consistent lock ordering), rather than sprinkling locks and hoping.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate