Coordination: waiting and handing over 00:00:00 Make a prediction You will distinguish a notification from a guarantee and write a waiting rule that remains correct after another consumer wins the race. Two consumers wake after one item arrives. Does receiving the wake-up mean each consumer owns an item? No. A wake-up means the condition may have changed. Each consumer must reacquire the associated lock and recheck the predicate; only the one that finds an item may remove it. 00:00:28 One afternoon on the toboggan hill A railway bell tells a crossing guard to look at the track. A bell is not permission to raise the gate without looking; a second train may already have arrived. The signal prompts rechecking. The protected condition determines whether a waiting thread can proceed, and wait must coordinate releasing and reacquiring the same associated lock. Coordination is the part of concurrent design that decides when a thread may go on. The thread waits for something that another thread does: a fact becomes true, some work ends, a group is complete, or an item arrives. Programs are split into threads that depend on each other. A printer needs pages, a server may not answer before its caches are loaded, a chapter may not be bound before all its pages exist. 00:01:16 Wait for a real queue item A consumer enters when the queue is empty. Testing once and then waiting can miss a change if checking and waiting are separate. Under the associated lock, wait while the queue remains empty. Waiting releases the lock so a producer can append an item and signal. A woken consumer reacquires the lock and checks again. Another consumer might already have taken the item, or the wake-up might be spurious. Place one item into the empty queue and wake both consumers. When the second regains the lock, the first may already have removed the item. It must loop and check again. Treating a signal as proof of ownership lets two consumers try to remove the same item. 00:01:59 Waiters need a shutdown condition During shutdown a worker may be sleeping on an empty queue. Mark the queue closed under the same coordination rules and wake the waiters. Each worker can then distinguish temporarily empty from closed and drained, rather than waiting forever. Now close an empty queue and wake every waiter. If the only predicate is nonempty, each worker goes back to sleep forever. Include closed as part of the wake condition, then distinguish closed and drained from temporarily empty in the consumer's result. Using a one-time if check can let a woken thread run on an empty queue after a faster consumer took the only item. When several tasks must all finish before opening a gate, a countdown latch can express that one-way condition more directly. Java CountDownLatch. The reference shows two latches: a start signal that holds all workers back, and a done signal that the driver waits for. It states that the count cannot be reset. Java CyclicBarrier. Uses an all-or-none breakage model: if one thread leaves early because of an interrupt, a failure or a timeout, all others at the barrier get an exception. await returns the arrival index. 00:03:14 Try an interview follow-up A wait times out at the same moment as it is notified. What should happen? The fact decides, not the clock. After waking I look at the fact first. If it is true I succeed. Otherwise the signal would be spent on a thread that leaves, and another waiter would sleep beside an available item. A worker waits for an item in a basket. Two workers wake after one item arrives. The first takes it. The second still owns the responsibility to check that the basket is nonempty before taking anything. Write the wait as a loop around the condition and test an extra wake-up. 00:03:53 What to take with you Name the fact, store it in state behind a lock, and wait for it in a loop. Change state first, then notify, with the lock held. One condition per fact lets you wake just one. A latch counts down once. A barrier counts arrivals in rounds, and each waiter keeps its own round object. A handoff has two facts, room and item, and closing it wakes everybody on both.