What's the meaning of an object's monitor in Java? Why use this word?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java, the concept of an "object's monitor" plays a critical role in facilitating concurrency and synchronization. Understanding this concept is essential for Java developers as they work to create systems that operate efficiently and correctly in multi-threaded environments. This article delves into what an object's monitor is, why it matters, and presents examples and detailed explanations to illuminate this fundamental aspect of Java programming.
What is an Object's Monitor?
In Java, every object has an associated monitor that provides a lock mechanism. This lock is used to ensure that only one thread can access a critical section of code at a time. This is essential to prevent concurrent threads from interfering with each other, which could lead to inconsistent or incorrect operations on shared resources.
Technical Explanation
A monitor in Java is, essentially, a synchronization construct that manages and governs access to an object's methods or block of code. Here's how it works:
- Lock Acquisition: When a thread wants to execute synchronized code (either a synchronized block or method), it must first acquire the object's monitor.
- Exclusive Access: Once a thread acquires the monitor, it has exclusive access to all synchronized sections guarded by that monitor. No other thread can execute any of the synchronized code guarded by the same monitor until the lock is released.
- Lock Release: The monitor is released either when the synchronized block or method is exited or when the thread calls `wait()` on the monitor within a synchronized block/method, temporarily releasing the monitor and suspending the thread.
Why Use the Term "Monitor"?
The word "monitor" comes from operating systems and synchronisation literature, where it's traditionally used to describe a synchronization construct that combines mutual exclusion and condition synchronization. The term may have been chosen to represent an object's lock mechanism because of its role in monitoring access to the object, enforcing the sequential execution of critical sections, and facilitating a controlled access model for shared resources.
Examples of Using Monitors
Let's look at some Java code examples that demonstrate how monitors are used to synchronize access to a shared resource:
- The `synchronized` keyword can be applied as a modifier on methods or as an expression within a block of code.
- When applied to a method, it makes the entire method a critical section, synchronized on the instance of the object for instance methods or the `Class` object for static methods.
- When used as a block, synchronization can be applied to arbitrary objects, not just `this`.
- `wait()`: Temporarily releases the monitor and puts the calling thread into a waiting state until another thread invokes `notify()` or `notifyAll()` on the same object.
- `notify()`: Wakes up one of the threads waiting on the monitor of this object.
- `notifyAll()`: Wakes up all the threads waiting on the monitor of this object.

