package com.itcast.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;
/**
* @author LWP
*
*/
public class Road {
private List<String> vechicles = new ArrayList<String>();
private String name = null;
public Road(String name){
this.name = name;
//java.util.concurrent 包含许多线程安全、测试良好、高性能的并发构建块。
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 1; i <= 1000; i++) {
try {
Thread.sleep((new Random().nextInt(10)+1)*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
vechicles.add(Road.this.name + "的第" +i+"辆车");
}
}});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//检查是否有车
if (vechicles.size()>0) {
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if (lighted) {
System.out.println(vechicles.remove(0)+" 已经通过!");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
package com.itcast.interview.traffic;
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);
private boolean lighted;
private String opposite;
private String next;
private Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
private Lamp(){}
public boolean isLighted(){
return lighted;
}
//使灯变绿
public void light(){
this.lighted = true;
if(opposite != null)
Lamp.valueOf(opposite).light();
System.out.println(name()+"变绿灯,总共应该有六个方向的汽车可以通行!");
}
//使灯变红
public Lamp blackOut(){
this.lighted = false;
if(opposite != null)
Lamp.valueOf(opposite).blackOut();
Lamp nextLamp = null;
if(next != null){
nextLamp = Lamp.valueOf(next);
System.out.println("绿灯从"+name()+"-->切换为"+next);
nextLamp.light();
}
return nextLamp;
}
}
package com.itcast.interview.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.light();
//定时器
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
package com.itcast.interview.traffic;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] directions = new String[]{"S2N","S2W","E2W","E2S",
"N2S","N2E","W2E","W2N",
"S2E","E2N","N2W","W2S"};
for (String str : directions) {
new Road(str);
}
new LampController();
}
}