skip to content

What causes thread starvation in Java, and how do fairness settings and thread priorities affect it?

level: seniorimportance: should knowfreq 48%

answer

  1. Starvation = ready thread perpetually denied lock or CPU
  2. Default synchronized & ReentrantLock are UNFAIR (barging) → can starve waiters
  3. new ReentrantLock(true) = longest waiter first, lower throughput
  4. Java thread priorities are advisory/platform-dependent — never rely on them
  5. Keep critical sections short; synchronized has no fairness knob

basics

~20 s

Starvation happens when a thread never gets a resource it needs. In Java it can come from a thread holding a lock too long, unfair locks that let other threads barge ahead, or low thread priority making the scheduler skip it. Using a fair lock can prevent lock starvation.

solid answer

~50 s

Starvation is a thread being perpetually denied the CPU or a lock. In Java the common causes are: a greedy thread that holds a shared lock for long stretches; the default unfair lock policy where ReentrantLock and synchronized let a thread 'barge' and reacquire a lock just released, repeatedly skipping waiters; and thread priorities, since a flood of higher-priority threads can starve lower-priority ones (though Java priorities are advisory: HotSpot ignores them on Linux by default, and virtual threads have a fixed priority). The main tool is fairness: under contention new ReentrantLock(true) favors the longest-waiting thread, and its Javadoc promises lack of starvation — at a throughput cost from reduced barging (untimed tryLock() still barges). ReentrantReadWriteLock and the Semaphore also have fair modes. synchronized has no fairness knob. Beyond fairness, keep critical sections short, avoid relying on priorities for correctness, and prefer concurrent collections or work queues that distribute fairly.

go deeper

for a junior

Knows starvation means a thread never gets a needed resource and that it differs from deadlock.

for a middle

Names concrete causes (greedy lock holder, low priority) and knows a fair lock exists to prevent lock starvation.

for a senior

Explains barging vs fair (longest-waiter-first) ordering and its throughput trade-off, knows which Java constructs offer fair modes (ReentrantLock/ReadWriteLock/Semaphore) and that synchronized doesn't, and treats priorities as advisory (fixed for virtual threads).

for a principal

Decides fairness policy per subsystem balancing tail latency vs throughput, designs work distribution (queues/executors) to avoid starvation system-wide, and reasons about cross-platform priority behavior and SLA guarantees.

