A deadlock occurs when two or more threads are blocked forever, each waiting for the other to release a resource. It's like two people meeting in a narrow hallway — each waits for the other to move first, and nobody goes anywhere!
Four Conditions for Deadlock: Mutual exclusion, hold and wait, no preemption, and circular wait. All four must be present for deadlock to occur.
Thread A acquires Lock 1 and needs Lock 2. Thread B acquires Lock 2 and needs Lock 1. Neither can proceed — classic circular wait deadlock!
Step 0 of 7
Click "Create Deadlock" to start...
Thread A:
acquire(Lock1) → acquire(Lock2)Thread B:
acquire(Lock2) → acquire(Lock1)Opposite order = deadlock risk!
Always acquire locks in the same order. If everyone acquires Lock1 before Lock2, no circular wait can occur.
Use tryLock with a timeout. If you can't get the lock in time, release what you have and retry.
Monitor for cycles in the wait-for graph. If detected, kill one thread to break the cycle.