Search⌘ K
AI Features

Java's Monitor & Hoare vs Mesa Monitors

Understand Java's implementation of monitors including the use of wait and notify within synchronized blocks. Explore the difference between Hoare and Mesa monitors, why Java uses Mesa semantics, and the importance of checking conditions in a while loop to avoid synchronization errors.

We discussed the abstract concept of a monitor in the previous section and now let's see the working of a concrete implementation of it in Java.

Java’s Monitor

In Java every object is a condition variable and has an associated lock that is hidden from the developer. Each java object exposes wait() and notify() methods.

Before we execute wait() on a java object we need to lock its hidden mutex. That is done implicitly through the synchronized keyword. If you attempt to call wait() or notify() outside of a synchronized block, an IllegalMonitorStateException would occur. It's ...