Q45: What is a Thread Deadlock and how can it be prevented?
A Deadlock occurs when two or more threads are blocked forever, each waiting for a lock held by the other.
- 4 Coffman Conditions: Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait.
- Prevention Strategies:
- Lock Ordering: Always acquire locks in a strict global sequence.
- Timed Locks: Use
ReentrantLock.tryLock(timeout)to break waiting cycles.
Key Takeaways
- Deadlocks require Circular Wait cycles across locks.
- Prevent deadlocks using consistent Lock Ordering or
tryLock(timeout).
Knowledge Check
Which strategy effectively prevents thread deadlocks in complex multi-lock systems?