CarBuilder

来源:互联网 发布:二维矩阵 编辑:程序博客网 时间:2024/05/29 15:52
package mypack;public class Car {private final int id;private boolean engine=false;    private boolean driveTrain=false;    private boolean wheels=false;    public Car(int idn){    id=idn;    }    public synchronized void addEngine(){    engine=true;    }public synchronized boolean getEngine() {return engine;}    public synchronized void addDriveTrain(){    driveTrain=true;    }public synchronized boolean getDriveTrain() {return driveTrain;}    public synchronized void addWheels(){    wheels=true;    }public synchronized boolean getWheels() {return wheels;}@Overridepublic String toString() {return "Car [id=" + id + ", engine=" + engine + ", driverTrain=" + driveTrain + ", wheels=" + wheels + "]";}    }


package mypack;import java.util.concurrent.LinkedBlockingQueue;public class CarQueue extends LinkedBlockingQueue<Car>{}


package mypack;import java.util.concurrent.TimeUnit;public class ChassisBuilder implements Runnable{private CarQueue carQueue;private int count=0;public ChassisBuilder(CarQueue queue){carQueue=queue;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted()){TimeUnit.MILLISECONDS.sleep(500);Car car = new Car(count++);System.out.println("ChassisBuilder has created "+car);carQueue.put(car);}}catch(InterruptedException e){System.out.println("ChassisBuilder interrupt");}System.out.println("ChassisBuilder finished");}    }


package mypack;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.TimeUnit;public class Assembler implements Runnable{private Car car;private CarQueue chassisQueue;private CarQueue finishQueue;private RobotPool robotPool;CyclicBarrier barrier=new CyclicBarrier(4);public Assembler(CarQueue chassisQueue, CarQueue finishQueue, RobotPool pool) {this.chassisQueue = chassisQueue;this.finishQueue = finishQueue;this.robotPool = pool;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted()){   car = chassisQueue.take();   robotPool.hire(EngineRobot.class, this);   robotPool.hire(DriveTrainRobot.class, this);   robotPool.hire(WheelRobot.class, this);   barrier.await();   finishQueue.put(car);}}catch(InterruptedException | BrokenBarrierException e){System.out.println("Assembler interruption");}System.out.println("Assemble finished");}public Car car() {// TODO Auto-generated method stubreturn car;}}


package mypack;public class Reporter implements Runnable{private CarQueue finishQueue;public Reporter(CarQueue cq){finishQueue=cq;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted()){Car car = finishQueue.take();System.out.println(car);}}catch(InterruptedException e){System.out.println("Reporter interruption");}System.out.println("Report Finished");}}

package mypack;import java.util.concurrent.BrokenBarrierException;public abstract class Robot implements Runnable{private RobotPool pool;protected Assembler assembler;private boolean enage=false;public Robot(RobotPool p){pool=p;}public Robot assignAssembler(Assembler d) {// TODO Auto-generated method stubassembler=d;return this;}public abstract void performService();    public synchronized void engage(){    enage=true;    notifyAll();    }    @Override    public void run() {    // TODO Auto-generated method stub    try{     powDown();     while(!Thread.interrupted()){    performService();     assembler.barrier.await();     powDown();     }    }catch(InterruptedException | BrokenBarrierException e){    System.out.println("Robot Run inperrution");    }    System.out.println(this+" off");    }    public synchronized void powDown() throws InterruptedException{    enage=false;    assembler=null;    pool.Release(this);    while(enage==false)    wait();    }    @Override    public String toString() {    // TODO Auto-generated method stub    return getClass().getName();    }}


package mypack;public class EngineRobot extends Robot {public EngineRobot(RobotPool p){super(p);}@Overridepublic void performService() {// TODO Auto-generated method stubSystem.out.println(this+" installing engine");assembler.car().addEngine();}}


package mypack;public class DriveTrainRobot extends Robot{public DriveTrainRobot(RobotPool p) {super(p);// TODO Auto-generated constructor stub}@Overridepublic void performService() {// TODO Auto-generated method stubSystem.out.println(this+" installing driveTrain");assembler.car().addDriveTrain();}   }

package mypack;public class WheelRobot extends Robot{public WheelRobot(RobotPool p) {super(p);// TODO Auto-generated constructor stub}@Overridepublic void performService() {// TODO Auto-generated method stubSystem.out.println(this+" installing Wheels");assembler.car().addWheels();}}

package mypack;import java.util.*;public class RobotPool {private Set<Robot> pool=new HashSet<Robot>();public synchronized void add(Robot r){pool.add(r);notifyAll();}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();return;}wait();//应对非对应的notifyAll()hire(robotType,d);}public synchronized void Release(Robot r){add(r);}}

package mypack;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;public class CatBuilder {public static void main(String[] args) {// TODO Auto-generated method stubCarQueue chassisQueue=new CarQueue();CarQueue finishQueue=new CarQueue();RobotPool pool = new RobotPool();ExecutorService exec = Executors.newCachedThreadPool();exec.execute(new EngineRobot(pool));exec.execute(new DriveTrainRobot(pool));exec.execute(new WheelRobot(pool));exec.execute(new Assembler(chassisQueue, finishQueue, pool));exec.execute(new Reporter(finishQueue));exec.execute(new ChassisBuilder(chassisQueue));try {TimeUnit.SECONDS.sleep(7);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}exec.shutdownNow();}}


0 0
原创粉丝点击