本方法模拟了现实生活中的交通信号灯的情况
1.先构建Road类,此类可以创建12个方向的路
代码如下:
package com.springtie.traffic; 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 {
// 12个方向的路名
private String roadname;
// 存储车辆的集合
private List<String> vehicle = new ArrayList<String>(); // 根据每个方向的不同创建不同的路名
public Road(String roadname) {
this.roadname = roadname;
// 向当前路添加1000辆车
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable() {
public void run() {
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int car = 0; car < 1000; car++) {
// 把车辆添加进来
vehicle.add(roadname + "方向的车辆" + car);
// System.out.println(roadname+"方向的车辆"+car+"正在开过来");
}
}
});
// 当前路上的,灯位绿色的,使其最前面的车辆1秒后穿越马路,再过一秒后上来的最前面车辆穿越马路,如此循环,知道灯变红
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
public void run() {
if (!vehicle.isEmpty()) {
boolean lampState = Lamp.valueOf(roadname).isLighted();
if (lampState) { System.out.println(vehicle.remove(0) + "穿越马路");
}
}
}
}, 1, 1, TimeUnit.SECONDS);
}
}
2.创建交通灯枚举Lamp,通过私有构造函数传入当前灯的对应灯和下一个灯以及当前的状态
代码如下:
package com.springtie.traffic; public enum Lamp {
// 12个信号灯枚举对象
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 String oppositeLamp;
private String nextLamp;
private boolean lampState; // 私有构造函数
private Lamp(String oppositeLamp, String nextLamp, boolean lampState) {
this.oppositeLamp = oppositeLamp;
this.nextLamp = nextLamp;
this.lampState = lampState;
} // 判断是红灯还是绿灯
public boolean isLighted() {
return lampState;
} // 让灯变绿,对应灯也变绿
public void turnGreen() {
this.lampState = true;
if (oppositeLamp != null) {
Lamp.valueOf(oppositeLamp).turnGreen();
}
System.out.println("灯:" + this.name() + "为绿灯,+对应等和其他四个灯也为绿灯");
} // 让灯和对应灯变红,同时让下一个灯变绿,返回下一个灯
public Lamp turnRed() {
this.lampState = false;
if (oppositeLamp != null) {
Lamp.valueOf(oppositeLamp).turnRed();
}
Lamp next = null;
if (nextLamp != null) {
next = Lamp.valueOf(nextLamp);
System.out.println("绿灯:" + this.name() + " 切换为:" + next.name());
next.turnGreen();
}
return next;
}
}
3.创建灯控系统类LampController,利用此类来控制灯变量的顺序
代码如下:
package com.springtie.traffic; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; public class LampController {
private Lamp currentLamp; public LampController() {
currentLamp = Lamp.S2N;
// 让当前灯变绿
currentLamp.turnGreen();
// 单开一个线程,当前灯变红的同时下个等变绿,一直重复
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("当前灯变红时,返回下个灯并让其变绿");
currentLamp = currentLamp.turnRed();
}
}, 10, 10, TimeUnit.SECONDS);
}
}
4.创建一个主类(MainClass)来测试
代码如下:
package com.springtie.traffic; public class MainClass { public static void main(String[] args) {
// 代表12条路的方向
String[] directionRoadName = new String[] { "S2N", "S2W", "E2W", "E2S",
"N2S", "N2E", "W2E", "W2N", "S2E", "E2N", "N2W", "W2S" };
// 把12条路的线程启动
for (int i = 0; i < directionRoadName.length; i++) {
new Road(directionRoadName[i]);
}
// 调用灯控系统
new LampController();
} }
至此,一个完整的模拟现实生活中的交通灯管理系统构建完毕。