public void run() { try { while (!Thread.interrupted()) { // Blocks until chassis is available: car = chassisQueue.take(); // Hire robots to perform work: robotPool.hire(EngineRobot.class, this); robotPool.hire(DriveTrainRobot.class, this); robotPool.hire(WheelRobot.class, this); barrier.await(); // Until the robots finish // Put car into finishingQueue for further work finishingQueue.put(car); } } catch (InterruptedException e) { print("Exiting Assembler via interrupt"); } catch (BrokenBarrierException e) { // This one we want to know about throw new RuntimeException(e); } print("Assembler off"); }
public synchronized void hire(Class<? extends Robot> robotType, Assembler d) throws InterruptedException { for (Robot r : pool) if (r.getClass().equals(robotType)) { pool.remove(r); r.assignAssembler(d); r.engage(); // Power it up to do the task return; } wait(); // None available hire(robotType, d); // Try again, recursively }