package com.itheima.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 { private List<String> vechicls = new ArrayList<String>(); //创建一个车的集合 private String name=null; public Road(String name) //构造方法赋值(路) { this.name = name; ExecutorService pool = Executors.newSingleThreadExecutor(); //使用Executors工具类,创建一个 增加车的线程的 pool.execute(new Runnable(){ public void run() { for(int i = 1;i< 1000;i++) //设定有1000辆车循环增加车辆 { try { Thread.sleep((new Random().nextInt(10)+1)*1000); //设定每隔1-10秒增加一次 } catch (InterruptedException e) { e.printStackTrace(); } vechicls.add(Road.this.name+"_"+i); //增加车辆(名字为:路_第几辆车) } } }); ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); // 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。//创建线程池,控制车辆行驶 timer.scheduleAtFixedRate( new Runnable(){ public void run() { if(vechicls.size()>0)//当集合大小不为0时,才执行。 { boolean lighted = Lamp.valueOf(Road.this.name).isLighted(); //调用Lamp类的isLighted();返回灯是否亮。 if(lighted) System.out.println(vechicls.remove(0)+"is traversing!"); } } }, 1,//第一秒开始 1,//每隔一秒执行一次 TimeUnit.SECONDS);//单位秒 } } package com.itheima.traffic; /* * 使用枚举,列举灯控制发方向 * 北:N 南:S 西:W 东:E * S2N代表:南向北方向。以此类推。 * */ public enum Lamp { 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); //列举出12个方向 private Lamp (String opposite,String next,boolean lighted) { this.opposite = opposite; this.next = next; } private boolean lighted; //灯是否是绿。 private String opposite; //对面的灯 private String next; //下一个灯 public boolean isLighted() { return lighted; } public void ligth() { this.lighted = true; //设定某一个路口的灯亮绿 if(opposite !=null) { Lamp.valueOf(opposite).ligth(); //对面驶来方向的灯也要亮绿 } System.out.println(name()+" Lamp is Green 下边总共有6个方向的汽车穿过!"); } /*某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿 * return 下一个要变绿的灯*/ public Lamp blackOut() { this.lighted = false; if(opposite !=null) { Lamp.valueOf(opposite).blackOut(); } Lamp nextLight = null; if(next !=null) { nextLight = Lamp.valueOf(next); System.out.println("绿灯从"+name()+"切换为--------->next"); nextLight.ligth(); } return nextLight; } } package com.itheima.traffic; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; //创建灯控制器类 public class LampController { private Lamp currentLamp; public LampController() { currentLamp = Lamp.S2N; //设定当前灯控制的方向 currentLamp.ligth(); //灯为绿 ScheduledExecutorService timer =Executors.newScheduledThreadPool(1); timer.scheduleAtFixedRate( new Runnable(){ public void run() { currentLamp = currentLamp.blackOut(); } }, 10, 10, TimeUnit.SECONDS); } } package com.itheima.traffic; //创建Main类 执行 public class MainClass { public static void main(String[] args) { String [] directions = new String[]{"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"}; for(int i = 0;i< directions.length;i++)//创建12条路线 { new Road(directions[i]); } new LampController(); //开启灯控制器 } }