交通灯面试题(具体解释在程序中)
1. 创建路Road
publicclass Road {
//定义路上的车
List<String>vechicles = new ArrayList<String>();
privateString name;
//根据方向为每一天路命名
publicRoad(String name){
this.name= name;
//在路上创建车,一个线程创建对象,一个线程增加车辆
//使用java5创建线程,单独一个线程来增加车辆,不然很难创键对象
//使用执行器来创建线程池,线程池中有很多线程,把任务放进线程池中,空闲的线程执行该任务
//Executors.newFixedThreadPool(1);线程池中有一个线程
ExecutorServicepool = Executors.newSingleThreadExecutor();//创建一个单线程的线程池
//让线程池执行一个任务
pool.execute(newRunnable(){
@Override
publicvoid run() {
for(inti = 1;i<1000;i++){
//每隔多长时间获得一辆车
try{
Thread.sleep((newRandom().nextInt(10)+1)*100);//1-10秒增加一辆
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
vechicles.add(Road.this.name+"_"+i);//此处获得外部类的成员变量,也可以用final
}
}
});
//使用调度池,创建一个定时器,定时多少时间灯亮和灭
ScheduledExecutorServicetimer = Executors.newScheduledThreadPool(1);
//使用定时器command是指线程池定时的任务,delay多长时间后执行,unit时间类型
//timer.schedule(command,delay, unit)
//使用固定频率的定时器
timer.scheduleAtFixedRate(
newRunnable(){
publicvoid run(){
//定时任务
//如果该路上为绿灯,则把该路上第一辆车移走
if(vechicles.size()>0){
//判断该灯状态
booleanlighted = Lamp.valueOf(Road.this.name).isLighted();
if(lighted){//vechicles.remove(0)返回移走的那辆车
System.out.println(vechicles.remove(0)+"istraversing!");
}
}
}
},
10,//过10s开始干
10,//每10s一次
TimeUnit.SECONDS);
}
2.创建灯
publicenum 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);
/*S2N("N2S"),S2W,E2W,E2S,
N2S,N2E,W2E,W2N,
S2E,E2N,N2W,W2S;*/
//通过构造方法来设置对应的灯
//枚举的构造方法必须是私有的,所以如果是单个对象,是一种单例模式
privateLamp(){}
privateLamp(String oppostie,String next,boolean lighted){
this.opposite= opposite;
this.next= next;
this.lighted= lighted;
}
//灯亮灭的标志
privateboolean lighted ;
//对应到灯,要和本灯一致
privateString opposite;
//下一个灯
privateString next;
//得到灯的状态
publicboolean isLighted(){
returnlighted;
}
//点亮
publicvoid light(){
this.lighted= true;
if(opposite!= null){
Lamp.valueOf(opposite).light();//同时对应的灯也量
}
System.out.println(this.name()+"lampis green,下面总共有六条路通车");
}
//变红
publicLamp blackOut(){
this.lighted= false;
if(opposite!= null){
Lamp.valueOf(opposite).light();//同时对应的灯也量
}
LampnextLamp = null;
if(next!= null){
nextLamp= Lamp.valueOf(next);
nextLamp.light();//下一个灯变绿
System.out.println("绿灯从"+this.name()+"-------->切换为"+next);
}
returnnextLamp;
}
3.创建控制器
//灯的控制器
publicclass LampController {
privateLamp currentLamp;
publicLampController( ){
currentLamp= Lamp.S2N;//获取当前灯
currentLamp.light();//当前灯变绿
//使用调度池定义一个定时器,每隔一段时间灯的状态发生改变,其实也是一个线程
ScheduledExecutorServicetimer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
newRunnable(){
publicvoid run(){
currentLamp= currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
4.测试
publicstatic void main(String[] args) {
String[]directions = new String[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
//创建12条路
for(inti = 0;i<directions.length;i++){
newRoad(directions[i]);
}
newLampController();
}
}