/**********************路类*************************/
package com.hans.ttaffic;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Road {
// 公路要有车 我就创造了车
List<String> vehicles = new ArrayList<String>();
private String name; // 公路要有名字
public Road(String name) {
this.name = name;
// 没隔1-10秒随机上路车
ExecutorService pool = Executors.newSingleThreadExecutor(); // 在线程池中得到一个线程
pool.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) { // 一共有999个车上路
try {
Thread.sleep((new Random().nextInt(10)+1) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
vehicles.add(Road.this.name + "_" + i + "进入道路!");
}
}
});
//没隔一秒钟通过一辆车
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); //一个计时器
timer.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
if(vehicles.size() > 0){
boolean light = Lamp.valueOf(Road.this.name).isLight();
if(light){
System.out.println(vehicles.remove(0)+"使出");
}
}
}
}, 1, 1, TimeUnit.SECONDS); //时间,1秒后执行,1秒执行一次,单位秒
}
}
/*************************灯枚举类************************/
package com.hans.ttaffic;
/**
* 灯,一共12个灯,当前灯亮起,对面的灯不亮,向右转的4个灯都一直亮,这是12个方向的灯
* S2N(南向北),S2W(南向西),E2W(东向西),E2S(东向南),
* N2S(北向南),N2E(北向东),W2E(西向东),W2S(西向南),
* N2E(南向东),E2N(东向北),N2W(北向西),W2S(西向南)
* @author Administrator
*
*/
public enum Lamp {
S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
private Lamp(String oppositeLight, String next,boolean light){
this.oppositeLight = oppositeLight; //对应的灯
this.next = next; //下一个准备亮起的灯
this.light = light; //每一个灯的初始亮不亮
}
private Lamp(){
}
//等要有灯的状态 亮 不亮
boolean light = false;
//得有对面的灯吧
private String oppositeLight;
//下一个等待亮起的灯
private String next;
//判断等是否亮
public boolean isLight(){
return light;
}
public void setLight(){ //灯变亮
this.light = true;
if(oppositeLight != null){ //对面的灯不的空的,就让对面的灯一起亮
Lamp.valueOf(oppositeLight).setLight();
}
System.out.println(name()+"变绿了,总共应该能看见6个方向的车");
}
public Lamp setBlack(){ //灯变黑
this.light = false;
if(oppositeLight != null){ //对面的灯不的空的,就让对面的灯一起变黑
Lamp.valueOf(oppositeLight).setBlack();
}
Lamp nextOpposite = null; //当你关闭一个灯,要返回下一个亮的灯 在控制器那边好调用下一个灯的变黑方法
if(next!=null){
nextOpposite = Lamp.valueOf(next);
nextOpposite.setLight();
System.out.println("绿灯由"+name()+"变为----->>>>>"+next);
}
return nextOpposite;
}
}
/*****************************灯控制器类*******************/
package com.hans.ttaffic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LampController {
private Lamp lamp;
public LampController(){
lamp = Lamp.S2N;
lamp.setLight();
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); //创建定时线程
timer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
lamp = lamp.setBlack();
}
}, 5, 5, TimeUnit.SECONDS);
}
}
/*****************************测试类***************************/
package com.hans.ttaffic;
public class LightTest {
/**
* @param args
*/
public static void main(String[] args) {
Lamp nextOpposite = Lamp.S2N;
String[] roads = new String[] { "S2N", "S2W", "E2W", "E2S", "N2S","N2E", "W2E", "W2N", "S2E", "E2N", "N2E", "E2S" }; //12条线路初始化
for (int i = 0; i < roads.length; i++) {
new Road(roads[i]);
}
new LampController();
}
}
,今天下午看的交通灯管制系统,等看完银行系统,我就准备预约面试了,加油,为了让老爸老妈老妹还有未来的一大家子过的更好 |
|