国产激情久久久久影院小草_国产91高跟丝袜_99精品视频99_三级真人片在线观看

JDK5交通燈模擬控制系統(tǒng)

時(shí)間:2024-10-11 20:52:38 JAVA認(rèn)證 我要投稿
  • 相關(guān)推薦

關(guān)于JDK5交通燈模擬控制系統(tǒng)

  為了方便廣大程序猿交流和學(xué)習(xí),下面小編準(zhǔn)備了關(guān)于JDK5交通燈模擬控制系統(tǒng),歡迎大家參考!

  本系統(tǒng)由 Lamp.java , LampController.java , Road.java 和MainClass.java組成。

  Lamp.java :

  package com.isoftstone.interview.traffic;

  public enum Lamp {

  //前進(jìn) ,左拐 ,右拐

  S2N("N2S","S2W",false), S2W("N2E","E2W",false), S2E(null,null,true),

  E2W("W2E","E2S",false), E2S("W2N","S2N",false), E2N(null,null,true),

  N2S(null,null,false) , N2E(null,null,false), N2W(null,null,true),

  W2E(null,null,false) , W2N(null,null,false), W2S(null,null,true);

  String opposite;

  String next;

  boolean lighted;

  //構(gòu)造函數(shù):初始化當(dāng)前燈

  private Lamp(String opposite,String next,boolean lighted){

  this.opposite = opposite;

  this.next = next;

  this.lighted = lighted;

  }

  //返回當(dāng)前燈的狀態(tài)

  public boolean isLighted(){return lighted;}

  public void light(){

  this.lighted = true;

  if(opposite != null){

  Lamp.valueOf(opposite)。light();

  }

  System.out.println(name() + "is Green. Soon there will be cars crossed the street at six deractions.");

  }

  public Lamp blackout(){

  //關(guān)閉當(dāng)前燈 : 設(shè)為false

  this.lighted = false;

  Lamp nextLamp = null;

  if(opposite != null){Lamp.valueOf(opposite)。blackout();}

  //檢查下一個(gè)燈并啟動(dòng)它

  if(next != null){

  nextLamp = Lamp.valueOf(next);

  System.out.println(name() + " to the " + next + " 's light is Green.");

  nextLamp.light();

  }

  return nextLamp;

  }

  }

  LampController.java

  package com.isoftstone.interview.traffic;

  import java.util.concurrent.Executors;

  import java.util.concurrent.TimeUnit;

  public class LampController {

  private Lamp currentLamp;

  public LampController(){

  currentLamp = Lamp.S2N;

  currentLamp.light();

  //啟動(dòng)一個(gè)線程 : 每十秒將當(dāng)前燈設(shè)置為紅

  Executors.newScheduledThreadPool(1)。scheduleAtFixedRate(

  new Runnable() {

  public void run() {

  currentLamp = currentLamp.blackout();

  }

  },

  10,

  10,

  TimeUnit.SECONDS

  );

  }

  }

  Road.java

  package com.isoftstone.interview.traffic;

  import java.util.List;

  import java.util.ArrayList;

  import java.util.Random;

  import java.util.concurrent.Executors;

  import java.util.concurrent.TimeUnit;

  public class Road {

  private String name;

  private List vehicles = new ArrayList();

  public Road(String name){

  this.name = name;

  //模擬車輛不斷隨機(jī)上路的過(guò)程

  Executors.newSingleThreadExecutor()。execute(new Runnable() {

  public void run() {

  for(int i = 0 ; i < 1000 ;i++){

  try {

  Thread.sleep((new Random()。nextInt(10) + 1) * 1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  vehicles.add(Road.this.name + "_" + i);

  }

  }

  });

  //每隔一秒檢查對(duì)應(yīng)的燈是否為綠,如果是 ,則放行一輛車,具體操作為從vehicles集合中移除第一輛車。

  Executors.newScheduledThreadPool(1)。scheduleAtFixedRate(

  new Runnable() {

  public void run() {

  if(vehicles.size() > 0){

  if(Lamp.valueOf(Road.this.name)。isLighted()){

  System.out.println(vehicles.remove(0) + " is traversing");

  }

  }

  }

  },

  1,

  1,

  TimeUnit.SECONDS);

  }

  }

  最后在Main方法中啟動(dòng)系統(tǒng):public static void main(String[] args) {

  String[] deractions = {"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};

  //模擬十二條方向的路線

  for(int i = 0 ; i < deractions.length; i++){

  new Road(deractions[i]);

  }

  //啟動(dòng)交通燈控制器

  new LampController();

  }

【JDK5交通燈模擬控制系統(tǒng)】相關(guān)文章:

沙盤模擬的介紹10-19

模擬主持實(shí)例08-18

模擬主持的開(kāi)場(chǎng)語(yǔ)-模擬主持注意事項(xiàng)07-06

CorelDRAW認(rèn)證模擬試題10-14

小升初模擬試卷語(yǔ)文06-28

小學(xué)小升初模擬試卷09-02

美國(guó)CPA模擬試題10-22

erp沙盤模擬的攻略05-28

Java筆試模擬試卷10-18

《經(jīng)營(yíng)與管理》模擬試題07-15