运行出来的效果只有这样的 为什么啊?? 这代码咋回事啊?
N2W:1 is running!
W2S:1 is running!
E2N:1 is running!
S2E:1 is running!
S2E:2 is running!
N2W:2 is running!
E2N:2 is running!
W2S:2 is running!
S2E:3 is running!
E2N:3 is running!
N2W:3 is running!
W2S:3 is running!
S2E:4 is running!
W2S:4 is running!
W2S:5 is running!
S2E:5 is running!
E2N:4 is running!
N2W:4 is running!
W2S:6 is running!
E2N:5 is running!
N2W:5 is running!
E2N:6 is running!
。。。
这是我的代码:
/*
* 实现需求:
* * 十字路口:每个路口都有直走、左拐、右拐三条路线,四个路口就有12条,每条路线上要设置一盏交通灯。
* 右转不受灯控制,直走时相对的两条路的灯一样,左拐时相对的两条路的灯也一样,所以有4对一样灯的路,
* 所以主要设置4盏灯即可,主灯亮以后让相对的灯跟着亮就行了,灭也跟着灭。
* 谁拥有数据谁叫对外提供操作这些数据的方法,所以灯应该拥有以下方法:
* 1、灯拥有判断自己灯的状态的方法
* 2、把自己点亮的方法,当自己亮的时候相对的灯也要打亮
* 3、把自己熄灭的方法,当自己熄灭时候相对的灯也要熄灭,并且让下一盏灯打亮
*
* 车看是否是红绿灯得问路,所以灯得跟路关联,这样路才能知道灯的状态,并告知车是否能通过,所以下一步定义路的类
*/
package com.itcast;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.Random;
public class Road
{
List<String> vehicles = new ArrayList<String>();
private String name = null;
public Road(String name)
{
this.name = name;
/*假设每一秒钟就出现一辆车*/
ExecutorService pool = Executors.newSingleThreadExecutor();
/*
匿名内部类定义前提:
内部类必须是继承一个类或者实现接口;
*/
pool.execute(new Runnable()
{
public void run()
{
for(int i = 1;i<1000;i++)
{
/*原因:public static void sleep(long millis)
throws InterruptedException*/
try{
Thread.sleep((new Random().nextInt(10)+1)*1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
vehicles.add(Road.this.name+":"+i);
}
}
});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); //这是一个调度池
timer.scheduleAtFixedRate(
new Runnable()
{
public void run()
{
if(vehicles.size()>0)
{
boolean ligthed = Lemp.valueOf(Road.this.name).isLight(); //获得路上的灯是不是亮着的
if(ligthed)
{
System.out.println(vehicles.remove(0)+" is running!");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
package com.itcast;
public enum Lemp
{
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);
Lemp(){};
Lemp(String opposite,String next,boolean ligthed)
{
this.opposite = opposite;
this.next = next;
this.ligthed = ligthed;
}
private boolean ligthed;
private String opposite;
private String next;
public void light()
{
System.out.println(name()+"lemp is green , 共有6个方向");
this.ligthed = true;
if(opposite != null)
{
Lemp.valueOf(opposite).light(); //用来取得枚举中对应的值
}
}
/*将这个方法定义成Lemp,我们在调用的时候 既可以把相对的灯搞灭,还能把下一盏灯给点亮的同时获取到下一盏灯*/
public Lemp blackOut()
{
this.ligthed = false;
if(opposite != null)
{
Lemp.valueOf(opposite).blackOut();
}
Lemp nextLemp = null;
if(next != null)
{
nextLemp = Lemp.valueOf(next);
System.out.println("绿灯从"+ name() +"----------> 切换为:"+ next);
nextLemp.light();
}
return nextLemp;
}
public boolean isLight()
{
return ligthed;
}
}
package com.itcast;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LempController
{
private Lemp currentLemp;
public void LempController()
{
currentLemp = Lemp.S2N; //当前的灯
currentLemp.light();
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable()
{
public void run()
{
currentLemp = currentLemp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
package com.itcast;
public class traffic{
public static void main(String[] args)
{
String[] direction =
new String[]{"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","N2W","W2S","S2E","E2N"};
for(int i = 0;i<direction.length;i++)
{
new Road(direction[i]);
}
new LempController();
}
}
帮帮忙啊 ,要不然又睡不着觉了、、
|