模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:
例如:
由南向而来去往北向的车辆 ---- 直行车辆
由西向而来去往南向的车辆 ---- 右转车辆
由东向而来去往南向的车辆 ---- 左转车辆
。。。
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;
/**
* 路线
* @author Administrator
*/
public class Road {
private List cars = new ArrayList(); //该路线上的车辆的集合
private String name = null; //该路线的名字
public Road(String name){
this.name = name;
//模拟车辆不断随机上路的过程
ExecutorService pool = Executors.newSingleThreadExecutor();//使用线程池,产生一个线程,向集合中添加车辆
pool.execute(new Runnable(){
public void run(){
for(int i=1;i<1000;i++){
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);//每隔1~10秒之间,添加一辆车到集合中
} catch (InterruptedException e) {
e.printStackTrace();
}
cars.add(Road.this.name + "_" + i);//按路名产生车辆
}
}
});
//每隔一秒检查对应的灯是否为绿,是则放行一辆车
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1/*池中所保存的线程数*/); //创建一个线程定时器。
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
if(cars.size()>0){
boolean lighted = Lamp.valueOf(Road.this.name).isGreen();//判断当前灯是否为绿
if(lighted){
System.out.println(cars.remove(0) + " is traversing !");
}
}
}
},
1,//第一次延时1秒调用
1,//以后每次再过1秒调用
TimeUnit.SECONDS);//设置数字的单位为秒
}
}
/**
* 每个Lamp元素代表一个方向上的灯,总共有12个方向,所有总共有12个Lamp元素。
* 有如下一些方向上的灯,每两个形成一组,一组灯同时变绿或变红,所以,
* 程序代码只需要控制每组灯中的一个灯即可:
* s2n,n2s
* s2w,n2e
* e2w,w2e
* e2s,w2n
* s2e,n2w
* e2n,w2s
* 上面最后两行的灯是虚拟的,由于从南向东和从西向北、以及它们的对应方向不受红绿灯的控制,
* 所以,可以假想它们总是绿灯。
*/
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 boolean state;
//与当前灯相对应的灯
private String oppositeLamp;
//当前灯变红时下一个变绿的灯
private String nextLamp;
private Lamp(String oppositeLamp,String nextLamp,boolean state){
this.oppositeLamp = oppositeLamp;
this.nextLamp = nextLamp;
this.state = state;
}
/**
* 获取当前灯的状态
* @return 前灯的状态
*/
public boolean isGreen(){
return state;
}
/**
* 某个灯变绿时,它对应方向的灯也要变绿
*/
public void greenChange(){
this.state = true;
if(oppositeLamp != null){
Lamp.valueOf(oppositeLamp).greenChange();
}
System.out.println(name() + "变绿了,下面总共应该有6个方向能看到汽车穿过!");//便于测试观看结果
}
/**
* 某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿
* @return 下一个要变绿的灯
*/
public Lamp redChange(){
this.state = false;
if(oppositeLamp != null){
Lamp.valueOf(oppositeLamp).redChange();
}
Lamp next = null;
if(nextLamp != null){
next = Lamp.valueOf(nextLamp);
System.out.println("绿灯从" + name() + "-------->切换为" + nextLamp);
next.greenChange();
}
return next;
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 路灯管理类
* @author Administrator
*/
public class LampContorller {
private Lamp currentLamp;//初始化一个路灯
public LampContorller(){
//刚开始让由南向北的灯变绿;
currentLamp = Lamp.S2N;
currentLamp.greenChange();
//每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
public void run() {
currentLamp = currentLamp.redChange();// 改变当前路灯状态,并返回下一个路灯
}
},
2,
2,
TimeUnit.SECONDS);
}
}
/**
* 测试类
* @author Administrator
*/
public class Test {
public static void main(String[] args) {
//产生12个方向的路线
String [] directions = new String[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
for(int i=0;i