Java Concurrency Interview

Deadlock, livelock and diagnostics question 140 of 180 mid

What is a livelock, and how does it differ from a deadlock?

The corridor analogy: two people step aside for each other, repeatedly, in the same direction. Nobody is blocked, everybody is polite, nobody gets through.

In code it usually comes from naive deadlock avoidance: two threads both detect a conflict, both release their locks, both retry immediately, and both collide again. It shows up as high CPU with no throughput, which is the opposite signature to a deadlock, where CPU is at zero and threads are BLOCKED.

The standard fix is randomised exponential backoff, so the retries desynchronise. Ethernet's collision handling is the same idea. Message-driven systems produce a variant where two actors bounce a message back and forth forever.

livelock theory

Quiz yourself on Deadlock, livelock and diagnostics Suggest a correction