Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

On a Java 25 codebase, how would you choose between synchronized and ReentrantLock for new code, including when virtual threads are used?

On Java 25, I use synchronized by default and ReentrantLock only when I need one of its features: timed or interruptible acquisition, several Condition objects, or fairness. The old reason to prefer ReentrantLock with virtual threads, pinning of the carrier thread inside synchronized, was largely removed in JDK 24 by JEP 491. On Java 21 to 23, I would still use ReentrantLock around blocking operations reached by virtual threads, and on any version I read lock owners in thread dumps with jstack -l or jcmd.

Why this question is asked

Choosing a lock looks like a small decision, but it affects how easy the code is to review, how it behaves under virtual threads and how quickly we can diagnose a hang. The advice changed twice in a few years: virtual threads arrived in Java 21 with a pinning problem that made ReentrantLock the safe choice, and JDK 24 removed most of that problem. Interviewers ask this to see whether our rules are current and based on reasons. In this answer, we will recap the two options, look at what pinning was and what JEP 491 changed, set out the default and the exceptions, cover the advice for Java 21 to 23, and finish with how both appear in thread dumps.

1A Quick Recap of the Two Options

Both synchronized and ReentrantLock give mutual exclusion, so only one thread runs the critical section at a time, and both give the same memory visibility guarantees: changes made under the lock are visible to the next thread that takes it. Both are reentrant.

The difference is in what surrounds that core. synchronized is a language construct: the JVM acquires the monitor when the block starts and always releases it when the block ends, even on an exception. ReentrantLock is a library class with more features, but we have to call unlock() ourselves in a finally block.

2What Pinning Was and What JEP 491 Changed

A virtual thread runs on top of a small pool of platform threads called carriers. When a virtual thread blocks, for example on a socket read, the JVM normally unmounts it: it saves its stack and frees the carrier to run another virtual thread. That is how a few carriers serve thousands of virtual threads.

In Java 21 to 23, a virtual thread that blocked while inside a synchronized block or method could not unmount; it was pinned to its carrier, because the monitor implementation was tied to the platform thread. If many virtual threads blocked on I/O inside synchronized code, all the carriers could be pinned and the application would stall, even though most threads were only waiting. ReentrantLock did not have this problem, because it is built on LockSupport.park, which virtual threads handle.

JEP 491, delivered in JDK 24, reimplemented monitors so that a virtual thread can unmount while it holds or waits for a monitor and while it is in Object.wait(). So on JDK 24 and later, blocking inside synchronized usually no longer pins the carrier. A few rarer cases can still pin, such as blocking while a native frame is on the stack or during class initialisation; check JEP 491 for the exact list.

Virtual thread blocking inside a lock
SituationJava 21 to 23Java 24 and later
blocks on I/O inside synchronizedpins the carrierunmounts, carrier is free
waits to enter a synchronized blockpins the carrierunmounts
Object.wait() inside synchronizedpins the carrierunmounts
blocks inside ReentrantLockunmountsunmounts
blocks with a native frame on the stackpinsstill pins
On a current JDK the main virtual thread argument for ReentrantLock is gone.

3The Default on Java 25: synchronized

With pinning mostly gone, the reasons to prefer synchronized are back in front. It cannot leave a lock held by a forgotten or misplaced unlock(), because release is automatic. The scope of the lock is visible in the code's structure. And for typical, moderately contended use, performance is similar to ReentrantLock in current HotSpot, so speed is not a reason to choose either one.

Java
class Inventory {
private final Map<String, Integer> stock = new HashMap<>();
synchronized void reserve(String sku, int qty) { // simple, always released
int have = stock.getOrDefault(sku, 0);
if (have < qty) throw new IllegalStateException("not enough " + sku);
stock.put(sku, have - qty);
}
}

One practical detail: lock on a private final object or on this in a class we control, never on a value-based class such as Integer or on an interned String, because other code may lock the same object.

4When ReentrantLock Is Worth It

ReentrantLock earns its extra ceremony when we need something synchronized cannot do. We need tryLock or tryLock with a timeout when a caller should give up instead of waiting, for example a cache refresh that should be skipped if one is already running, or deadlock avoidance in a transfer between two accounts. We need lockInterruptibly when a waiting thread must be cancellable. We need several Condition objects when different threads wait for different states, as in a bounded buffer with notFull and notEmpty. And we need the fair mode when starvation is a measured problem, accepting lower throughput.

