N2S lamp is green 下面总共可以有六个方向看到汽车穿梭
S2N lamp is green 下面总共可以有六个方向看到汽车穿梭
N2S_1 is traversing!
N2S_2 is traversing!
N2W_1 is traversing!
E2N_1 is traversing!
W2S_1 is traversing!
N2W_2 is traversing!
S2N_1 is traversing!
S2E_1 is traversing!
--来了--
绿灯从S2N-------->切换为S2W
N2E lamp is green 下面总共可以有六个方向看到汽车穿梭
S2W lamp is green 下面总共可以有六个方向看到汽车穿梭
S2W_1 is traversing!
N2E_1 is traversing!
S2E_2 is traversing!
W2S_2 is traversing!
S2W_2 is traversing!
N2E_2 is traversing!
E2N_2 is traversing!
--来了--
绿灯从S2W-------->切换为E2W
W2E lamp is green 下面总共可以有六个方向看到汽车穿梭
E2W lamp is green 下面总共可以有六个方向看到汽车穿梭
E2W_1 is traversing!
W2E_1 is traversing!
E2W_2 is traversing!
W2E_2 is traversing!
--来了--
绿灯从E2W-------->切换为E2S
W2N lamp is green 下面总共可以有六个方向看到汽车穿梭
E2S lamp is green 下面总共可以有六个方向看到汽车穿梭
E2S_1 is traversing!
W2N_1 is traversing!
E2S_2 is traversing!
W2N_2 is traversing!
--来了--
绿灯从E2S-------->切换为S2N
N2S lamp is green 下面总共可以有六个方向看到汽车穿梭
S2N lamp is green 下面总共可以有六个方向看到汽车穿梭
S2N_2 is traversing!
--来了--
绿灯从S2N-------->切换为S2W
N2E lamp is green 下面总共可以有六个方向看到汽车穿梭
S2W lamp is green 下面总共可以有六个方向看到汽车穿梭
--来了--
绿灯从S2W-------->切换为E2W
W2E lamp is green 下面总共可以有六个方向看到汽车穿梭
E2W lamp is green 下面总共可以有六个方向看到汽车穿梭
--来了--
绿灯从E2W-------->切换为E2S
W2N lamp is green 下面总共可以有六个方向看到汽车穿梭
E2S lamp is green 下面总共可以有六个方向看到汽车穿梭
--来了--
绿灯从E2S-------->切换为S2N
N2S lamp is green 下面总共可以有六个方向看到汽车穿梭
S2N lamp is green 下面总共可以有六个方向看到汽车穿梭
这样设计比较好将Road更改一下:
public class Road {
private List<String> vechicles = new ArrayList<String>();
private String name = null; private int count=0;//车辆计数器 private int SumCount=0;//存储总车辆 private Calendar calendar=new GregorianCalendar();//日历
public Road(String name) {
this.name = name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable() {
public void run() {
int i=0; while (true) { int startHour=calendar.get(Calendar.HOUR);//获得开始小时
for (i = 1; i < 100000; i++) {//间隔是一个小时 一个小时之内不可能有10万辆车上路
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} count++;
vechicles.add(Road.this.name + "_" + i);
} int endHour=calendar.get(Calendar.HOUR);//获得结束小时 int jiange=(endHour-startHour); if(jiange==2){ SumCount+=count; System.out.println(Road.this.name+"一个小时之内已经有"+vechicles+"车辆"); startHour=endHour;//将结束时间给赋给初始时间,下一秒就会变成当前系统时间小时,这只是起到一个重新计数的作用,也可以把100赋给初始时间 i=0;//计数重新开始,然后车辆的编号也开始重新命名 vechicles.removeAll(vechicles);//将车辆集合清空 }
}
}
});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
public void run() {
if (vechicles.size() > 0) {
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if (lighted) {
System.out.println(vechicles.remove(0)
+ " is traversing!");
}
}
}
}, 1, 1, TimeUnit.SECONDS);
}
}