本帖最后由 何苦似梦离 于 2014-2-23 07:42 编辑
当时候,我脑袋都爆炸了,就因为那个什么N2W之类的方向,我干脆就用这种办法模拟,就是老张的那个7K面试题,我简化了一下,那个交通灯,他用一条路线对应一个灯,太费资源了 ,我用了3个灯,4条1个,4条1个,4条个(这是常绿的),然后,路的名称就我们自己输入进入就可以了,简单发一下灯,和MainClass代码灯代码:
- package cn.apple.Traffic;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class LampController
- {
- private TrafficLamp currentLamp;
-
- public LampController()
- {
- currentLamp = TrafficLamp.LAMP1;
- currentLamp.light();
-
- ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
- timer.scheduleAtFixedRate(
- new Runnable() //一个定时器,每隔10秒转换灯
- {
- @Override
- public void run()
- {
- currentLamp = currentLamp.blackOut();
-
- System.out.println(currentLamp+"转绿灯");
- }
-
- },
- 10,
- 10,
- TimeUnit.SECONDS);
-
-
- }
- }
复制代码 MainClass代码:
- package cn.apple.Traffic;
- public class MainClass
- {
- public static void main(String[] args)
- {
- new Road("南到北","LAMP1");
- new Road("南到西","LAMP1");
- new Road("南到东","AFTENGREEN");
- new Road("北到南","LAMP1");
- new Road("北到西","AFTENGREEN");;
- new Road("北到东","LAMP1");
-
-
- new Road("西到东","LAMP2");
- new Road("西到北","LAMP2");
- new Road("西到南","AFTENGREEN");
- new Road("东到西","LAMP2");
- new Road("东到北","AFTENGREEN");
- new Road("东到南","LAMP2");
-
-
- new LampController();
- }
复制代码
|