Medium👤 3-5 years👤 8-15 years 1 min read

ReentrantLock vs synchronized — when do you reach for the explicit lock?

Asked inAmazonMicrosoftGoogle
#reentrantlock#synchronized#trylock#fairness#condition
Report issue

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

Coffee Chat Question

Concept Made Simple

ReentrantLock vs synchronized — when do you reach for the explicit lock?

🧠Mind Map Answer

Remember It Faster

synchronizedsimple, auto-release, no timeout
ReentrantLocktryLock, timeout, interruptible, fair
Conditionsmultiple wait-sets per lock

⌨️Hands-on Keyboard

Learn by Doing

java
if (lock.tryLock(2, TimeUnit.SECONDS)) {
    try { criticalSection(); }
    finally { lock.unlock(); } // MUST unlock in finally
} else {
    // couldn't acquire in time → fail fast / fallback
}

🔥What If?

Think Beyond the Expected

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/interruptiblefairness optionmultiple Conditionsunlock in finallyprefer 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

  • 1What does lock fairness cost in throughput?
  • 2How do ReadWriteLock / StampedLock differ?
  • 3Why must unlock() be in a finally block?

🧩Related Technologies

ReadWriteLockStampedLockCondition

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions