看了张孝祥老师的交通灯项目后,我自己重写编写了一遍代码,但是,在运行时却发生了意想不到的问题。我的代码如下:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Road {
String name = null;
List<String> vehicles = new ArrayList<String>();
Road(String name) {
this.name = name;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i < 1000; i++) {
// 内部类访问外部类的方法
vehicles.add(Road.this.name + "_" + i);
// 输出对应道路上车辆数及对应灯是否为緑色
System.out.println(" " + Road.this.name + " "
+ Lamp.valueOf(Road.this.name).isGreen + " 车辆 "
+ vehicles.size());
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
} catch (InterruptedException e) {
}
}
}
}).start();
// Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
// new Runnable() {
// @Override
// public void run() {
// Lamp lamp = Lamp.valueOf(Road.this.name);
// if (lamp.isGreen) {
// System.out.println(vehicles.remove(0)
// + " is running....." + "rest:"
// + vehicles.size());
// }
// }
// }, 1, 1, TimeUnit.SECONDS);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Lamp lamp = Lamp.valueOf(Road.this.name);
if (vehicles.size() > 0)
if (lamp.isGreen) {
System.out.println(vehicles.remove(0)
+ " is running....." + "rest:"
+ vehicles.size());
}
}
}
}).start();
}
}
public enum Lamp {
W2E("E2W","W2N",false),W2N("E2S","S2N",false),S2N("N2S","S2W",false),S2W("N2E","W2E",false),
E2W(null,null,false),E2S(null,null,false),N2S(null,null,false),N2E(null,null,false),
W2S(null,null,true),S2E(null,null,true),E2N(null,null,true),N2W(null,null,true);
String oppositeLamp;
String nextLamp;
boolean isGreen;
private Lamp(String oppositeLamp, String nextLamp, boolean isGreen) {
this.oppositeLamp = oppositeLamp;
this.nextLamp = nextLamp;
this.isGreen = isGreen;
}
public void turnGreen(){
isGreen=true;
System.out.println(name()+"変緑");
if(oppositeLamp!=null){
Lamp.valueOf(oppositeLamp).turnGreen();
}
}
public void turnRed(){
isGreen=false;
System.out.println("转换....");
if(oppositeLamp!=null){
Lamp.valueOf(oppositeLamp).turnRed();
}
}
public Lamp getNextLamp(){
Lamp next=null;
if(nextLamp!=null)
next=Lamp.valueOf(nextLamp);
return next;
}
public boolean isGreen(){
return isGreen;
}
}
public class LampController {
Lamp presentLamp;
LampController() {
presentLamp=Lamp.W2E;
new Thread(new Runnable() {
@Override
public void run() {
while(true){
presentLamp.turnGreen();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
presentLamp.turnRed();
presentLamp=presentLamp.getNextLamp();
}
}
}).start();
}
}
public class MainClass {
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"
};
new LampController();
for(int i=0;i<directions.length;i++){
new Road(directions);
}
}
}
在以上代码中,此时运行是正常的。但是,当将Road类中判断车辆是否通过该路线的线程改为线程池创建的线程(即Road类中注释了得代码)时,运行却除了问题。具体表现为第二次変灯后路面向右转弯的车辆停止不前进了,但是,通过输出内容知道向右拐弯的车辆继续在增加,右拐灯也始终为緑。
再运行一段时间后,十字路口只有来的车辆,但是车辆却堵在了十字路口。 |