skip to content

What does the fairness flag on a Semaphore control, and what is the tradeoff?

level: seniorimportance: should knowfreq 45%

answer

  1. Fairness = which waiter gets the next released permit
  2. Fair = FIFO, no starvation, lower throughput
  3. Non-fair (default) = barging allowed, higher throughput, can starve
  4. Barging avoids a wake-up/handoff context switch
  5. Untimed tryAcquire() barges even on a fair semaphore

basics

~20 s

The fairness flag sets which waiting thread gets the next permit. Fair (true) serves acquire() callers first-come-first-served (FIFO), so later callers cannot overtake a waiter. Non-fair (the default) lets a newcomer take a free permit first: usually more throughput, but waiters can starve.

solid answer

~50 s

`new Semaphore(n, fair)` takes a boolean. With `fair=true`, threads calling `acquire()` get permits in FIFO order, so a later `acquire()` caller cannot overtake a waiting one and latecomers cannot starve it. With `fair=false` (the default), the semaphore allows **barging**: a thread arriving just as a permit is released can take it before threads that have been queued longer. Barging skips the wake-up and handoff to a parked thread, and the AQS documentation says throughput is generally highest for this default barging strategy. The cost is potential starvation: an unlucky thread may wait far longer than others, hurting tail latency. One wrinkle: the untimed `tryAcquire()` barges regardless of the fairness setting, so it can still jump a fair queue. The Javadoc's general advice is fair for semaphores that control access to a resource, and non-fair for other kinds of synchronization control, where its throughput advantages often outweigh fairness.

go deeper

for a junior

Knows there is a fairness flag and that fair means first-come-first-served.

for a middle

Explains barging in non-fair mode and that fairness prevents starvation at a throughput cost.

for a senior

Articulates the throughput-vs-starvation tradeoff, why non-fair is the default, and the tryAcquire barging exception; picks fairness from latency/SLA requirements.

for a principal

Reasons about fairness within the AQS framework and at system scale — tail-latency, tenant fairness, and benchmarking the throughput cost under realistic contention.

## What "fairness" means here When several threads are **blocked** in `acquire()` waiting for a permit, and a permit becomes available (someone calls `release()`), *which* waiting thread gets it? That ordering policy is what the **fairness flag** controls. ```java new Semaphore(5); // non-fair (default) new Semaphore(5, true); // fair ``` ## Fair (true): FIFO, no overtaking Waiting threads sit in a first-in-first-out queue (the `AbstractQueuedSynchronizer` wait queue that `Semaphore` is built on). A fair semaphore grants the next permit to the **longest-waiting** thread. Critically, a brand-new thread calling `acquire()` will **not** snatch a permit if others are already queued — it goes to the back of the line. So no waiter can be **starved** by threads that call `acquire()` after it. Besides the `tryAcquire()` exception below, the Javadoc and the mechanism put two limits on that promise: - FIFO is decided at an internal ordering point inside the method, so of two threads that call `acquire()` at almost the same moment, the first caller can still be served second. - Fairness fixes your place in line, not your wall-clock wait: how long you wait still depends on how long the threads ahead of you hold their permits. ## Non-fair (false, the default): barging allowed A non-fair semaphore permits **barging**. When a permit is released, a thread that happens to call `acquire()` at that very moment can grab the permit **immediately**, even though other threads have been waiting longer. The newcomer "jumps the queue." Why is this the default? **Throughput.** Handing a permit to a queued thread requires waking it up — a context switch and scheduling delay. If a running thread can take the permit right now without that handoff, the CPU does useful work sooner. The `AbstractQueuedSynchronizer` documentation states the direction: throughput and scalability are generally highest for this default barging (also called *convoy-avoidance*) strategy, which is not guaranteed to be fair or starvation-free. ## The tradeoff in one line - **Fair** → FIFO order, no starvation by latecomers, generally lower throughput. - **Non-fair** → generally higher throughput, but a thread can **starve** (wait indefinitely / suffer terrible tail latency) if it keeps getting barged past. **Starvation** = a thread is perpetually denied progress because others keep being chosen ahead of it. Fairness is the standard cure. ## A subtle exception: tryAcquire() ignores fairness The untimed, no-argument `tryAcquire()` **barges** whenever a permit is free, *even on a fair semaphore*. It checks for a free permit and takes it without consulting the wait queue. So if you rely on fairness for ordering, be aware that `tryAcquire()` callers can still cut the line. The **timed** `tryAcquire(timeout, unit)`, by contrast, queues and honours fairness. ## How to choose The `Semaphore` Javadoc gives the rule of thumb: semaphores used to control resource access should generally be fair, so that no thread is starved out, while for other kinds of synchronization control the throughput advantages of non-fair ordering often outweigh fairness. | Situation | Pick | |---|---| | Semaphore guarding a pool or other resource | fair (the Javadoc's general advice) | | Fairness across many clients/tenants | fair | | Maximise throughput, occasional long waits OK | non-fair (default) | | Low/no contention (rarely any waiters) | non-fair — fairness barely matters | When contention is low, the choice is almost irrelevant (there's rarely a queue to overtake). The decision matters under **sustained contention**, where barging's throughput win and starvation risk both become real. ## Cost of fairness Fairness isn't free at the mechanism level either: a fair acquire must check the queue and often park even when a permit is technically available, adding overhead. The `ReentrantLock` Javadoc states the size of that cost for locks: programs using fair locks accessed by many threads "may display lower overall throughput (i.e., are slower; often much slower)" than with the default. `Semaphore`, `ReentrantLock` and `ReentrantReadWriteLock` all default to non-fair.

  • Why is non-fair the default for Semaphore and ReentrantLock?
    Throughput. Barging lets a running thread take a freed permit without the wake-up and context switch needed to hand off to a parked, queued thread, and the AQS documentation says throughput is generally highest for this default barging strategy. The cost is possible starvation, which is why the Semaphore Javadoc advises fair mode for semaphores that control access to a resource.
  • Does setting fairness=true guarantee no thread ever waits long?
    It guarantees FIFO ordering so no thread is overtaken by latecomers (no starvation). But the absolute wait still depends on how long permit-holders run; fairness bounds your position in line, not the wall-clock time, and the untimed tryAcquire() can still barge past you.

  • Thinking fairness controls performance only, not ordering/starvation
  • Assuming fair semaphores are always better — they cost throughput
  • Believing fairness prevents tryAcquire() from barging (the untimed form still does)
  • Worrying about fairness when there's essentially no contention

context