1、package com.isoftstone.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;/* 每个 Road 对象代
2、表一条路线,总共有 12 条路线,即系统中总共要产生 12 个 Road 实例对象。* 每条路线上随机增加新的车辆,增加到一个集合中保存。* 每条路线每隔一秒都会检查控制本路线的灯是否为绿,是则将本路线保存车的集合中的第一辆车移除,即表示车穿过了路口。* author 张孝祥 www.it315.org*/public class Road private List vechicles = new ArrayList();private String name =null;public Road(String name)this.name = name;/模拟车辆不断随机上路的过程Execut
3、orService pool = Executors.newSingleThreadExecutor();pool.execute(new Runnable()public void run()for(int i=1;i0)boolean lighted = Lamp.valueOf(Road.this.name).isLighted();if(lighted)System.out.println(vechicles.remove(0) + “ is traversing !“);,1,1,TimeUnit.SECONDS);package com.isoftstone.interview.traffic;public class MainClass /* param args*/public static void main(String args) /*产生 12 个方向的路线 */String directions = new String“S2N“,“S2W“,“E2W“,“E2S“,“N2S“,“N2E“,“W2E“,“W2N“,“S2E“,“E2N“,“N2W“,“W2S“;for(int i=0;i切换为“ + next);nextLamp.light();return nextLamp;