需要三个对象: 红绿灯,灯的控制器,以及路
1 .路:把路作为容器,把车作为变量存储在容器里,所以在定义路的时候,就定义了车
每过多久就有车上路,如果灯为绿灯则每过一秒就放行一辆车
2. 灯:一共12个灯,但经过分析只需要定义四个灯,另外四个灯与这四个灯相对应,一起变绿变红。最后四个灯则为向右转的灯常绿。
当前的灯为绿灯时,对面的灯也需要为绿灯,
当对面的灯变红后,下一个放向的灯则需要变绿
3. 灯的控制器:控制哪一个是当前的灯的红绿,以及过多久转变灯的颜色
程序中的新线程以及Random
Executors 执行器
Executors.newFixedThreadPool(nThreads)//产生多个线程
Executors.newSingleThreadExecutor()//产生单独的线程
//返回的都是ExecutorService类型的 叫线程池
Thread.sleep(1000)采用一个范围,比使用1000更有意义
Thread.sleep((new Random().nextInt(10) + 1)* 1000);
java.util.Random
public int nextInt(int n)
返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。nextInt 的常规协定是,伪随机地生成并返回指定范围中的一个 int 值。
路
public class Road {
private List<String> vechicles = new ArrayList<String>();
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);
} catch (InterruptedException e) {
e.printStackTrace();
}
vechicles.add(Road.this.name + "_" + i);
//访问外部类成员变量可以直接调用name,但如果自己类中有相同的则必须使用Road.this.name
}
}
});
//每隔一秒检查对应的灯是否为绿,是则放行一辆车
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);
}
}
灯
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(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
/*当前灯是否为绿*/
private boolean lighted;
/*与当前灯同时为绿的对应方向*/
private String opposite;
/*当前灯变红时下一个变绿的灯*/
private String next;
//是不是亮的
public boolean isLighted(){
return lighted;
}
/**
* 某个灯变绿时,它对应方向的灯也要变绿
*/
public void light(){
this.lighted = true;
if(opposite != null){
Lamp.valueOf(opposite).light();
}
System.out.println(name() + " lamp is green,下面总共应该有6个方向能看到汽车穿过!");
}
/**
* 某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿
* @return 下一个要变绿的灯
*/
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;
}
}
管理器
public class LampController {
//当前的灯
private Lamp currentLamp;
public LampController(){
//刚开始让由南向北的灯变绿;
currentLamp = Lamp.S2N; //指定当前灯
currentLamp.light();//当前灯变绿
/*每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿*/
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
System.out.println("来啊");
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}