4.5.3 Locks in Java

Aim
Introduce locking on objects as a programming concept
Objective
Enable the students to write simple critical sections
Exercise
A number of students share a house. If they look in the fridge and there is no milk, then the student goes to the shop to buy milk. How should a runnable Student class use the Fridge class below to ensure that only one student ever goes to buy milk if there is an empty fridge?
public class Fridge {
   Milk getMilk() throws NoMilkException;
   boolean isMilk();
   void putMilk(Milk carton) throws TooMuchMilkException;
   void putNote(Note postit);
   boolean isNote();
   Note removeNote();
}
Possible solution is in milk.zip.

Ian Wakeman 2005-02-22