MediumExperience level: 3-5 years · 8-15 years1 min read

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

Reviewed by Gurusankar M.

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.

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

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
}

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

  1. What does lock fairness cost in throughput?
  2. How do ReadWriteLock / StampedLock differ?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.