欢迎来到天天文库
浏览记录
ID:16478826
大小:44.50 KB
页数:6页
时间:2018-08-10
《java 交通灯管理系统源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagecom.isoftstone.interview.traffic;importjava.util.ArrayList;importjava.util.List;importjava.util.Random;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importja
2、va.util.concurrent.TimeUnit;/***每个Road对象代表一条路线,总共有12条路线,即系统中总共要产生12个Road实例对象。*每条路线上随机增加新的车辆,增加到一个集合中保存。*每条路线每隔一秒都会检查控制本路线的灯是否为绿,是则将本路线保存车的集合中的第一辆车移除,即表示车穿过了路口。*@author张孝祥www.it315.org**/publicclassRoad{privateListvechicles=newArrayList3、>();privateStringname=null;publicRoad(Stringname){this.name=name;//模拟车辆不断随机上路的过程ExecutorServicepool=Executors.newSingleThreadExecutor();pool.execute(newRunnable(){publicvoidrun(){for(inti=1;i<1000;i++){try{Thread.sleep((newRandom().nextInt(10)+1)*1004、0);}catch(InterruptedExceptione){e.printStackTrace();}vechicles.add(Road.this.name+"_"+i);}}});//每隔一秒检查对应的灯是否为绿,是则放行一辆车ScheduledExecutorServicetimer=Executors.newScheduledThreadPool(1);timer.scheduleAtFixedRate(newRunnable(){publicvoidrun(){if(vechic5、les.size()>0){booleanlighted=Lamp.valueOf(Road.this.name).isLighted();if(lighted){System.out.println(vechicles.remove(0)+"istraversing!");}}}},1,1,TimeUnit.SECONDS);}}packagecom.isoftstone.interview.traffic;publicclassMainClass{/***@paramargs*/public6、staticvoidmain(String[]args){/*产生12个方向的路线*/String[]directions=newString[]{"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};for(inti=0;i7、om.isoftstone.interview.traffic;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;publicclassLampController{privateLampcurrentLamp;publicLampController(){//刚开始让由南向北的灯变绿;curren8、tLamp=Lamp.S2N;currentLamp.light();/*每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿*/ScheduledExecutorServicetimer=Executors.newScheduledThreadPool(1);timer.scheduleAtFixedRate(newRunnable(){publicvoidrun(){System.out.println("来啊");currentLamp=currentLamp.blackOut();}},
3、>();privateStringname=null;publicRoad(Stringname){this.name=name;//模拟车辆不断随机上路的过程ExecutorServicepool=Executors.newSingleThreadExecutor();pool.execute(newRunnable(){publicvoidrun(){for(inti=1;i<1000;i++){try{Thread.sleep((newRandom().nextInt(10)+1)*100
4、0);}catch(InterruptedExceptione){e.printStackTrace();}vechicles.add(Road.this.name+"_"+i);}}});//每隔一秒检查对应的灯是否为绿,是则放行一辆车ScheduledExecutorServicetimer=Executors.newScheduledThreadPool(1);timer.scheduleAtFixedRate(newRunnable(){publicvoidrun(){if(vechic
5、les.size()>0){booleanlighted=Lamp.valueOf(Road.this.name).isLighted();if(lighted){System.out.println(vechicles.remove(0)+"istraversing!");}}}},1,1,TimeUnit.SECONDS);}}packagecom.isoftstone.interview.traffic;publicclassMainClass{/***@paramargs*/public
6、staticvoidmain(String[]args){/*产生12个方向的路线*/String[]directions=newString[]{"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};for(inti=0;i7、om.isoftstone.interview.traffic;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;publicclassLampController{privateLampcurrentLamp;publicLampController(){//刚开始让由南向北的灯变绿;curren8、tLamp=Lamp.S2N;currentLamp.light();/*每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿*/ScheduledExecutorServicetimer=Executors.newScheduledThreadPool(1);timer.scheduleAtFixedRate(newRunnable(){publicvoidrun(){System.out.println("来啊");currentLamp=currentLamp.blackOut();}},
7、om.isoftstone.interview.traffic;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;publicclassLampController{privateLampcurrentLamp;publicLampController(){//刚开始让由南向北的灯变绿;curren
8、tLamp=Lamp.S2N;currentLamp.light();/*每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿*/ScheduledExecutorServicetimer=Executors.newScheduledThreadPool(1);timer.scheduleAtFixedRate(newRunnable(){publicvoidrun(){System.out.println("来啊");currentLamp=currentLamp.blackOut();}},
此文档下载收益归作者所有