ReentrantLock is a mutual-exclusion lock in java.util.concurrent.locks that only one thread can hold at a time. It is called reentrant because the thread that owns the lock can acquire it again without blocking, and it must release it the same number of times. Compared with a synchronized block it adds timed and interruptible locking, optional fairness and multiple Condition objects.
Use synchronized for simple critical sections and switch to ReentrantLock when you need one of those extra features. The rest of this article shows the API, the correct try/finally pattern and a complete runnable example that works on current Java versions (Java 8 and later, including Java 21 and 25 LTS).
Reentrant means that a lock remembers its owner and a hold count. When a thread that already holds the lock calls lock() again, the count goes from 1 to 2 instead of blocking the thread against itself. The lock is free for other threads only when the owner has called unlock() as many times as it called lock().
This matters when one locked method calls another locked method of the same object. Without reentrancy that call would deadlock. Java's synchronized keyword is also reentrant, so both mechanisms share this behaviour.
You can inspect the state with getHoldCount(), isHeldByCurrentThread() and isLocked(), which are useful in assertions and tests.
The one rule you must follow is to call unlock() in a finally block. A synchronized block releases its monitor automatically when an exception escapes, but a ReentrantLock does not, so a missing unlock() leaves the lock held forever and every other thread blocks.
Call lock() before the try, not inside it. If acquiring failed inside the try, the finally would call unlock() on a lock the thread does not hold and throw IllegalMonitorStateException.
| Feature | synchronized | ReentrantLock |
|---|---|---|
| Try without waiting | No | tryLock() |
| Wait with a timeout | No | tryLock(timeout, unit) |
| Interruptible while waiting | No | lockInterruptibly() |
| Fair ordering | No | new ReentrantLock(true) |
| Multiple wait conditions | One per monitor | Many Condition objects |
| Automatic release | Yes | No, use finally |
| Inspect state | No | Hold count, queue length, owner checks |
tryLock() returns immediately with true or false, and the timed version waits up to a limit. This lets a thread give up and do something else, which is a practical way to avoid deadlocks when several locks are involved.
new ReentrantLock(true) creates a fair lock that grants access to the longest-waiting thread. The default is unfair, which is usually faster because it allows a running thread to barge in ahead of a queued one. Choose fairness only if starvation is a real problem, since it lowers throughput.
lock.newCondition() returns a Condition that works like wait() and notify() but you can create several for one lock, for example "not full" and "not empty" for a buffer. await() releases the lock while waiting and re-acquires it before returning. Always call await() in a while loop, because a thread can wake up spuriously.
The program below combines a reentrant counter used by several threads with a bounded buffer that uses two conditions. It compiles with javac Main.java and runs with java Main.
The expected output is:
Eight threads each call incrementTwice() 1,000 times, which increments the counter 16,000 times with no lost updates. The buffer holds at most two items, so the producer blocks on notFull when it is ahead and the consumer blocks on notEmpty when it is empty. In production code you would normally use a ready-made ArrayBlockingQueue or LinkedBlockingQueue instead of writing this yourself.
Virtual threads (final in Java 21) work well with ReentrantLock. A virtual thread blocked on a ReentrantLock unmounts from its carrier thread and lets other work run. In Java 21 through 23, blocking while inside a synchronized block could pin the carrier thread, which is why ReentrantLock was the recommended choice for long blocking sections. Newer JDKs (Java 24 and later) removed most synchronized pinning, so both are fine for short critical sections, but ReentrantLock is still the better fit when you need timeouts or interruption.
synchronized for short, simple critical sections. It is shorter and cannot leak a lock.ReentrantLock when you need tryLock, timeouts, interruptible waits, fairness or several conditions.ReentrantReadWriteLock or StampedLock when reads vastly outnumber writes.AtomicInteger or LongAdder for a single counter, which is often faster than any lock.BlockingQueue before writing your own locking code.Not by default. Performance is similar in modern JVMs, so the choice is about features. Use ReentrantLock when you need timed, interruptible or fair locking or several conditions.
The lock stays held by that thread and every other thread that needs it blocks forever. Always put unlock() in a finally block.
No. Only the owning thread can call unlock(). Another thread that tries gets an IllegalMonitorStateException.
No. The no-argument constructor creates an unfair lock, and you pass true to request fairness.
No. It coordinates threads inside one JVM only. For several processes or machines, use a database lock, a file lock or a distributed lock service.
ReentrantLock is an explicit lock that the owning thread can acquire repeatedly, with tryLock, timeouts, interruptible waits, optional fairness and multiple Condition objects. Always acquire it before a try block and release it in finally. Use synchronized when you do not need those extras, and prefer higher-level concurrency utilities whenever they fit.