Quick revision
The short answer
In a livelock, threads keep reacting to each other and changing state but make no progress, for example two threads that each take one lock, fail tryLock on the second, release, and retry in lockstep forever; we fix it with randomized backoff or a fixed lock order. In starvation, one thread never gets the resource it needs, for example under a non-fair lock with heavy contention, or low-priority work stuck behind constant high-priority work; we fix it with fair policies, separate capacity, or limits on the dominating work. Unlike deadlock, in both cases the threads are alive and busy.
What to remember
In this answer, we saw that livelock and starvation, unlike deadlock, leave threads alive and busy. In a livelock, threads keep reacting to each other without progress, as when two threads repeatedly take one lock, fail tryLock on the second and back off in lockstep; randomized backoff or a fixed lock order fixes it. In starvation, one thread or task never gets the resource, as under a non-fair lock with heavy contention or behind a stream of higher-priority work; fair policies, separate capacity or limits on the dominating work fix it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In a livelock, threads keep reacting to each other and changing state but make no progress, for example two threads that each take one lock, fail tryLock on the second, release, and retry in lockstep forever; we fix it with randomized backoff or a fixed lock order. In starvation, one thread never gets the resource it needs, for example under a non-fair lock with heavy contention, or low-priority work stuck behind constant high-priority work; we fix it with fair policies, separate capacity, or limits on the dominating work. Unlike deadlock, in both cases the threads are alive and busy.
Why this question is asked
Deadlock gets most of the attention, but its two cousins are just as real and harder to spot. A deadlocked service has idle threads and a thread dump that says so; a livelocked or starving service shows busy threads, normal-looking stack traces and sometimes high CPU, while some or all of its work never completes. Interviewers ask for definitions with examples to check whether we can tell the three apart and pick the right fix. In this answer, we will recap how they differ from deadlock, build a livelock in Java and fix it, build a starvation case and fix it, and finish with how to recognise each in production.
1A Quick Recap: Three Ways to Make No Progress
Progress means that work eventually completes. In a deadlock, threads are blocked waiting for each other and do nothing at all. In a livelock, threads are not blocked: they keep running and keep responding to each other, but their responses undo each other, so nothing completes. The usual picture is two people in a corridor who keep stepping aside in the same direction. In starvation, the system as a whole makes progress, but one particular thread or kind of task never gets its turn, because others keep taking the resource first.
So the question to ask during an incident is not just whether threads are stuck, but who is making progress: nobody and idle (deadlock), nobody but busy (livelock), or everyone except some (starvation).
| Deadlock | Livelock | Starvation | |
|---|---|---|---|
| Threads | Blocked, waiting | Running, retrying | Running; some never served |
| CPU | Idle | Often high | Normal or high |
| Progress | None for those threads | None for those threads | System yes, victim no |
| Thread dump | Can report the deadlock | Looks normal; changes each time | Victim always waiting |
| Typical fix | Lock order | Random backoff, lock order | Fairness, separate capacity, limits |
2A Livelock in Java
Here two threads each need two locks. To avoid deadlock, each one takes its first lock, tries the second with tryLock, and if that fails, politely releases the first and tries again. The politeness is the problem.
Notice that neither thread is ever blocked. Each one keeps acquiring and releasing locks, and the CPU shows them as busy. Because they run in step, each always finds the other's lock taken. In practice timing differences often break the symmetry after a while, but under load the retry loop can go on for a long time and waste a lot of CPU.
3Fixing the Livelock
There are two fixes. The first is to break the symmetry with randomized backoff: after failing, wait a short random time before retrying, so the two threads fall out of step and one of them gets both locks.
The second, and often better, fix is to remove the need for backing off. If both threads acquire the locks in the same fixed order, a before b, the second thread simply waits for a while holding nothing, no cycle can form, and no retry loop is needed. Randomized backoff is the right tool when a fixed order is impossible, and the same idea, randomized retry delays called jitter, is what prevents livelock between network clients that retry together.
4Starvation in Java
Starvation happens when a resource keeps going to someone else. A common Java case is a non-fair lock under heavy contention. ReentrantLock is non-fair by default: when the lock is released, a thread that happens to be arriving right then can take it before the threads that have been waiting in the queue. That gives better throughput, but under constant contention an unlucky thread can wait a very long time.
A second common case is work priority. If a single executor serves both urgent and background work, and urgent work arrives constantly, a PriorityBlockingQueue will always pick urgent tasks first, so background tasks may never run. The same happens with a shared queue where one heavy tenant floods it and others wait behind. Thread priorities in Java are only hints to the operating system, so they are not a reliable cause or cure.
5Fixing Starvation
The fixes give the victim a guaranteed share. A fair policy, such as new ReentrantLock(true) or a fair Semaphore, serves waiting threads in roughly arrival order, at some cost in throughput, because the lock is handed to a parked thread instead of a running one. Separate capacity gives low-priority or periodic work its own thread or executor, so it never competes with the dominating work at all. And limits on the dominating work, such as a cap per tenant, a rate limit, or a bounded queue for urgent tasks, ensure there is always room left for others. Which one fits depends on whether the problem is a lock, a queue or a shared pool.
6Follow-up questions
Can a livelock fix itself?
Sometimes. Small timing differences between threads can break the symmetry and let one succeed. But that is luck, not design, and under load the pattern can persist long enough to cause an outage. Randomized backoff makes the escape deliberate.
Why is ReentrantLock not fair by default?
Because fairness is expensive. With a fair lock, every release hands the lock to the longest-waiting thread, which must first be unparked and scheduled, while a thread that is already running and could have taken the lock immediately has to wait. Non-fair locks let that running thread go first, which gives much higher throughput in most workloads.
How do I spot livelock or starvation in production?
For livelock, look for high CPU with low throughput, and repeated thread dumps that show the same threads in retry loops at different points. For starvation, measure the wait time or age of each kind of work: a cleanup job whose last run was hours ago, or a tenant whose requests wait far longer than others, points directly at it.
7Conclusion
In this answer, we saw that livelock and starvation, unlike deadlock, leave threads alive and busy. In a livelock, threads keep reacting to each other without progress, as when two threads repeatedly take one lock, fail tryLock on the second and back off in lockstep; randomized backoff or a fixed lock order fixes it. In starvation, one thread or task never gets the resource, as under a non-fair lock with heavy contention or behind a stream of higher-priority work; fair policies, separate capacity or limits on the dominating work fix it.
Open with
Both are ways of making no progress without being blocked, which is what separates them from deadlock. A livelock is when threads keep reacting to each other and changing state but nothing completes.
Then, if they want more
My Java example is two threads that each need locks a and b, in opposite orders. Each takes its first lock, tries the second with tryLock, fails because the other holds it, releases its own and retries. If they move in step, they repeat that forever: CPU busy, no work done.
The fix is either randomized backoff, sleeping a short random time before retrying so they fall out of step, or simply a fixed lock order, so the second thread waits while holding nothing and no retry loop is needed. Starvation is when the system progresses but one thread never gets its turn.
Example: ReentrantLock is non-fair by default, so under heavy contention a running thread can grab a just-released lock ahead of a thread that has been waiting, again and again. Or low-priority work in a shared priority queue behind a constant stream of urgent tasks.
The fixes are a fair policy, like new ReentrantLock(true), at some throughput cost; separate capacity, like a dedicated executor for the background work; or limits on the dominating work. In both cases a thread dump shows threads alive and busy, so I look at throughput and the age of waiting work instead.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Define livelock and starvation with a Java example of each, and how to fix them.": https://scrivio-interview-library.netlify.app/java/concurrency-problems/bfbc651130/