Lesson · Multithreading Fundamentals
Livelock: busy, polite and stuck
14 min readLivelock
A livelock is a deadlock with motion: every thread is RUNNABLE, every thread is working, and the program still makes no progress because each thread keeps undoing its own advance to stay out of the other's way. No tool flags it, no thread is blocked, and the CPU is the only witness. Jittered backoff is the general fix.
Busy, polite and stuck
A livelock is two threads responding to each other forever. Both are RUNNABLE, both are taking steps, and the combined state never advances, because each thread undoes just enough of its own progress to stay out of the other's way.
Nothing blocks, so nothing times out. The CPU graph is often the only visible clue: it climbs while useful output stays flat.
The three ways a concurrent program stops making progress share one symptom, and differ in who is moving:
| Failure mode | Thread state | Who progresses | What the dump shows |
|---|---|---|---|
| Deadlock | BLOCKED / WAITING | Nobody | waits pointing around a cycle; HotSpot may print Found one Java-level deadlock |
| Livelock | RUNNABLE | Nobody | every thread busy near the retry point; no deadlock is reported |
| Starvation | RUNNABLE | Everyone except one | one thread perpetually passed over while other threads come and go |
Deadlock is stuck waiting; livelock is stuck trying. Both stop the work, and only one of them burns your CPU while doing it.
The hallway shuffle
Two people meet in a hallway and each steps aside in the same direction, then again, and again. Both are being considerate, and neither gets past. That is the exact shape of a lock-free retry: take what you can, and the moment you sense a collision, undo it.
The retry needs a pause, because a spin loop would flatten a core while it waits. The first pause everyone writes is a fixed sleep, and a fixed sleep on both sides stops being a pause: it becomes a schedule. Both threads wake at the same moment, collide again, release again.
// both threads run this loop, starting on opposite locks while (true) { if (left.tryLock()) { if (right.tryLock()) { doWork(); return; } left.unlock(); // politeness: the other thread may need it } Thread.sleep(10); // the same 10 ms on every thread: a collision calendar }
Two threads · tryLock retry loop
Shared counter at 0. Both threads run the same polite loop: take your own lock, tryLock the other side, and if it is busy, release and retry after a pause.
Why it is harder to debug than a deadlock
A deadlock names itself: BLOCKED threads, a waiting-for cycle, and on HotSpot the words Found one Java-level deadlock. A livelock has no signature. Every thread is RUNNABLE, the stacks are nearly identical (a loop, a tryLock, a sleep), and no tool will tell you the program is going nowhere.
What you see instead is resource use without results: cores at 100%, latency climbing, completion counters flat. A profiler points at the retry loop, which looks like healthy work until you notice what it produces, which is nothing.
Livelocks are timing-dependent, so they often appear only under load, when the collisions land close enough together. A reproduction that fails to fail is part of the experience.
Identical backoff on every thread is a collision schedule, not a solution.
The fix: randomized backoff with jitter
Give every retry a different pause. Randomizing the backoff window, and typically widening it after each failure, breaks the shared schedule: the windows drift apart, and the first attempt that finds the resources free completes the work.
Two knobs matter: the base delay and the jitter. A random delay drawn from a fixed range is enough to break a simple collision; exponential growth with jitter is the shape that survives under load, because it also bounds how hard a losing thread keeps trying.
- Randomize the wait: ThreadLocalRandom.current().nextLong(5, 40) instead of a shared 10, so two threads rarely pick the same slot.
- Grow the window: double the range after each failure, so a loser falls back faster than it retries instead of hammering at full speed.
- Cap and decide: after N failures, block (accept a lock() wait) or fail. An unbounded retry loop is a load generator with no exit.
Jitter is the cheapest fix in concurrency: one random number converts a scheduled collision into an occasional one.
Retry schedule
A filled slot is an attempt that wakes up and tries.Lockstep: one shared delay
Both threads sleep the same 10 ms, so every attempt lands in the same slot. The loop has no exit because the schedule has no gap.
Jittered: offset windows
The offsets are drawn two slots apart to make the point visible. In code they are random, which is enough: collisions become occasional instead of guaranteed.
The fix changes the schedule, not the loop: same tryLock, same release, different wake-up times. That is why jitter is the first repair to reach for, and why identical retry configuration across threads is a smell.
Deterministic tie-breaks, when randomness is not allowed
Randomness can be an awkward guest in tests and audits. A tie-break on data both parties can see is reproducible: the lower account id goes first, or odd thread ids win, or one elected leader owns the contested resource.
The tie-break must be asymmetric. Two threads that both defer to the same symmetric rule (after you, no, after you) reproduce the livelock with extra ceremony.
Replacing the race with a queue is often better than scheduling around it: one lock, or one owner thread fed by a BlockingQueue, deletes the collision instead of randomizing it.
The same shape at database and fleet scale
Optimistic concurrency runs this race against a database. Two transactions read the same row, both write with their version check, one loses (zero rows affected or a serialization failure), and both retry. If every writer retries on the same fixed delay, the same collision repeats; jitter fixes it there too.
Distributed systems meet the shape at fleet scale as retry storms: clients and services retry a failing call on a shared schedule, multiplying load exactly when the system is weakest. The standard counter is randomized exponential backoff with a cap, which should look familiar by now.
What to remember
The sequence below is the response order that works: classify, read the schedule, break the phase, delete the race, then bound it.
- Ask who is moving: nobody blocked and nobody progressing points at a retry loop, not at a lock.
- Read the schedule before the code: equal delays, equal timeouts and identical backoff configuration are the fingerprint.
- Break the phase: jitter the pause; if the environment must stay deterministic, add an asymmetric tie-break.
- Delete the collision where you can: one lock, or a queue with a single owner, beats a smarter retry.
- Bound it: cap the retries and choose the fallback (block, degrade or fail), because full-speed retrying is not a fallback.
A livelock is a scheduling bug: fix the schedule or delete the race, and never ship an unbounded polite retry.
RUNNABLE is not progress
Monitoring that watches for BLOCKED threads reports a livelock as healthy: every thread is running, CPU is busy, nothing is queued. The counter that catches it is throughput, not state. When cores are pegged and completion is flat, suspect a retry loop before you suspect capacity.
Interview angles
What is a livelock, and how does it differ from a deadlock?+
In a livelock every thread is running and none progresses, because each keeps undoing its own work to avoid or escape a collision. In a deadlock every thread is blocked and none progresses. Same standstill, different thread states, and only the livelock burns CPU.
Why does a fixed backoff on every thread cause it?+
It stops being a pause and becomes a schedule: both threads wake at the same instant, find the same collision, release, and sleep the same amount again. The retry loop turns periodic, with the period equal to the collision period.
Common misconception: People assume that sleeping between retries is enough. Sleep without jitter keeps the threads in phase, which is how the loop stays alive.
How do you diagnose one in a running JVM?+
Thread dumps show RUNNABLE threads stuck at the same retry frame, and no deadlock is reported. Confirm with the pair of signals (CPU busy, throughput flat), profile the hot loop, and read the retry configuration: identical delays across threads are the fingerprint.
For programmatic detection, ThreadMXBean.findDeadlockedThreads() returning null while throughput is zero and every thread is RUNNABLE is a strong hint that the wait-for graph lives in your own task graph or retry schedule, not in the monitors.
What does jitter actually change?+
It de-synchronizes the retry windows, so collisions become occasional instead of scheduled: at least one thread reaches the resource while the others are still waiting. Exponential growth adds a bound on how often losers retry, so the system converges instead of oscillating.
When would you use a deterministic tie-break instead of randomness?+
When reproducibility matters, such as tests, audits or debugging. Choose the winner by data both sides can see, like a lower id or an elected leader, as long as the rule is asymmetric; two threads deferring to the same symmetric rule still livelock.
The same failure shows up as a retry storm in distributed systems. What is the counter?+
Randomized exponential backoff with a cap, plus a retry budget so clients cannot retry forever. Unbounded synchronized retries add load exactly when capacity is gone, which is how a small blip becomes an outage.