本方法模拟了现实生活中的交通信号灯的情况
1.先构建Road类,此类可以创建12个方向的路
代码如下:
1 package com.springtie.traffic; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Random; 6 import java.util.concurrent.ExecutorService; 7 import java.util.concurrent.Executors; 8 import java.util.concurrent.ScheduledExecutorService; 9 import java.util.concurrent.TimeUnit; 10 11 public class Road { 12 // 12个方向的路名 13 private String roadname; 14 // 存储车辆的集合 15 private List<String> vehicle = new ArrayList<String>(); 16 17 // 根据每个方向的不同创建不同的路名 18 public Road(String roadname) { 19 this.roadname = roadname; 20 // 向当前路添加1000辆车 21 ExecutorService pool = Executors.newSingleThreadExecutor(); 22 pool.execute(new Runnable() { 23 public void run() { 24 try { 25 Thread.sleep((new Random().nextInt(10) + 1) * 1000); 26 } catch (InterruptedException e) { 27 e.printStackTrace(); 28 } 29 for (int car = 0; car < 1000; car++) { 30 // 把车辆添加进来 31 vehicle.add(roadname + "方向的车辆" + car); 32 // System.out.println(roadname+"方向的车辆"+car+"正在开过来"); 33 } 34 } 35 }); 36 // 当前路上的,灯位绿色的,使其最前面的车辆1秒后穿越马路,再过一秒后上来的最前面车辆穿越马路,如此循环,知道灯变红 37 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); 38 timer.scheduleAtFixedRate(new Runnable() { 39 public void run() { 40 if (!vehicle.isEmpty()) { 41 boolean lampState = Lamp.valueOf(roadname).isLighted(); 42 if (lampState) { 43 44 System.out.println(vehicle.remove(0) + "穿越马路"); 45 } 46 } 47 } 48 }, 1, 1, TimeUnit.SECONDS); 49 } 50 }
2.创建交通灯枚举Lamp,通过私有构造函数传入当前灯的对应灯和下一个灯以及当前的状态
代码如下:
1 package com.springtie.traffic; 2 3 public enum Lamp { 4 // 12个信号灯枚举对象 5 S2N("N2S", "S2W", false), S2W("N2E", "E2W", false), E2W("W2E", "E2S", false), E2S( 6 "W2N", "S2N", false), N2S(null, null, false), N2E(null, null, false), W2E( 7 null, null, false), W2N(null, null, false), S2E(null, null, true), E2N( 8 null, null, true), N2W(null, null, true), W2S(null, null, true); 9 // 枚举私有成员变量 10 private String oppositeLamp; 11 private String nextLamp; 12 private boolean lampState; 13 14 // 私有构造函数 15 private Lamp(String oppositeLamp, String nextLamp, boolean lampState) { 16 this.oppositeLamp = oppositeLamp; 17 this.nextLamp = nextLamp; 18 this.lampState = lampState; 19 } 20 21 // 判断是红灯还是绿灯 22 public boolean isLighted() { 23 return lampState; 24 } 25 26 // 让灯变绿,对应灯也变绿 27 public void turnGreen() { 28 this.lampState = true; 29 if (oppositeLamp != null) { 30 Lamp.valueOf(oppositeLamp).turnGreen(); 31 } 32 System.out.println("灯:" + this.name() + "为绿灯,+对应等和其他四个灯也为绿灯"); 33 } 34 35 // 让灯和对应灯变红,同时让下一个灯变绿,返回下一个灯 36 public Lamp turnRed() { 37 this.lampState = false; 38 if (oppositeLamp != null) { 39 Lamp.valueOf(oppositeLamp).turnRed(); 40 } 41 Lamp next = null; 42 if (nextLamp != null) { 43 next = Lamp.valueOf(nextLamp); 44 System.out.println("绿灯:" + this.name() + " 切换为:" + next.name()); 45 next.turnGreen(); 46 } 47 return next; 48 } 49 }
3.创建灯控系统类LampController,利用此类来控制灯变量的顺序
代码如下:
1 package com.springtie.traffic; 2 3 import java.util.concurrent.Executors; 4 import java.util.concurrent.ScheduledExecutorService; 5 import java.util.concurrent.TimeUnit; 6 7 public class LampController { 8 private Lamp currentLamp; 9 10 public LampController() { 11 currentLamp = Lamp.S2N; 12 // 让当前灯变绿 13 currentLamp.turnGreen(); 14 // 单开一个线程,当前灯变红的同时下个等变绿,一直重复 15 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); 16 timer.scheduleAtFixedRate(new Runnable() { 17 public void run() { 18 System.out.println("当前灯变红时,返回下个灯并让其变绿"); 19 currentLamp = currentLamp.turnRed(); 20 } 21 }, 10, 10, TimeUnit.SECONDS); 22 } 23 }
4.创建一个主类(MainClass)来测试
代码如下:
1 package com.springtie.traffic; 2 3 public class MainClass { 4 5 public static void main(String[] args) { 6 // 代表12条路的方向 7 String[] directionRoadName = new String[] { "S2N", "S2W", "E2W", "E2S", 8 "N2S", "N2E", "W2E", "W2N", "S2E", "E2N", "N2W", "W2S" }; 9 // 把12条路的线程启动 10 for (int i = 0; i < directionRoadName.length; i++) { 11 new Road(directionRoadName[i]); 12 } 13 // 调用灯控系统 14 new LampController(); 15 } 16 17 }
至此,一个完整的模拟现实生活中的交通灯管理系统构建完毕。
时间: 2024-12-23 14:37:13