Fundamentals 10 min read

Differences Between ReentrantLock and synchronized in Java: An Interview‑Style Explanation

This article explains the key differences between Java's ReentrantLock and synchronized, covering fairness, tryLock, timeout, interruptibility, condition support, underlying implementations, and usage examples with code snippets, presented as an interview dialogue for candidates.

IT Services Circle
IT Services Circle
IT Services Circle
Differences Between ReentrantLock and synchronized in Java: An Interview‑Style Explanation

The text presents an interview‑style discussion that explores the fundamental differences between Java's synchronized keyword and the java.util.concurrent.locks.ReentrantLock class, focusing on practical usage and internal mechanisms.

Fairness : synchronized is a non‑fair lock; it does not check the waiting queue before acquiring the monitor. ReentrantLock can be created as either fair or non‑fair by passing a boolean to its constructor, and the fair implementation checks hasQueuedPredecessors() before attempting acquisition.

Try‑lock and timeout : ReentrantLock provides tryLock() for a non‑blocking attempt and tryLock(long timeout, TimeUnit unit) for a timed attempt. Example implementation:

public boolean tryLock() {
    return sync.nonfairTryAcquire(1);
}

public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
    return sync.tryAcquireNanos(1, unit.toNanos(timeout));
}

The timed version converts the timeout to nanoseconds and repeatedly attempts acquisition until the deadline expires.

Interruptible lock acquisition : ReentrantLock.lockInterruptibly() reacts to thread interruption by throwing InterruptedException , whereas a thread blocked in synchronized cannot be interrupted. The implementation checks for interruption during the park phase and aborts the acquisition if an interrupt is detected.

Condition support : synchronized relies on Object.wait()/notify()/notifyAll() , which cannot target specific conditions. ReentrantLock can create one or more Condition objects, enabling fine‑grained await/signal semantics.

Underlying mechanisms :

Lock object: synchronized uses the object header (monitor) to store lock state; ReentrantLock uses a volatile int state field.

Implementation: synchronized may upgrade from no‑lock to biased, lightweight, and finally heavyweight (kernel) locks; ReentrantLock relies on CAS loops and volatile reads/writes.

Release: synchronized releases automatically at the end of the block; ReentrantLock requires an explicit unlock() , typically placed in a finally block.

Summary of differences :

synchronized is always non‑fair; ReentrantLock can be fair or non‑fair.

synchronized lacks try‑lock and timed lock; ReentrantLock provides both.

synchronized cannot be interrupted while waiting; ReentrantLock offers interruptible acquisition.

synchronized has no built‑in condition objects; ReentrantLock supports Condition for precise thread signaling.

Implementation details, lock object storage, and release semantics differ between the two.

JavaConcurrencyInterviewLockReentrantLocksynchronized
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.