Java
private final ReentrantLock refreshLock = new ReentrantLock();
void refreshIfIdle() {
if (!refreshLock.tryLock()) return; // another thread is refreshing: skip, do not wait
try {
reload();
} finally {
refreshLock.unlock();
}
}
Step throughChoosing a lock for new code on Java 25
  1. Can we avoid shared mutable state?
    YesImmutable data, confinement or a concurrent collectionNoContinue
  2. Need timeout, interruptible wait, several conditions or fairness?
    YesReentrantLock with unlock in finallyNosynchronized
  3. Code also runs on Java 21 to 23 with virtual threads and blocks inside the lock?
    YesReentrantLock to avoid pinningNoKeep the choice above

3 steps in order. Play them, or step through them one at a time.

5If We Still Run Java 21 to 23

Many services stay on Java 21, an LTS release, for a long time. There, the pinning problem is real. If code reached by virtual threads blocks inside a lock, especially on network or database I/O, prefer ReentrantLock, or restructure so the blocking happens outside the lock. JDBC drivers and connection pools were a common source, and many of them replaced internal synchronized blocks with ReentrantLock for this reason (check your driver version).

On those versions, JFR records a jdk.VirtualThreadPinned event when a virtual thread blocks while pinned for longer than a threshold, which is the easiest way to find the hot spots. The older -Djdk.tracePinnedThreads system property printed stack traces for pinning, but it was removed in JDK 24, so do not rely on it after an upgrade.

6How Both Appear in Thread Dumps

Diagnosis matters when choosing too. A thread blocked on synchronized shows as BLOCKED with the line 'waiting to lock <0x...>', and the owner shows 'locked <0x...>' in its stack. A thread waiting on a ReentrantLock shows as WAITING (parking) with 'parking to wait for <0x...> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)', and the owner is not visible in the stack frames. To see who holds a java.util.concurrent lock, ask for locked ownable synchronizers with jstack -l <pid> or jcmd <pid> Thread.print -l, which list the lock under its owning thread.

Virtual threads are not all listed in the classic dump. jcmd <pid> Thread.dump_to_file -format=json <file> writes every thread, including virtual ones, which is what we want when thousands of virtual threads are waiting on one lock.

7Follow-up questions

Is ReentrantLock faster than synchronized?

Not in any way that should drive the choice on a current JDK. Early Java versions had a large gap under contention, but synchronized has been optimised heavily since then. Measure the real workload if it matters; for most code the difference is lost in the work done inside the lock.

Does JEP 491 mean virtual threads never pin?

No. They can still pin in a few cases, for example when blocking with a native method frame on the stack, or in some class initialisation situations. They are much rarer than synchronized blocks, and the JFR jdk.VirtualThreadPinned event still reports them.

Should we rewrite existing ReentrantLock code back to synchronized after upgrading?

Not as a project of its own. Working ReentrantLock code with correct finally blocks is fine. I would apply the new default to new code and simplify old code only when we touch it for other reasons and it uses none of ReentrantLock's features.

8Conclusion

In this answer, we saw that on Java 25 synchronized is the default for new code, because it is simpler, cannot be left locked by mistake and performs similarly, and because JEP 491 in JDK 24 removed the usual pinning of virtual threads inside synchronized. ReentrantLock remains the right tool when we need timed or interruptible acquisition, several conditions or fairness. On Java 21 to 23, ReentrantLock is still safer around blocking operations used by virtual threads, and on every version jstack -l or jcmd Thread.print -l shows who owns a java.util.concurrent lock.

What to say in the interviewAbout 90 seconds aloud

Open with

On Java 25 my default for new code is synchronized. It gives the same mutual exclusion and visibility as ReentrantLock, the JVM always releases it so nobody can forget an unlock, and for normal use the performance is similar.

Then, if they want more

The reason many of us switched to ReentrantLock was virtual threads on Java 21: a virtual thread that blocked inside synchronized was pinned to its carrier, so I/O inside synchronized could exhaust the carriers.

JEP 491 in JDK 24 reimplemented monitors so that virtual threads can unmount while holding or waiting for a monitor, so on 24 and later that reason mostly disappears. A few cases still pin, such as native frames on the stack, and JFR's VirtualThreadPinned event shows them.

I choose ReentrantLock when I need its features: tryLock with or without a timeout to skip or avoid deadlock, lockInterruptibly so a waiting thread can be cancelled, several Conditions like notFull and notEmpty, or a fair mode when starvation is measured.

If a service is still on Java 21 to 23 and uses virtual threads, I prefer ReentrantLock around blocking calls, or move the blocking outside the lock. For diagnosis, synchronized owners show directly in thread dumps, while for ReentrantLock I use jstack -l or jcmd Thread.print -l to see the owning thread.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 24, Java 25
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 "On a Java 25 codebase, how would you choose between synchronized and ReentrantLock for...": https://scrivio-interview-library.netlify.app/java/locks/6a2eb87844/