【Road类】:
每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
public class Road {
privateArrayList<String> vehicles = new ArrayList<String>();
private String name;
public Road(Stringname){
this.name = name;
ExecutorServicepool = Executors.newSingleThreadExecutor();
pool.execute(newRunnable(){
public voidrun(){
for(inti=1;i<1000;i++){
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(
newRunnable(){
public void run(){
if(vehicles.size()>0){
booleanstatus = Lamp.valueOf(Road.this.name).getStatus();
if(status){
System.out.println(vehicles.remove(0) + " is traversing !");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
【Lamp类】:
系统中有12个方向上的灯,在程序的其他地方要根据灯的名称就可以获得对应的灯的实例对象,综合这些因素,将Lamp类用java5中的枚举形式定义更为简单。
每个Lamp对象中的亮黑状态用lighted变量表示,选用S2N、S2W、E2W、E2N这四个方向上的Lamp对象依次轮询变亮,Lamp对象中还要有一个oppositeLampName变量来表示它们相反方向的灯,再用一个nextLampName变量来表示此灯变亮后的下一个变亮的灯。这三个变量用构造方法的形式进行赋值,因为枚举元素必须在定义之后引用,所以无法再构造方法中彼此相互引用,所以,相反方向和下一个方向的灯用字符串形式表示。
增加让Lamp变亮和变黑的方法:light和blackOut,对于S2N、S2W、E2W、E2N这四个方向上的Lamp对象,这两个方法内部要让相反方向的灯随之变亮和变黑,blackOut方法还要让下一个灯变亮。
除了S2N、S2W、E2W、E2N这四个方向上的Lamp对象之外,其他方向上的Lamp对象的nextLampName和oppositeLampName属性设置为null即可,并且S2N、S2W、E2W、E2N这四个方向上的Lamp对象的nextLampName和oppositeLampName属性必须设置为null,以便防止light和blackOut进入死循环。
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(Stringcomposite,String nextLamp,boolean status){
this.opposite =composite;
this.nextLamp =nextLamp;
this.status =status;
}
public booleangetStatus(){
return status;
}
public voidsetStatus(boolean status){
this.status =status;
}
public LampgetNextLamp(){
return Lamp.valueOf(this.nextLamp);
}
public voidturnStatus(){
if (getStatus()) {
setStatus(false);
System.out.println(this.name()+"turns from green to red");
if (opposite!= null) {//一定要判断,否则进入无限递归
Lamp.valueOf(opposite).turnStatus();
}
if(nextLamp!= null){ //一定要判断,否则进入无限递归
Lamp.valueOf(nextLamp).turnStatus();
}
}else{
setStatus(true);
System.out.println(this.name()+"turns from red to green");
if (opposite!= null) {//一定要判断,否则进入无限递归
Lamp.valueOf(opposite).turnStatus();
}
}
}
//注释掉的部分是张老师的原来代码,我讲light和blackOut方法整合成了turnStatus函数
/ *public void light(){
this.status = true;
if(opposite != null){
Lamp.valueOf(opposite).light();
}
System.out.println(name() + " lamp is green");
}
public Lamp blackOut(){
this.status = false;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp tempnextLamp=null;
if(nextLamp != null){
tempnextLamp =Lamp.valueOf(nextLamp);
System.out.println("" + name() + "-------->" +nextLamp);
tempnextLamp.light();
}
return tempnextLamp;
}* /
private String opposite=null;
private String nextLamp= null;
private boolean status =false;
}
【LampController类】:
整个系统中只能有一套交通灯控制系统,所以,LampController类最好是设计成单例。
LampController构造方法中要设定第一个为绿的灯。
LampController对象的start方法中将当前灯变绿,然后启动一个定时器,每隔10秒将当前灯变红和将下一个灯变绿。
public class LampController {
private LampcurrentLamp;
public LampController(){
currentLamp =Lamp.S2N;
currentLamp.turnStatus();
//currentLamp.light();
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
newRunnable(){
public void run(){
currentLamp.turnStatus();
currentLamp = currentLamp.getNextLamp();
//currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
【MainClass类】:
l
用for循环创建出代表12条路线的对象。
l
接着再获得LampController对象并调用其start方法。
public class MainClass {
public static voidmain(String[] args) {
String[] directions= new String[]{
"S2N","S2W","E2W","E2S",
"N2S","N2E","W2E","W2N",
"S2E","E2N","N2W","W2S"
};
for (String str:directions) {
newRoad(str);
}
new LampController();
}
}