Quick revision
The short answer
A fair ReentrantLock grants the lock, roughly in arrival order, to the thread that has waited longest. The default non-fair lock allows barging: a thread that arrives just as the lock is released can take it ahead of threads already queued. Fair locks usually have much lower throughput, because every hand-off has to wake a parked thread. The untimed tryLock() barges even on a fair lock, while tryLock(0, TimeUnit.SECONDS) respects fairness, and a fair lock still does not make thread scheduling fair.
What to remember
In this answer, we saw that a fair ReentrantLock grants the lock roughly in arrival order to the longest-waiting thread, while the default non-fair lock allows barging, letting an arriving thread take a just-released lock ahead of the queue. Fairness costs throughput because every hand-off must wake and schedule a parked thread while the lock sits idle. The untimed tryLock() barges even on a fair lock, whereas tryLock(0, TimeUnit.SECONDS) respects fairness, and a fair lock does not make the operating system's thread scheduling fair.
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.
A fair ReentrantLock grants the lock, roughly in arrival order, to the thread that has waited longest. The default non-fair lock allows barging: a thread that arrives just as the lock is released can take it ahead of threads already queued. Fair locks usually have much lower throughput, because every hand-off has to wake a parked thread. The untimed tryLock() barges even on a fair lock, while tryLock(0, TimeUnit.SECONDS) respects fairness, and a fair lock still does not make thread scheduling fair.
Why this question is asked
Fairness sounds like an obvious good, so it surprises people that Java's locks are unfair by default and that switching to fair mode can make a service much slower. Understanding why requires a picture of what happens when a lock is released and a waiting thread has to be woken. Interviewers ask this to see whether we know the trade-off and the exact rules, including the tryLock exception, which catches out many people. In this answer, we will recap how threads wait for a ReentrantLock, explain what fairness guarantees, see barging in action, understand the cost, go through the tryLock rules, and finish with what fairness does not guarantee.
1A Quick Recap of How Threads Wait for a Lock
When a thread asks for a ReentrantLock that another thread holds, it does not spin forever. It joins a queue, the wait queue of the AbstractQueuedSynchronizer that the lock is built on, and parks, which means the operating system stops scheduling it. When the holder calls unlock(), the lock wakes the first queued thread. That thread must then be scheduled by the operating system, which takes some microseconds, and only then can it try to take the lock.
The question of fairness is what happens during that gap: the lock is free, the woken thread is not yet running, and some other thread that was not in the queue at all calls lock().
2What Fairness Guarantees
new ReentrantLock(true) creates a fair lock. Before taking a free lock, a thread checks whether other threads are already queued. If they are, it joins the end of the queue instead of taking the lock. So the lock is granted in roughly first-come, first-served order: the longest-waiting thread gets it next. 'Roughly' because arrival order is only as precise as the moment each thread entered the queue.
new ReentrantLock(), or new ReentrantLock(false), creates the default non-fair lock. It makes no promise about order. A thread calling lock() first tries to grab the lock immediately, and only joins the queue if that fails.
3Barging in a Non-Fair Lock
Taking a just-released lock ahead of queued threads is called barging. It happens all the time in a busy non-fair lock, and it is usually a good thing for throughput.
5 messages in order. Play them, or step through them one at a time.
Non-fair: C never waited, yet goes first. Fair: C would have joined the queue behind B.
4What Fairness Costs
Look at the gap again. In the non-fair lock, the lock is used during the gap by C, which was already running and had its data in its CPU cache. In the fair lock, the lock stays idle until B is woken, scheduled and running, and C must park, which means C will later need its own wake-up. Every hand-off becomes a context switch, typically several microseconds, during which nobody holds the lock.
If the critical section itself takes a microsecond, the lock can spend more time idle between owners than in use. That is why fair locks usually have much lower throughput under contention, often several times lower in benchmarks, though the exact factor depends on hardware and workload. Non-fair locks also let a thread that releases the lock and immediately needs it again, for example in a loop, keep it without any switch at all.
The benefit of fairness is predictability: no thread can be passed over indefinitely, so the worst-case wait is bounded by the queue in front of it. Fair mode trades average throughput for lower variance in waiting time.
| Aspect | Non-fair (default) | Fair (new ReentrantLock(true)) |
|---|---|---|
| who gets a free lock | whoever asks first, even if not queued | longest-waiting queued thread |
| barging | allowed | not allowed for lock() and timed tryLock |
| hand-off cost | often none; running thread takes it | wake and schedule a parked thread |
| throughput under contention | high | usually much lower |
| worst-case wait | unbounded in principle | bounded by the queue ahead |
5Does tryLock() Respect Fairness?
Not the untimed one. tryLock() is defined to take the lock if it is free at the moment of the call, whether or not other threads are waiting, even on a fair lock. It barges. The idea is that tryLock() is an opportunistic 'only if I can get it right now' call, and making it queue would defeat that.
The timed version, tryLock(timeout, unit), does respect the fairness setting. On a fair lock it will not take the lock ahead of queued threads. So if we want an immediate attempt that still honours fairness, we write tryLock(0, TimeUnit.SECONDS), which returns at once but follows the fair ordering.
Notice that the timed version declares InterruptedException, so it needs handling where the untimed one does not. That small difference often explains why code uses the barging variant without realising it.
6What Fairness Does Not Guarantee
A fair lock is fair among threads that are waiting in its queue. It says nothing about the operating system's thread scheduler. A thread that is runnable but not currently scheduled on a CPU, perhaps because the machine is busy, cannot take its turn until it runs. Meanwhile, another thread that is running may release the lock, come back, find nobody able to take it yet and obtain it again several times in a row, while other threads that want it are not making progress. So a fair lock reduces starvation in the lock, but it cannot make the threads themselves progress at equal rates.
Fairness also applies to one lock only. If a request takes three locks, fairness on one of them does not make the request's total waiting time fair. And it does not fix the underlying issue when a lock is heavily contended; it only changes who waits.
7Follow-up questions
When would you actually use a fair lock?
When bounded waiting matters more than throughput and contention is moderate, for example a lock where starvation of some requests was measured and the critical section is long compared with the cost of a context switch. Even then, I would first try to reduce contention, because that helps both throughput and fairness.
Is synchronized fair?
No. synchronized makes no fairness promise and has no fair mode; in practice it behaves like a non-fair lock, with spinning and barging. If strict ordering is needed, a fair ReentrantLock is the standard option.
Are ReentrantReadWriteLock and Semaphore fair?
Both have fair and non-fair modes chosen in the constructor, with non-fair as the default, and the same trade-off applies. Their untimed tryAcquire or tryLock methods barge in the same way as ReentrantLock's untimed tryLock.
8Conclusion
In this answer, we saw that a fair ReentrantLock grants the lock roughly in arrival order to the longest-waiting thread, while the default non-fair lock allows barging, letting an arriving thread take a just-released lock ahead of the queue. Fairness costs throughput because every hand-off must wake and schedule a parked thread while the lock sits idle. The untimed tryLock() barges even on a fair lock, whereas tryLock(0, TimeUnit.SECONDS) respects fairness, and a fair lock does not make the operating system's thread scheduling fair.
Open with
A fair ReentrantLock, created with new ReentrantLock(true), grants the lock roughly in arrival order: when it becomes free, the thread that has waited longest gets it, and a newly arriving thread joins the back of the queue.
Then, if they want more
The default is non-fair, which allows barging: when the holder unlocks, the lock wakes the first queued thread, but that takes microseconds to be scheduled, and in the meantime a thread that just arrived can grab the free lock.
That sounds bad, but it is why non-fair locks are fast: the lock is used by a thread that is already running instead of sitting idle during a context switch. With a fair lock every hand-off requires waking a parked thread, so under contention throughput is usually much lower, while the benefit is a bounded wait and less tail latency.
On tryLock: the untimed tryLock barges even on a fair lock, it takes the lock if it is free right now regardless of the queue. If I want an immediate attempt that respects fairness, I use tryLock(0, TimeUnit.SECONDS), which follows the fair ordering, though it declares InterruptedException.
Finally, lock fairness is not scheduling fairness: a fair lock only orders the threads in its queue, and a thread that the OS is not running cannot take its turn, so one thread can still get the lock several times in a row.
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 "What does a fair ReentrantLock guarantee, what does it cost, and does tryLock() respect...": https://scrivio-interview-library.netlify.app/java/locks/23aa6d64d0/