Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Show the correct usage pattern for ReentrantLock and list features it offers that synchronized does not.

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.

Java
class Account {
private final ReentrantLock lock = new ReentrantLock();
private long balance;
void deposit(long amount) {
lock.lock(); // 1. acquire before the try
try {
balance += amount; // 2. critical section
} finally {
lock.unlock(); // 3. always released, even if the body throws
}
}
}

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.

Java
if (lock.tryLock(200, TimeUnit.MILLISECONDS)) { // waits up to 200 ms, may throw InterruptedException
try {
refreshCache();
} finally {
lock.unlock();
}
} else {
log.warn("cache refresh already running, skipping"); // false: we never held the lock
}

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.

Java
void process() throws InterruptedException {
lock.lockInterruptibly(); // a waiting thread can be cancelled here
try {
doWork();
} finally {
lock.unlock();
}
}
// worker.interrupt() while it waits -> InterruptedException, the lock was never taken

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.

Java
private final Condition notEmpty = lock.newCondition();
private final Condition notFull = lock.newCondition();
// take(): while (count == 0) notEmpty.await(); ... notFull.signal();
// put(): while (count == items.length) notFull.await(); ... notEmpty.signal();

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.

Java
ReentrantLock lock = new ReentrantLock();
lock.lock();
lock.lock(); // reentrant: same thread again
System.out.println(lock.getHoldCount()); // 2
System.out.println(lock.isHeldByCurrentThread());// true
lock.unlock();
System.out.println(lock.isLocked()); // true, one hold remains
lock.unlock();
System.out.println(lock.isLocked()); // false
ReentrantLock compared with synchronized
CapabilitysynchronizedReentrantLock
mutual exclusion and visibilityyesyes
reentrantyesyes
released automaticallyyes, at block endno, unlock() in finally
give up or time outnotryLock(), tryLock(timeout, unit)
interruptible waitnolockInterruptibly()
wait sets per lockone (wait/notify)many (newCondition())
fair modenonew ReentrantLock(true)
inspectionThread.holdsLock onlyisLocked, 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.

What to say in the interviewAbout 80 seconds aloud

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.
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/