← Lessons

quiz vs the machine

Gold1500

Concurrency

Monitors and Intrinsic Locks

Bundling mutual exclusion and condition waiting into one construct.

5 min read · core · beat Gold to climb

Monitors and Intrinsic Locks

A monitor is a synchronization construct that bundles together mutual exclusion and the ability to wait for conditions. Every object can act as a monitor with an intrinsic lock, sometimes called a built in lock or mutex, that guards its synchronized methods and blocks.

The monitor guarantees that only one thread executes inside the protected region at a time. When a thread enters a synchronized method it implicitly acquires the object intrinsic lock, and it releases that lock on exit, even if an exception is thrown. This automatic acquire and release is why monitors are easier to use correctly than manual locks.

Monitors also provide condition waiting through wait and notify operations tied to the same lock. A thread inside the monitor can wait, which releases the lock and suspends, until another thread inside the monitor signals.

Important properties:

  • The intrinsic lock is reentrant, so a thread already holding it can enter another synchronized method on the same object without deadlocking itself
  • Locking is per object, so different instances do not block each other
  • Static synchronized methods lock on the class rather than an instance

The classic monitor model packages data, the lock that protects it, and the conditions threads wait on into a single coherent unit. This is a higher level abstraction than raw locks and condition variables, though under the hood it uses exactly those pieces.

Key idea

A monitor combines an object intrinsic reentrant lock with condition waiting, automatically acquiring and releasing the lock so only one thread runs in the protected region at a time.

Check yourself

Answer to earn rating on the learn ladder.

1. What does it mean that an intrinsic lock is reentrant?

2. What does a monitor bundle together?