Quick revision
The short answer
The correct pattern is to call lock.lock() immediately before a try block and lock.unlock() in its finally block, so the lock is released on every path and only after it was really acquired. On top of what synchronized gives, ReentrantLock offers tryLock() and tryLock(timeout, unit) to avoid waiting forever, lockInterruptibly() so a waiting thread can be cancelled, several Condition objects per lock through newCondition(), an optional fair mode, and inspection methods such as isLocked(), getHoldCount() and getQueueLength().
What to remember
In this answer, we saw that the correct ReentrantLock pattern calls lock() directly before try and unlock() in finally, so the lock is released on every path and only after it was really acquired. We then went through the features synchronized does not offer: tryLock with and without a timeout, lockInterruptibly for cancellable waits, multiple Condition objects through newCondition(), an optional fair mode, and inspection methods such as isLocked, getHoldCount and getQueueLength.
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.
The correct pattern is to call lock.lock() immediately before a try block and lock.unlock() in its finally block, so the lock is released on every path and only after it was really acquired. On top of what synchronized gives, ReentrantLock offers tryLock() and tryLock(timeout, unit) to avoid waiting forever, lockInterruptibly() so a waiting thread can be cancelled, several Condition objects per lock through newCondition(), an optional fair mode, and inspection methods such as isLocked(), getHoldCount() and getQueueLength().
Why this question is asked
synchronized is enough for most locking in Java, but there are situations it cannot handle: giving up after a timeout, cancelling a waiting thread, or waiting for two different conditions on one lock. ReentrantLock covers those, at the cost of making us release the lock ourselves. Interviewers ask for the usage pattern because getting it slightly wrong leaves a lock held forever, and they ask for the feature list to see whether we know when the extra complexity is worth it. In this answer, we will recap what a reentrant lock is, write the correct pattern and see why each line is where it is, and then go through each extra feature with a small example.
1A Quick Recap of ReentrantLock
A lock makes sure that only one thread at a time runs a block of code, called the critical section, and it also makes the changes made inside the block visible to the next thread that takes the lock. ReentrantLock in java.util.concurrent.locks does the same job as a synchronized block, with the same memory guarantees.
Reentrant means that a thread that already holds the lock can acquire it again without blocking itself. The lock counts how many times the owner has acquired it, the hold count, and it is only released when the owner has called unlock() the same number of times. synchronized is reentrant in the same way.
2The Correct Usage Pattern
With synchronized, the JVM releases the monitor automatically when the block ends, even through an exception. With ReentrantLock, we must do it ourselves, and the only reliable way is a finally block.
Each line's position matters. lock() is called before try, not inside it, because if lock() itself failed, the finally block would call unlock() on a lock this thread does not hold, which throws IllegalMonitorStateException and hides the original problem. Placing lock() directly before try means finally only runs once the lock is really held. unlock() is in finally so that an exception in the body, or an early return, cannot leave the lock held forever, which would block every other thread that ever wants it.
Notice also that the lock is a private final field. If it could be reassigned, two threads could end up locking different lock objects and both enter the critical section.
3Feature 1: tryLock Without Waiting Forever
A thread entering a synchronized block waits until it gets the monitor, however long that takes. tryLock() returns immediately with false if the lock is taken, and tryLock(timeout, unit) waits at most the given time. This lets the code do something else, report busy, or avoid a deadlock.
Notice that unlock() is only called on the branch where tryLock returned true. The try-finally goes inside the if, for the same reason lock() goes before try.
4Feature 2: lockInterruptibly for Cancellable Waits
A thread blocked on a synchronized monitor cannot be interrupted out of the wait; Thread.interrupt() only sets a flag. lockInterruptibly() waits for the lock but throws InterruptedException if the thread is interrupted while waiting, so a shutdown or a cancelled task can stop waiting.
5Feature 3: Several Conditions per Lock
A Condition lets a thread wait inside the lock until some state changes, like wait() and notify() on a monitor. The difference is that a monitor has exactly one wait set, while a ReentrantLock can create as many Condition objects as we need with newCondition(). A bounded queue can then have one condition for 'not full' and one for 'not empty', and wake only the threads waiting for the change that just happened.
6Feature 4: Fairness and Inspection
new ReentrantLock(true) creates a fair lock, which hands the lock to the longest-waiting thread instead of letting a newly arriving thread barge in. It reduces starvation but usually lowers throughput considerably, so the default is non-fair. synchronized has no fairness option.
ReentrantLock also lets us look inside it, which helps with monitoring, tests and debugging: isLocked() tells whether any thread holds it, isHeldByCurrentThread() whether we do, getHoldCount() how many times the current thread holds it, and getQueueLength() roughly how many threads are waiting.
| Capability | synchronized | ReentrantLock |
|---|---|---|
| mutual exclusion and visibility | yes | yes |
| reentrant | yes | yes |
| released automatically | yes, at block end | no, unlock() in finally |
| give up or time out | no | tryLock(), tryLock(timeout, unit) |
| interruptible wait | no | lockInterruptibly() |
| wait sets per lock | one (wait/notify) | many (newCondition()) |
| fair mode | no | new ReentrantLock(true) |
| inspection | Thread.holdsLock only | isLocked, getHoldCount, getQueueLength |
7Follow-up questions
Why is lock() not placed inside the try block?
Because if acquiring the lock fails, for example lockInterruptibly() throwing InterruptedException, the finally block would call unlock() on a lock this thread does not hold. That throws IllegalMonitorStateException, which replaces the original exception. With lock() just before try, finally only runs after the lock was acquired.
If ReentrantLock can do more, why not use it everywhere?
Because synchronized cannot be forgotten: the JVM always releases it. It is shorter, reviewers see the scope at a glance, and performance is similar for ordinary use. I use ReentrantLock only when I need one of its extra features.
Does tryLock() on a fair lock respect fairness?
No. The untimed tryLock() barges: it takes the lock if it is free at that moment, even if other threads are queued. tryLock(0, TimeUnit.SECONDS) does respect the fairness setting.
8Conclusion
In this answer, we saw that the correct ReentrantLock pattern calls lock() directly before try and unlock() in finally, so the lock is released on every path and only after it was really acquired. We then went through the features synchronized does not offer: tryLock with and without a timeout, lockInterruptibly for cancellable waits, multiple Condition objects through newCondition(), an optional fair mode, and inspection methods such as isLocked, getHoldCount and getQueueLength.
Open with
The pattern is: call lock.lock() on the line immediately before the try, put the critical section in the try, and call lock.unlock() in the finally. Unlock goes in finally so that an exception or an early return can never leave the lock held.
Then, if they want more
Lock goes before try, not inside it, because if acquiring failed, finally would call unlock on a lock I do not hold, which throws IllegalMonitorStateException and hides the real error. I also keep the lock in a private final field.
Compared with synchronized, ReentrantLock gives me tryLock, which returns false immediately if the lock is taken, and tryLock with a timeout, so a thread does not wait forever and I can avoid deadlocks. It gives me lockInterruptibly, so a waiting thread can be cancelled by interrupting it, which a synchronized wait cannot.
I can create several Condition objects with newCondition, for example notFull and notEmpty in a bounded buffer, so a signal only wakes the threads waiting for that state. It has an optional fair mode that serves the longest-waiting thread, at a throughput cost. And it has inspection methods like isLocked, isHeldByCurrentThread, getHoldCount and getQueueLength.
If I need none of those, I use synchronized, because it cannot be left locked by mistake.
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 "Show the correct usage pattern for ReentrantLock and list features it offers that...": https://scrivio-interview-library.netlify.app/java/locks/c366b7e53c/