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

Why Non-Reentrant Locks Would Deadlock

Understanding that without reentrancy, a thread calling a synchronized method from another synchronized method would deadlock itself.

Why Non-Reentrant Locks Would Deadlock

What You'll Learn

You'll understand why Java's intrinsic locks must be reentrant—without reentrancy, even simple method calls would cause threads to deadlock themselves.

The Deadlock Scenario

Imagine if Java's synchronized locks were non-reentrant: once a thread acquired a lock, it couldn't acquire the same lock again, even though it already owns it.

This would be catastrophic. Consider this common pattern:

public synchronized void outer() {
    System.out.println("In outer");
    inner(); // Calling another synchronized method
}

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

What Would Happen Without Reentrancy

  1. Thread A calls outer() and acquires the object's lock
  2. Inside outer(), Thread A calls inner()
  3. inner() is also synchronized on the same object
  4. Thread A tries to acquire the lock... but it already holds it
  5. With a non-reentrant lock, Thread A would block waiting for itself to release the lock
  6. Deadlock! Thread A is waiting for Thread A to release the lock that Thread A holds

The Analogy

Imagine a room with a door that locks from inside. You enter and lock the door. Then you try to open an interior closet door—but it requires the same key you used for the main door.

With a non-reentrant lock, you'd be stuck: "I can't enter the closet because I'm already using the key, even though I'm the one using it!"

Why Reentrancy Saves Us

Because Java's intrinsic locks are reentrant, the thread recognizes "I already own this lock" and proceeds immediately. The lock tracks how many times the thread has entered, releasing only when the count returns to zero.

Key Takeaway: Without reentrancy, synchronized methods calling other synchronized methods on the same object would immediately deadlock themselves—making the entire synchronization mechanism nearly useless.