public Widget waitForWidget()...{ synchronized(finishedWidgets)...{ if(finishedWidgets.size()==0)...{ try...{ finishedWidgets.wait(); } catch(InterruptedException e) ...{} } return finishedWidgets.remove(0); } } } public class WidgetUser extends Thread...{ private WidgetMaker maker; public WidgetUser(String name,WidgetMaker maker)...{ super(name); this.maker=maker; } public void run()...{ Widget w=maker.waitForWidget(); System.out.println(getName()+"got a widget"); }
public static void main(String[] args) ...{ WidgetMaker maker=new WidgetMaker(); maker.start(); new WidgetUser("Lenny",maker).start(); new WidgetUser("Moe",maker).start(); new WidgetUser("Curly",maker).start();