What is a livelock, and how is it different from a deadlock?
In a deadlock, the involved threads sit in the BLOCKED or WAITING state, consuming essentially no CPU and never changing state again. In a livelock, by contrast, the threads remain in the RUNNABLE state and consume significant CPU, but they keep changing their behavior in direct response to each other without ever actually making useful progress. A classic example is two threads that both try to be polite: Thread A backs off whenever it notices Thread B needs a shared resource, and Thread B does the same for Thread A, so both keep yielding to each other in lockstep and neither one ever proceeds. The typical fix is to introduce a random backoff duration before retrying, so the two threads' timing desynchronizes and one of them eventually gets a clear opportunity to proceed -- this is the same idea behind the exponential random backoff used by CSMA/CD collision handling in classic Ethernet networking.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response