只学了javase,对枚举不了解,对线程池也不了解,根据张孝祥老师的思路,自己写个程序,写的不好,但是运行成功,小小的自豪感,拿出来晒晒,请各位批评指教。
/*
信号灯类
*/
public class Lamp {
String name;
boolean isGreen;
Lamp nextLamp;
Lamp oppositeLamp;
public Lamp(String name, boolean isGreen) {
super();
this.name = name;
this.isGreen = isGreen;
}
public void setNextLamp(Lamp nextLamp) {
this.nextLamp = nextLamp; }
public void setOppositeLamp(Lamp oppositeLamp) {
this.oppositeLamp = oppositeLamp;
}
//信号灯变红的方法。
public Lamp changered(){
this.isGreen=false;
this.oppositeLamp.isGreen=false;
this.nextLamp.isGreen=true;
this.nextLamp.oppositeLamp.isGreen=true;
System.out.println(this.name+" 和 "+this.oppositeLamp.name+"灯变红 "+
this.nextLamp.name+" 和 "+this.nextLamp.oppositeLamp.name+"灯变绿");
return nextLamp;
}
}
//信号灯控制类,采用单独一个线程控制。
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Control {
Lamp currentLamp;
public Control(Lamp lamp){
this.currentLamp =lamp;
new Timer().schedule(new TimerTask(){
@Override
public void run() {
currentLamp= currentLamp .changered();
}}, new Date(System.currentTimeMillis()+5000), 5000);
}
}
//道路类 和main 方法,道路类 中将信号灯作为道路类的一个变量,每条路一条线程。
package Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) throws InterruptedException {
Lamp s2n,s2w,n2s,n2e,w2e,w2n,e2w,e2s,s2e,e2n,n2w,w2s;
s2n=new Lamp("南向北",true);
s2w=new Lamp("南向西",false);
n2s=new Lamp("北向南",true);
n2e=new Lamp("北向东",false);
w2e=new Lamp("西向东",false);
w2n=new Lamp("西向北",false);
e2w=new Lamp("东向西",false);
e2s=new Lamp("东向南",false);
s2e=new Lamp("南向东",true);
e2n=new Lamp("南向东",true);
n2w=new Lamp("南向东",true);
w2s=new Lamp("南向东",true);
s2n.setOppositeLamp(n2s);
s2w.setOppositeLamp(n2e);
//n2s.setOppositeLamp(s2n);
//n2e.setOppositeLamp(s2w);
w2e.setOppositeLamp(e2w);
w2n.setOppositeLamp(e2s);
//e2w.setOppositeLamp(w2e);
//e2s.setOppositeLamp(w2n);
s2n.setNextLamp(s2w);
//n2s.setNextLamp(s2n);
s2w.setNextLamp(w2e);
// n2e.setNextLamp(e2w);
w2e.setNextLamp(w2n);
// e2w.setNextLamp(e2s);
w2n.setNextLamp(s2n);
// e2s.setNextLamp(n2s);
Road S2N=new Road("S2N",s2n);
Road S2W=new Road("S2W",s2w);
Road W2E=new Road("W2E",w2e);
Road W2N=new Road("W2N",w2n);
Road N2S =new Road("N2S",n2s);
Road N2E=new Road("N2E",n2e);
Road E2W=new Road("E2W",e2w);
Road E2S=new Road("E2S",e2s);
Road S2E=new Road("S2E右拐弯",s2e);
Road E2N=new Road("E2N右拐弯",e2n);
Road N2W=new Road("N2W右拐弯",n2w);
Road W2S=new Road("W2S右拐弯",w2s);
Control c=new Control(s2n);
}
}
class Road {
String roadName=null;
List<String> carName;
Lamp lamp;
public Road(String roadName,Lamp lamp) throws InterruptedException{
this.roadName =roadName;
this.lamp =lamp;
this.carName=new ArrayList<String>();
new Thread( new Runnable( ){
public void run() {
for(int i=0;i<100;i++){
try {
Thread.currentThread().sleep(((new Random().nextInt(10) + 1) * 1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
carName.add(Road.this.roadName+"方向上第"+i+"辆车");
}
}
}).start();
new Timer().schedule(new TimerTask(){
@Override
public void run() {
if(carName.size()>0){
if(Road.this.lamp.isGreen){
String s=carName.remove(0);
System.out.println(s);
}}
}
},
new Date(System.currentTimeMillis()+0),
500);
}
}
|
|