NOTE

Nonfair Semaphore

Default semaphore barging behavior, shared AQS permit state, and why nonfair acquisition often improves throughput.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Nonfair Acquisition

A nonfair semaphore can allow a newly running thread to take an available permit without honoring the exact wait order of queued threads.

Contended waiters still use AQS shared queueing.

2. Why Use It?

Barging can avoid unnecessary wakeup/context-switch cost and usually improves throughput.

3. Cost

Wait-time fairness is weaker and starvation risk can be higher under pathological contention.

Use the default nonfair mode when throughput matters and strict fairness is not a requirement; measure fair mode when latency distribution matters.

Loading helpful count