package com.isoftone.interview.traffic;
publicclass MainClass {
publicstaticvoid main(String[] args) {
//NEW 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();
new LampController();
}
}
灯控制器
package com.isoftone.interview.traffic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LampController {
private Lamp currenLamp;
public LampController(){
//初始化灯
currenLamp=Lamp.S2N;
//路灯亮
currenLamp.light();
ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
public void run() {
currenLamp=currenLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
灯
package com.isoftone.interview.traffic;
/**
*每个Lamp元素代表一个方向上的灯,总共有12个方向,所有总共有12个Lamp元素。
*有如下一些方向上的灯,每两个形成一组,一组灯同时变绿或变红,所以,
*程序代码只需要控制每组灯中的一个灯即可:
* s2n,n2s
* s2w,n2e
* e2w,w2e
* e2s,w2n
* s2e,n2w
* e2n,w2s
*上面最后两行的灯是虚拟的,由于从南向东和从西向北、以及它们的对应方向不受红绿灯的控制,
*所以,可以假想它们总是绿灯。
*@author张孝祥org
*
*/
publicenum Lamp {
//十二个灯的状态
S2N("N2S","S2W",false),S2W("N2W","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 Lamp(){
}
private Stringnext;
privatebooleanlighted;
private Stringopposite;//对应的灯
publicboolean isLighted(){
returnlighted;
}
//灯变绿时,它对应方向的灯也要变绿
publicvoid light(){
this.lighted=true;
//不这样写会死循环
if(opposite!=null){
//返回枚举对象
Lamp.valueOf(opposite).light();
}
System.out.println(name()+"Lamp is green,下面总共应该有6个方向能看到汽车穿过");
}
public Lamp blackOut(){
this.lighted=false;
if(opposite!=null){
//返回枚举对象
Lamp.valueOf(opposite).blackOut();
}
//绿等为null会影响下面的执行
Lamp nextLamp=null;
if (next!=null) {
nextLamp=Lamp.valueOf(next);
nextLamp.light();
System.out.println("绿灯从"+name()+"--------->>切换为"+next);
}
return nextLamp;
}
}
路
package com.isoftone.interview.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 {
//1.外部类+final
//2.访问外部类的成员变量,内部类,使用外部类的名字+this.内部类的成员变
List<String> vechicles = new ArrayList<String>();
private String name=null;
public Road(String name){
this.name=name;
ExecutorService pool=Executors.newSingleThreadExecutor();
//new一个实现类
pool.execute(new Runnable() {
public void run() {
for(int i=1;i<1000;i++){
try {
Thread.sleep(new Random().nextInt(10)+1*1000);
System.out.println("随机停顿时间");
} catch (Exception e) {
System.out.println("出现线程异常,情况!");
}
//重名访问外部类name
vechicles.add(Road.this.name+"_"+i);
System.out.println("增加第"+i+"车辆");
}
}
});
//做定时器
ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
public void run() {
//移走路上的车
if(vechicles.size()>0){
boolean lightd=true;
if (lightd) {
//返回拿出的数据
System.out.println(vechicles.remove(0)+"is traveling!");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}*/
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;
/**
*每个Road对象代表一条路线,总共有12条路线,即系统中总共要产生12个Road实例对象。
*每条路线上随机增加新的车辆,增加到一个集合中保存。
*每条路线每隔一秒都会检查控制本路线的灯是否为绿,是则将本路线保存车的集合中的第一辆车移除,即表示车穿过了路口。
* @author张孝祥 www.it315.org
*
*/
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);
}
}
});
//每隔一秒检查对应的灯是否为绿,是则放行一辆车
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);
}
}