1.道路有4个方向 路径就有12个组合方式
先直行再左行, 右行的灯常绿
2.类: Lamp ,LampControl,Car, Road
而car只需要一个字符串来表示即可。
3.Road
一共12条路径
使用List来表示某个路径上的车辆存储
每隔1~10秒 add一辆车 并打上index作为记号
每秒进行一次检查 是否灯为绿 是 remove一辆 否则 什么都不做
需要导入的两个方法:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。
ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
变量 : name 代表路线名称
方法只提供了构造方法方法,
Road被初始化时,就会建立一个随即时间增加车辆的线程并每隔1秒检查一下灯的状态 来确定是否放行
4.Lamp
使用枚举的方法来定义灯
一共有12个灯
右转灯都是常量
而对行的灯总是一起变化的 因此只需要考虑其中4个灯的交替变换
每个灯3个参数,分别表示:
和他配对的灯(非逻辑灯为null),下一个逻辑灯(非逻辑灯为null),红灯还是绿灯
考虑两个方法
逻辑灯绿灯 配对的灯也变绿
逻辑灯红灯 配对的灯也红灯 下一个逻辑灯绿灯
5.LampController
有一个初始化值
南北方向的灯为绿
然后每隔10秒开始当前逻辑灯变红 下一个逻辑等变绿
变量为currentLamp,表示当前逻辑灯
方法只有构造方法:每隔10秒换灯
MainClass
产生12个方向路线 并建立交通灯系统
package test;
/**
*
* @author 黑马_王康
*
*/
public class MainClass {
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<directions.length;i++)
{
new Road(directions[i]);
}
//产生整个交通灯系统
new LampController();
}
}
package test;
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 {
private List<String> vechicles = new ArrayList<String>();
private String name =null;
public Road(String name){
this.name = name;
//模拟车辆不断随机上路的过程
//public static ExecutorService newSingleThreadExecutor()
//创建一个使用单个 worker 线程的 Executor
ExecutorService pool = Executors.newSingleThreadExecutor();
//void execute(Runnable command)
//在未来某个时间执行给定的命令
pool.execute(new Runnable(){
public void run(){
//每隔1~10秒 增加这条线路上的一辆车 并给与编号
int i=0;
while(true){
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
vechicles.add(Road.this.name + "_" + ++i);
}
}
});
//每隔一秒检查对应的灯是否为绿,是则放行一辆车
//public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
//创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
// ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
//创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,
//然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
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);
}
}
package test;
public enum Lamp {
//四个逻辑等
S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
//opposite灯
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 lighted;
//对应的方向
private String opposite;
//下一个逻辑灯
private String next;
//构造函数
private Lamp(String opposite,String next,boolean lighted)
{
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
//红 ->false 绿-> true
public boolean isLighted()
{
return lighted;
}
// 某个灯变绿时,它对应方向的灯也要变绿
public void light()
{
this.lighted = true;
//判断 方便调用light();
if(opposite != null)
{
//使用valueOf可以很容易的调用到opposite和next的对象
Lamp.valueOf(opposite).light();
}
System.out.println(this.name() + " lamp is green,6个方向能看到汽车穿过..");
}
// 逻辑等灯变红,对应的灯也要变红,下一个逻辑灯要变绿
public Lamp blackOut(){
this.lighted = false;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp= null;
//对逻辑灯处理
if(next != null){
nextLamp = Lamp.valueOf(next);
System.out.println("绿灯从" + name() + "-------->切换为" + next);
nextLamp.light();
}
return nextLamp;
}
}
package test;
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.light();
//1秒延迟启动
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
//每隔10秒开始当前逻辑灯变红 下一个逻辑等变绿
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}