ReentrantLock vs synchronized — when do you reach for the explicit lock?
Reviewed by Gurusankar M.
Short Answer
synchronized is simpler and auto-released. ReentrantLock adds tryLock (with timeout), interruptible locking, fairness, and multiple Conditions — use it when you need those; otherwise prefer synchronized. Always unlock() in finally.
On this page
Concept Made Simple
Coffee Chat Question
“ReentrantLock vs synchronized — when do you reach for the explicit lock?”
Remember It Faster
Mind Map Answer
- synchronized
- simple, auto-release, no timeout
- ReentrantLock
- tryLock, timeout, interruptible, fair
- Conditions
- multiple wait-sets per lock
Learn by Doing
Hands-on Keyboard
if (lock.tryLock(2, TimeUnit.SECONDS)) {
try { criticalSection(); }
finally { lock.unlock(); } // MUST unlock in finally
} else {
// couldn't acquire in time → fail fast / fallback
}Think Beyond the Expected
What If?
Why is tryLock(timeout) a useful tool against deadlock?
Instead of blocking forever to acquire a lock (risking deadlock), tryLock with a timeout lets a thread give up, back off, release any locks it holds, and retry — breaking potential deadlock cycles and enabling graceful degradation.
Real World
tryLock with timeout is used for lock-ordering-resistant code and to fail fast under contention (e.g. 'couldn't get the lock in 200ms → return 503') instead of piling up blocked threads.
Interviewer's Expectation
Keywords they're listening for:
- tryLock/timeout/interruptible
- fairness option
- multiple Conditions
- unlock in finally
- prefer synchronized when simple
Common Mistakes
- Forgetting unlock() in finally (permanent lock)
- Using ReentrantLock where synchronized suffices
- Enabling fairness and tanking throughput unnecessarily
Best Practices
- unlock() in finally, always
- Use synchronized unless you need Lock features
- Consider ReadWriteLock/StampedLock for read-heavy data
Follow-up Questions
- What does lock fairness cost in throughput?
- How do ReadWriteLock / StampedLock differ?
- Why must unlock() be in a finally block?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Locks (Multithreading) Interview question: "ReentrantLock vs synchronized — when do you reach for the explicit lock?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair