Skip to content

Monitor

Category: Synchronization · Status: stub · Lessons: chapter 04, Waiting for each other (planned)

One line: An object whose methods all run under one built-in lock, with condition variables for waiting inside it — Java's synchronized with wait and notify.

Also called: synchronized, intrinsic lock.

How it connects

flowchart LR
  n_condition_variable["Condition variable"]
  n_monitor["Monitor"]
  n_mutex["Mutex"]
  n_synchronization["Synchronization"]
  n_monitor -->|is a| n_synchronization
  n_monitor -->|uses| n_condition_variable
  n_monitor -->|uses| n_mutex
  classDef center stroke-width:3px
  class n_monitor center
  classDef outside stroke-dasharray: 4 3
  class n_condition_variable,n_mutex,n_synchronization outside

In each language

Rust No monitor type: a Mutex<T> holds the data and a Condvar waits on its guard
Go No monitor: a sync.Cond carries a Locker that must be held when the condition changes and when calling Wait
Java Every object has one: JLS §17.1 ↗ synchronized locks it, and Object.wait, notify and notifyAll wait inside it
Python threading.Condition bundles a lock with wait and notify
C# Monitor: Enter and Exit, and Wait, Pulse and PulseAll inside; the lock statement uses it
Kotlin @Synchronized marks the generated JVM method as synchronized

Where to read more