Monitor 00:00:00 Make a prediction You will encapsulate the condition, the state and its lock in one object, then trace a waiter that wakes but must wait again. A waiter needs five litres. The bell rings when the tub has six, but another waiter takes three before the first re-enters. May the first take five? No. Only three remain. The first must check the predicate again under the monitor and wait until the amount is sufficient. A notification is not a reservation. 00:00:31 The paint room At the water hut one person at a time may inspect and change the tub. A bell invites somebody back from the waiting mat. They still need to look at the water level with the door closed behind them. The monitor owns the tub, the exclusion rule, and the conditions for waiting. Splitting them across unrelated locks loses the reasoning that makes the check safe. A monitor is an object whose methods exclude each other. Its data is private, each method takes the same lock, and a method that cannot go on waits on a condition variable, which lets others in meanwhile. A lock alone can protect a change, but it cannot wait for one. A thread that finds a buffer empty must let go of the lock, sleep, and be woken. Doing this by hand leaves a gap in which a wake-up gets lost. 00:01:21 Guard a one-item mailbox A mailbox has room for one letter. A monitor owns the slot, the synchronization and the conditions for waiting. A sender waits while the slot is full and writes while holding the monitor. A receiver waits while it is empty and removes the letter under the same protection. Notify the relevant waiters after changing the slot. The receiver checks its condition after waking because the signal describes a change, not ownership of the letter. Put one message into the mailbox and wake the waiting consumers. The first takes it and clears the slot under the monitor lock. The second must see the empty slot and wait. Releasing the lock between inspecting and clearing the slot could deliver that one message twice. 00:02:04 Do not hold the monitor while delivering Copy and remove a letter under the mailbox monitor, then release it before contacting a slow delivery service. That lets another sender fill the slot. Decide separately what to do if delivery fails, since synchronization alone does not provide retry semantics. After removing a message, let the remote delivery stall. A producer should still be able to place the next message into the now-empty mailbox. Decide separately whether a failed delivery is retried; holding the monitor for the network call only stalls otherwise independent work. Running a slow external delivery while holding the monitor can block every sender even after the internal slot is free. Move the slow work after the protected state change and define recovery separately; mutual exclusion does not itself guarantee delivery. Java objects. The Java Language Specification says that each object is associated with a monitor, and defines wait sets and notification. Object.wait documents that waiting should happen in a loop because of spurious wakeups. Java Condition. java.util.concurrent.locks.Condition gives one lock several wait sets. Its documentation advises to always assume that spurious wakeups can occur. 00:03:21 Try an interview follow-up What is a spurious wakeup and why is it allowed? wait returns without a signal. The standards allow it because it makes waiting cheaper to implement, and correct code looks at the condition again anyway. A sender sets the ready flag and signals before a new receiver starts to wait. Must the receiver wait for another signal? The flag is the remembered fact. Under the monitor lock, read it before deciding to wait. If ready is already true, proceed immediately. Waiting unconditionally for an additional signal can sleep forever although the desired condition is present. 00:04:02 What to take with you A monitor is data, lock and conditions in one object. Methods enter the lock, wait in a while loop, change the state so that the rule holds again, signal, and leave. wait leaves the lock and sleeps in one step. A signal is a hint: barging, broadcast and spurious wakeups can make it false. Use one condition per reason, and broadcast when in doubt.