class Car{ private final int id; private boolean engine = false, driveTrain = false, wheels = false; public Car(int idn){ id = idn; } public Car(){ id = -1; } public synchronized int getId(){ return id; } public synchronized void addEngine(){ engine = true; } public synchronized void addDriveTrain(){ driveTrain = true; } public synchronized void addWheels(){ wheels = true; } public synchronized String toString(){ return "Car " +id +"[ engine: " + engine + " driveTrain: " + driveTrain + " wheels: "+ wheels+ " ]"; } } class CarQueue extends LinkedBlockingQueue<Car>{} class ChassisBuilder implements Runnable{ private CarQueue carQueue; private int counter = 0; public ChassisBuilder(CarQueue cq){ carQueue =cq; } public void run(){ try { while(!Thread.interrupted()){ TimeUnit.MILLISECONDS.sleep(500); Car c = new Car(counter++); System.out.println("ChassisBuilder created " +c); // Insert into queue carQueue.put(c); } } catch (InterruptedException e) { System.out.println("Interrupted: ChassisBuilder"); } System.out.println("ChassisBuilder off"); } } class Assembler implements Runnable{ private CarQueue chassisQueue, finishingQueue; private Car car; private CyclicBarrier barrier = new CyclicBarrier(4); private RobotPool robotPool; public Assembler(CarQueue cq, CarQueue fq, RobotPool rp){ chassisQueue = cq; finishingQueue = fq; robotPool = rp; } public Car car(){ return car; } public CyclicBarrier barrier(){ return barrier; } 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(); // Unitl the robots finish // Put car into finishedQueue for futher work finishingQueue.put(car); } } catch (InterruptedException e) { System.out.println("Exiting Assembler via interrupt"); } catch (BrokenBarrierException e) { // This one we want to know about throw new RuntimeException(e); } System.out.println("Assembler off"); } } class Reporter implements Runnable{ private CarQueue carQueue; public Reporter(CarQueue cq){ carQueue = cq; } public void run(){ try { while(!Thread.interrupted()){ System.out.println(carQueue.take()); System.out.println(); } } catch (InterruptedException e) { System.out.println("Exiting Reporter via interrupt"); } System.out.println("Reporter off"); } } abstract class Robot implements Runnable{ private RobotPool pool; public Robot(RobotPool p){ pool = p; } protected Assembler assembler; public Robot assignAssembler(Assembler assembler){ this.assembler = assembler; return this; } private boolean engage = false; public synchronized void engage(){ engage = true; notifyAll(); } // The part of run() that's different for each robot; abstract protected void performService(); public void run(){ try { powerDown(); // Wait unitll needed while(!Thread.interrupted()){ performService(); assembler.barrier().await(); // Synchronized // We're done with that job... powerDown(); } } catch (InterruptedException e) { System.out.println("Exiting "+ this+ " via interrupt"); } catch (BrokenBarrierException e) { // This one we want to know about throw new RuntimeException(e); } System.out.println(this +" off"); } private synchronized void powerDown() throws InterruptedException{ engage = false; assembler = null; // Disconnet from the Assembler; // Put ourserlves back in the available pool; pool.release(this); while(engage == false) // Power down wait(); } @Override public String toString() { return getClass().getName(); } } class EngineRobot extends Robot{ public EngineRobot(RobotPool pool){ super(pool); } protected void performService(){ System.out.println(this +" installing engine"); assembler.car().addEngine(); } } class DriveTrainRobot extends Robot{ public DriveTrainRobot(RobotPool pool){ super(pool); } protected void performService(){ System.out.println(this+" installing DriveTrain"); assembler.car().addDriveTrain(); } } class WheelRobot extends Robot{ public WheelRobot(RobotPool pool){ super(pool); } protected void performService(){ System.out.println(this + " installing Wheels"); assembler.car().addWheels(); } } class RobotPool{ // Quietly prevents identical entries; 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(); // Power it up to do the task return; } wait(); // None available hire(robotType, d); // Try again, recursively } public synchronized void release(Robot r){ add(r); } } public class CarBuilder { public static void main(String[] args) throws InterruptedException { CarQueue chassisQueue = new CarQueue(), finishingQueue = new CarQueue(); ExecutorService exec = Executors.newCachedThreadPool(); RobotPool robotPool = new RobotPool(); exec.execute(new EngineRobot(robotPool)); exec.execute(new DriveTrainRobot(robotPool)); exec.execute(new WheelRobot(robotPool)); exec.execute(new Assembler(chassisQueue, finishingQueue, robotPool)); exec.execute(new Reporter(finishingQueue)); // Start everything running by producing chassis; exec.execute(new ChassisBuilder(chassisQueue)); TimeUnit.SECONDS.sleep(7); exec.shutdownNow(); } }Car 是经由CarQueue从一个地方传送到另一个地方的, CarQueue是一种LinkedBlockingQueue类型。 ChassisBuilder创建了一个未加修饰的Car, 并把它放到了一个CarQueue中。 Assembler从一个CarQueue中取走Car, 并雇请Robot对其加工。 CyclicBarrier使Assembler等待。直至所有的Robot都完成, 并且在那一时刻它会将Car放置到将离开CarQueue中,然后被传送 到下一个操作。最总的CarQueue的消费者是一个Report对象, 它只打印Car, 以显示所有的任务都已经正确的完成了。 Car将其所有方法都设置成了synchronized的。 正如它所表现出来的那样, 在本例中,这是多余的, 因为在工程的内部, Car是通过队列一动的, 并且在任何时, 只有一个任务能够在某辆车上工作。 基本上,队列可以强制串行化地访问Car。但是这正是你可能会落入的陷阱-- 你可能会说“让我们尝试着通过不对Car类同步来进行优化, 因为看起来 Car在这里并不需要同步。 但是当这个系统连接到另一个需要Car被同步 的系统时,它就会崩溃。 // 即Car可能会被多个线程使用,因此我们需要以明显的方式使其成为线程安全的
发表评论
-
线程例子[Condition, SignlAll, await]
2012-02-09 22:05 1018package concurrency.waxomati ... -
生产者消费者[简单示例]
2012-02-09 21:38 650class Meal{ private final ... -
泛型[创建类型实例]
2012-02-06 01:01 725public Class Erased<T> ... -
多路分发3
2012-02-05 23:16 811使用常量相关的方法 常量相关的方法允许我们为每个 ... -
多路分发2[使用enum分发]
2012-02-05 18:09 1893直接将RoShamBo1.java翻译为基于enum的版 ... -
多路分发
2012-02-05 02:06 1368多路分发 Number.plus(Number) ... -
使用Enum的自动贩卖机
2012-02-04 13:52 908public enum Input { NICKEL ... -
使用enum的责任链
2012-02-04 12:39 1166package enumerated; impor ... -
枚举类Enum,EnumSet,EnumMap
2012-02-04 02:00 1406EnumSet与HashSet相比,非常快。 p ... -
枚举类enum
2012-02-03 15:00 895一般来说,我们希望每个美剧实例能够返回对自身的描述, ... -
类的简化历[从内部类到匿名内部类]
2012-02-02 12:41 626Version1: public class DirL ... -
持有引用java.lang.ref 和 WeakHashMap
2012-02-02 01:27 972Thinking in java P554 持 ... -
Collections快速报错 fial-fast
2012-02-02 00:46 774{ public static void main( ... -
散列HashCode
2012-02-01 14:04 693散列的价值在于速度: 散列使得查询快速,由于瓶颈位于键 ... -
使用散列数据结构注意点
2012-02-01 01:32 581Goundhog自动继承基类Object,所以这里使用 ... -
Set注意点
2012-02-01 01:31 294如果我们尝试着将没有恰当支持不许的操作的类型用于需要这些 ... -
享元Map
2012-01-30 00:28 374这里之所以叫享元,注意static关键字。 定制了Entry ... -
深入容器Map
2012-01-19 15:12 391Map生成器 对Map的使用相同的方式,需要一 ... -
深入容器List
2012-01-19 14:40 339一种Generator解决方案: 所有Collect ... -
泛型数组
2012-01-19 00:57 632不能创建泛型数组,但能通过转型来赋予 public cl ...
相关推荐
基于SLP法的汽车制造厂布置设计及仿真模拟课程设计 本课程设计的主要目的是应用SLP法(Systematic Layout Planning)对汽车制造厂的布置设计进行分析和仿真模拟。SLP法是一种系统化的布置设计方法,旨在通过分析...
中国汽车仿真技术行业正在经历一场由数字化技术引领的改革,这一改革对汽车制造产业产生了深远影响。仿真技术,尤其是计算机辅助工程(CAE)在汽车研发领域的重要性日益凸显,它不仅提高了设计效率,降低了研发成本...
汽车性能仿真计算实验实验报告.pdf汽车性能仿真计算实验实验报告.pdf汽车性能仿真计算实验实验报告.pdf汽车性能仿真计算实验实验报告.pdf汽车性能仿真计算实验实验报告.pdf汽车性能仿真计算实验实验报告.pdf汽车性能...
仿真可以帮助工程师在实际制造之前发现潜在问题,减少实物原型测试的成本和时间。 接下来,涉及的是“汽车尾灯原理图”。原理图是电路设计的核心,它直观地展示了各个电子元件的连接方式,包括电阻、电容、二极管、...
ADVISOR 2002是一款专为电动汽车(EV)设计的高级仿真工具,由美国能源部(DOE)的国家 Renewable Energy Laboratory (NREL) 开发。该软件的主要目标是帮助研究人员、工程师以及学生理解和优化电动汽车的动力系统...
汽车制造涂装-总装缓存调序区调度优化建模的matlab仿真源码.zip 已获导师指导并通过的97分的高分课程设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 汽车制造涂装-总装缓存调序...
20210630-头豹研究院-2021年中国汽车仿真技术行业概览:数字化技术为汽车制造带来改革.pdf
通过对驾驶舱乘员脚部平均温度的仿真和实测对比,证明了AMESim能准确预测车辆冷却能力,为汽车制造商提供了一个有效的工具来评估空调系统的整体性能。 总之,AMESim仿真技术在汽车空调制冷系统中的应用显著提高了...
2021年中国汽车仿真技术行业概览:数字化技术为汽车制造带来改革
增程式电动汽车动力系统参数匹配仿真与...本文对增程式电动汽车动力系统参数匹配进行了仿真和分析,为增程式电动汽车的设计和制造提供了重要的理论依据和技术支持,对于推动新能源汽车技术的发展和应用具有重要的意义。
在当前数字化转型的大潮中,云环境汽车制造业供应链服务发现与协同机制的仿真平台扮演着至关重要的角色。这个平台旨在优化整个供应链流程,提高效率,降低成本,并促进多方合作。以下是对这一主题的深入探讨: 一、...
1. **数字化设计**:UG软件提供了强大的三维建模功能,使得汽车制造商能够快速创建复杂的产品模型,并进行仿真分析,以确保设计的可行性和优化性能。这大大减少了物理原型的制作和测试,缩短了产品开发周期。 2. **...
在IT行业中,曲面制造仿真是一项关键的技术,特别是在CAD(计算机辅助设计)和CAM(计算机辅助制造)领域。这项技术允许工程师和设计师模拟并优化复杂曲面的加工过程,确保最终产品的精度和质量。以下是对曲面制造...
对于汽车制造业,MSC软件同样解决了关键挑战,如缩短产品上市时间、提高生产率。在白车身虚拟装配中,软件可以进行装配评估、间隙模拟、冲压模具设计优化,减少迭代次数,以及通过机上和机外的探測来改善冲压部件的...
汽车整体动力性MATLAB仿真计算 本文讨论了汽车整体动力性仿真计算的MATLAB实现。汽车动力性是指汽车在良好路面上直线行驶时由汽车所受到的纵向外力决定的、所能达到的...这些结果对汽车设计和制造具有重要的参考意义。
基于SLP法的汽车制造厂布置设计及仿真模拟.pdf
数字化仿真技术是汽车数字化技术的核心部分,通过使用计算机辅助设计(CAD)、计算机辅助工程(CAE)和计算机辅助制造(CAM)等技术,汽车企业可以快速设计、测试和制造汽车产品,从而缩短产品开发周期,降低成本,...