NOTE

Nonfair ReentrantLock

Default ReentrantLock barging behavior, why it can improve throughput, and how AQS queues still handle contended waiters.

JavaCreated Updated 1 min readhistorical

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

1. Nonfair Does Not Mean No Queue

The default ReentrantLock is nonfair. A newly arriving/running thread may acquire an available lock even when other threads have waited longer.

Contended losers still use AQS queueing and parking.

2. Why Barge?

A thread already executing on a CPU may be able to acquire the lock immediately, avoiding wakeup/context-switch delay. This can improve throughput under many workloads.

3. Trade-Off

The cost is weaker fairness and potentially longer waits for unlucky contenders.

If starvation/fairness is a concrete requirement, test fair mode and measure its cost rather than assuming the default is wrong.

4. Implementation Caveat

Exact fast paths and atomic primitives vary across JDK versions. Rely on the documented fairness contract, not historical Unsafe/CAS source code.

Loading helpful count