Course contentsShow
← Java
Lesson 2672 of 2,87049. Synchronization and LockingFree lesson

Intrinsic Locks Are Reentrant by Default

Learning that Java's synchronized keyword uses reentrant locks, allowing a thread to re-acquire a lock it already holds.

Intrinsic Locks Are Reentrant by Default

You'll learn why Java's synchronized keyword allows a thread to safely acquire the same lock multiple times without deadlocking itself.

What Reentrancy Means in Practice

When you learned about reentrancy, you discovered that a lock can be acquired multiple times by the same thread. Now here's the crucial point: Java's intrinsic locks (the monitors used by synchronized) are reentrant by default. This means when a thread holds a lock via synchronized, it can enter other synchronized blocks or methods protected by that same lock without blocking itself.

Why This Matters

Imagine a synchronized method that calls another synchronized method on the same object:

public synchronized void outer() {
    System.out.println("In outer");
    inner();  // Can I call this?
}

public synchronized void inner() {
    System.out.println("In inner");
}

Both methods require the lock on this. When the thread executing outer() calls inner(), it already holds the lock. Because intrinsic locks are reentrant, the thread successfully enters inner() without deadlocking. Java internally tracks how many times the thread has acquired the lock and only releases it when the count drops to zero.

The Alternative Would Be Disastrous

If locks weren't reentrant, the thread would block waiting for itself to release the lock—an instant deadlock. You couldn't have synchronized methods call each other, severely limiting design flexibility.

Think of It Like a Key Card

Imagine a building where your key card opens multiple doors. Reentrancy means you can walk through Door A (which your card unlocked), then use the same card to open Door B inside, without getting locked out.

Key Takeaway:
Java's synchronized locks are reentrant by default, allowing a thread to re-acquire a lock it already holds, preventing self-deadlock when synchronized methods call each other.