## What starvation is **Starvation** is the liveness failure where a thread is **ready to do work** but is **perpetually denied a resource it needs** — either CPU time or a lock. Unlike deadlock there is no cycle of mutual waiting, and unlike livelock the starved thread isn't reacting to others; it simply **never gets its turn**. ## The two resources a thread can be starved of ### A) Starved of a lock A **lock** lets one thread at a time into a **critical section** (code touching shared data). When several threads contend for the same lock, the rule deciding *who gets it next* matters: - **Unfair / barging (the Java default).** Both `synchronized` and `new ReentrantLock()` are **unfair**: when a lock is released, a thread that happens to be running *right now* may grab it immediately — 'barging' ahead of threads that have been **queued and waiting longer**. Barging is great for **throughput** (no need to wake a parked waiter and context-switch to it), but a steady stream of barging threads can mean a particular waiter is **never** chosen → starvation. - **Fair.** `new ReentrantLock(true)` is **fair**: under contention it favors the **longest-waiting** thread, like a queue at a counter, and its Javadoc promises lack of starvation. It costs throughput because it curbs barging and forces more context switches. Two limits: the untimed `tryLock()` ignores fairness and grabs a free lock even when others wait, and a fair lock does not make *thread scheduling* fair, so one thread may still take it several times in a row while others are not running. `ReentrantReadWriteLock(true)` and `Semaphore(permits, true)` also offer fair modes. `synchronized` has **no** fairness option — if you need fairness you must use the explicit `Lock` types. A second lock-starvation cause is a **greedy holder**: one thread keeps the lock for very long periods (or reacquires a **reentrant** lock so often it rarely lets go), so others rarely see it free. The cure is to **keep critical sections short**. ### B) Starved of the CPU Even with no locks, a **runnable** thread needs the **scheduler** to assign it a CPU core. Each Java thread has a **priority** (`Thread.setPriority`, 1–10, default 5). Higher priority *suggests* the scheduler should prefer it. On platforms that honor priorities strictly, a constant supply of high-priority work can leave low-priority threads **never scheduled** → CPU starvation. Crucially, **Java thread priorities are advisory and platform-dependent**: the JVM maps its 10 levels onto whatever the OS provides, and some platforms ignore them. HotSpot does so on Linux by default: its `ThreadPriorityPolicy` flag notes that a normal process gets no static priorities there. So you must **never rely on priorities for correctness** — treat them as a hint, not a guarantee, and never assume a high-priority thread will always preempt a low one (nor that a low one will eventually run). ### C) Virtual threads (JDK 21 and later) Priorities play no part: a virtual thread's priority is always `NORM_PRIORITY`, and `setPriority` is ignored. Starvation comes from the carriers instead. Virtual threads run on a small pool of platform threads (by default one per available processor), JEP 444 says the scheduler does not time-slice them, and the `Thread` Javadoc says they are not intended for long-running CPU-intensive work: a few CPU-bound virtual threads can keep the rest waiting for a carrier. Pinning, where a blocked virtual thread keeps its carrier, is another route to the same starvation (JEP 491 notes that pinning "can lead to starvation or even deadlock"). ## Diagnosing starvation A starved thread shows **low CPU for itself** (it isn't running) while the system overall may be busy. Thread dumps repeatedly show it `BLOCKED` on the same monitor, or `WAITING` parked on the same lock, while other threads keep taking it; or `RUNNABLE` but never progressing. This contrasts with deadlock (the same threads stuck in a stable cycle) and livelock (churning, usually at high CPU). ## How to prevent it 1. **Use a fair lock** (`new ReentrantLock(true)`, fair `ReentrantReadWriteLock`, fair `Semaphore`) when no waiter may be left behind — accept the throughput hit knowingly, and don't call the untimed `tryLock()`/`tryAcquire()` on it, which barges. 2. **Keep critical sections short** so the lock is frequently free and no holder is greedy. 3. **Don't depend on priorities** for correctness; if you must coordinate, use explicit synchronization, not scheduling hints. 4. **Distribute work fairly** with bounded queues / `ExecutorService` / concurrent collections rather than ad-hoc contention. ## How to derive the answer Ask: *which resource is denied — a lock or the CPU?* For a lock, the lever is **fairness** (longest waiter first vs barging) and **hold time**. For the CPU, the lever is the **scheduler** and **priorities**, which in Java are only advisory. Fairness trades throughput for a no-starvation guarantee — that trade-off is the heart of the topic.

  • Why is a fair lock often slower than the default unfair one?
    Fairness curbs barging: under contention it favors the longest-waiting (parked) thread, which requires waking and context-switching to it instead of letting a currently-running thread reacquire it cheaply. That extra wakeup/switch overhead lowers throughput.
  • Can you make a synchronized block fair?
    No. The synchronized monitor offers no fairness guarantee or knob. If you need fairness, switch to an explicit ReentrantLock constructed with new ReentrantLock(true) (or a fair ReadWriteLock/Semaphore).

  • Saying synchronized can be made fair — it has no fairness setting; use ReentrantLock(true)
  • Treating thread priorities as a hard guarantee of scheduling order
  • Claiming fair locks are always better — they reduce throughput vs barging
  • Confusing starvation with deadlock (no cycle here) or assuming it pins the CPU (the victim shows low CPU)

ReentrantLock fairness setting
Java 1.5
virtual threads
Java 21

Assumes JDK 27. Virtual threads (final in 21) have a fixed priority and starve through their carriers, not through priorities.

context