面试题之交通灯


交通灯面试题(具体解释在程序中)

      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();

                            }

 

                   }

 

                  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值