import java.awt.*; /** * SupplyRow starts with fixed list of Box's and gives them away, * is not a process */ public class SupplyRow extends Row { public SupplyRow (Rectangle loc, Canvas canvas) { super (loc, canvas); // Our initial set of boxes for (int i=0; i<22; i++) { boxes.add (new Box ()); } } /** * Give away one of our boxes * Need a single, "synchronized" routine * that reports whether we have any to give * AND takes it ("test and set") * So this routine tries to give a box, * returns the Box if succeeds, or null if fails */ public synchronized Box take() { if (boxes.size()>0) { Box ans = boxes.remove (0); canvas.repaint (); return ans; } else return null; } }