Classic Livelock Example: The Hallway Problem
What you'll learn: You'll understand livelock through the classic "hallway problem" analogy where two people repeatedly step aside in perfect synchronization, preventing either from making progress.
The Hallway Analogy
Imagine two polite people approaching each other in a narrow hallway. Person A steps left to let Person B pass, but Person B simultaneously steps right (which is also left from their perspective) to be polite. Now they're still blocking each other. They both step back to their original positions and try again—but the same thing happens! They keep moving in perfect synchronization, always mirroring each other's politeness, yet neither ever passes through.
This is livelock: both threads are actively running and responding to each other, but neither makes forward progress. Unlike deadlock where threads are frozen waiting, livelock threads are "busy doing nothing useful."
Livelock vs Deadlock
In deadlock, threads are blocked and waiting indefinitely—they're stuck and won't proceed without external intervention. In livelock, threads remain runnable and keep executing instructions, but they're trapped in a repetitive pattern that prevents them from completing their tasks. They're responding to each other's actions in ways that perpetually interfere with progress.
Why It Happens
Livelock typically occurs when threads try to resolve conflicts or recover from contention using similar retry logic. If both threads detect the same condition and respond identically at the same time, they can enter a synchronized dance where they continuously undo each other's attempts to proceed.
Key Takeaway: Livelock occurs when threads remain active but cannot make progress because they keep responding to each other's actions in ways that perpetually block forward movement—like two people endlessly stepping aside in a hallway without anyone actually passing through.