代码模块
1路
目的:
1、用集合模拟车辆,集合中元素的增加和减少模拟车辆的增加和减少
2、随机往路末尾加入车辆
代码:
public class Road{
List<String> vechicles = ArrayList<E>//1,面向接口编程
//指定路线的名字
private String name = null ;
public Road(String name){
this.name = name ;
//随机加入车辆
ExcecutorService pool = Executors.newSingleThreadExecutor() ; //2线程池
//利用线程池随机添加车辆
pool.execute(new Runnable(){
public run(){
for(int i = 1 ; i< 1000 ,I ++){
try{
Thread.sleep(
(new Random().nextInt(10)+1)*1000) ;//3随机值
}catch(InterruptedException e){
}
vechicles.add(this.name + “” + i)
//4内部类访问外部类的成员变量1成员变量被final修饰2Road.this.name
}
}
}) ;
//定时移开车辆
//5定时器,调度池
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
if(vechicles.size() > 0){
boolean lighted = true ;
if(lighted){
//移开车辆
vechicles.remove(0) ;
}
}
}
},
1,
1,
TimeUnit.SECONDS
) ;
}
}
2灯
目的:
1、12个灯,使用枚举,只有4个灯有逻辑
2、三个成员变量:下一个灯、 对应方向的灯、 自己的状态
代码:
public enum Lamp{
S2N,S2W,E2W,E2S,
N2S,N2E,W2E,W2N,
S2N,E2W,N2W,W2S ;//1分号
//对应的灯
private Lamp opposite ;
//灯的状态
private boolean liglted ;
public boolean isLighted(){
return lighted ;
}
//灯变绿变红的方法
public void liglt(){
this.lighted = true ;
if(opposite != null)
opposite.light() ;
}
public void blackOut(){
this.lighted = false ;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp = null ;
if(next != null){
nextLamp = Lamp.valueOf(next) ;
nextLamp.light() ;
}
return nextLamp ;
}
}
3灯的控制器
当前的绿灯变红
定时器
当灯变红,下一个灯返回,便于下一次变绿
public class LampContro{
//当前的灯
private Lamp currentLamp;
public LampContro(){
//指定当前的灯
currentLamp = Lamp.S2N;
//让当前的灯是绿的
currentLamp.light() ;
//每隔10S让当前灯变红,让下一个灯变绿
ScheduledExecutorService timer = Excutors.newScheduledThreadPool(1)
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
currentLamp = currentLamp.blackOut() ;
}
},
10,
10,
TimeUnit.SECONDS;
);
}
}
4主程序
public class MainClass{
public static void main(){
//1字符数组,循环遍历
String[] directions = new String[]{
“S2N”,”S2W”,”E2W”,”E2S”,
“N2S”,”N2E”,”W2E”,”W2N”,
“S2E”,”E2N”,”N2W”,”W2S” ;
};
for(int i = 0 ; i<directions.length ; i++){
new Road(directions[i]) ;
}
new LampContro();
}
}
代码模块
1路
目的:
1、用集合模拟车辆,集合中元素的增加和减少模拟车辆的增加和减少
2、随机往路末尾加入车辆
代码:
public class Road{
List<String> vechicles = ArrayList<E>//1,面向接口编程
//指定路线的名字
private String name = null ;
public Road(String name){
this.name = name ;
//随机加入车辆
ExcecutorService pool = Executors.newSingleThreadExecutor() ; //2线程池
//利用线程池随机添加车辆
pool.execute(new Runnable(){
public run(){
for(int i = 1 ; i< 1000 ,I ++){
try{
Thread.sleep(
(new Random().nextInt(10)+1)*1000) ;//3随机值
}catch(InterruptedException e){
}
vechicles.add(this.name + “” + i)
//4内部类访问外部类的成员变量1成员变量被final修饰2Road.this.name
}
}
}) ;
//定时移开车辆
//5定时器,调度池
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
if(vechicles.size() > 0){
boolean lighted = true ;
if(lighted){
//移开车辆
vechicles.remove(0) ;
}
}
}
},
1,
1,
TimeUnit.SECONDS
) ;
}
}
2灯
目的:
1、12个灯,使用枚举,只有4个灯有逻辑
2、三个成员变量:下一个灯、 对应方向的灯、 自己的状态
代码:
public enum Lamp{
S2N,S2W,E2W,E2S,
N2S,N2E,W2E,W2N,
S2N,E2W,N2W,W2S ;//1分号
//对应的灯
private Lamp opposite ;
//灯的状态
private boolean liglted ;
public boolean isLighted(){
return lighted ;
}
//灯变绿变红的方法
public void liglt(){
this.lighted = true ;
if(opposite != null)
opposite.light() ;
}
public void blackOut(){
this.lighted = false ;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp = null ;
if(next != null){
nextLamp = Lamp.valueOf(next) ;
nextLamp.light() ;
}
return nextLamp ;
}
}
3灯的控制器
当前的绿灯变红
定时器
当灯变红,下一个灯返回,便于下一次变绿
public class LampContro{
//当前的灯
private Lamp currentLamp;
public LampContro(){
//指定当前的灯
currentLamp = Lamp.S2N;
//让当前的灯是绿的
currentLamp.light() ;
//每隔10S让当前灯变红,让下一个灯变绿
ScheduledExecutorService timer = Excutors.newScheduledThreadPool(1)
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
currentLamp = currentLamp.blackOut() ;
}
},
10,
10,
TimeUnit.SECONDS;
);
}
}
4主程序
public class MainClass{
public static void main(){
//1字符数组,循环遍历
String[] directions = new String[]{
“S2N”,”S2W”,”E2W”,”E2S”,
“N2S”,”N2E”,”W2E”,”W2N”,
“S2E”,”E2N”,”N2W”,”W2S” ;
};
for(int i = 0 ; i<directions.length ; i++){
new Road(directions[i]) ;
}
new LampContro();
}
}