Intrinsic Lock (Monitor Lock)

About

In Java, every object has an associated intrinsic lock (also known as a monitor lock), which is used for synchronization. The Java Virtual Machine (JVM) automatically associates a lock with every object to coordinate access among multiple threads.

When a thread enters a synchronized block or method, it must acquire the intrinsic lock of the associated object. Once acquired, other threads attempting to enter any synchronized section on the same object are blocked until the lock is released.

The intrinsic lock is also used in inter-thread communication via wait(), notify(), and notifyAll().

  • wait(): Releases the lock and puts the thread in a waiting state.

  • notify(): Wakes up one waiting thread.

  • notifyAll(): Wakes up all waiting threads.

Intrinsic Lock Acquisition Process

  1. Thread Tries to Enter a Synchronized Block or Method

    • If no other thread holds the object's lock, the thread acquires the lock and proceeds.

    • If another thread holds the lock, the current thread waits until the lock is released.

  2. Thread Executes the Critical Section

    • The thread performs operations inside the synchronized block/method while holding the lock.

  3. Thread Releases the Lock

    • After the synchronized block/method completes execution, the thread releases the lock, allowing other waiting threads to proceed.

Types of Intrinsic Lock Usage in Java

1. Synchronized Methods and Intrinsic Lock

When a method is declared as synchronized, the intrinsic lock of the object (this) is used.

Example: Synchronization Using Intrinsic Lock on an Object

2. Synchronized Blocks and Intrinsic Lock

Instead of locking an entire method, synchronized blocks allow locking of specific code sections, improving efficiency.

Example: Synchronization Using an Intrinsic Lock on a Specific Object

3. Class-Level Synchronization and Intrinsic Lock on Class<T>

When a static method is synchronized, the intrinsic lock is on the Class object (Class<T>), ensuring that only one thread can execute any synchronized static method at a time.

Example: Synchronization Using Class-Level Lock

Is the Intrinsic Lock Implicit or Explicit?

The intrinsic lock (monitor lock) in Java is an implicit lock, meaning:

  • It is automatically associated with every Java object.

  • It does not require explicit creation—it is acquired and released automatically when using synchronized.

  • The JVM manages it internally, unlike ReentrantLock, which must be explicitly created and controlled.

Intrinsic Lock = Implicit Lock

When a thread enters a synchronized method or block, it implicitly acquires the intrinsic lock of the associated object without needing manual intervention.

Example: Implicit Lock Using synchronized

Why Is It Implicit?

  • The lock is automatically acquired when synchronized is used.

  • The lock is automatically released when the synchronized method/block completes execution.

  • No explicit lock handling (e.g., lock.lock() and lock.unlock()) is required.

Explicit Locks: How They Differ from Intrinsic Locks

Unlike intrinsic locks, explicit locks like ReentrantLock must be manually controlled.

Example: Explicit Lock Using ReentrantLock

Equivalent Explicit Lock Code for Intrinsic Lock

1.1. Intrinsic Lock (Implicit) – Using synchronized

1.2. Equivalent Explicit Lock – Using ReentrantLock

2.1. Intrinsic Lock – Using synchronized Block

2.2. Equivalent Explicit Lock – Using ReentrantLock in a Block

Last updated