????????????
????Road?????????·?????????????????
<span style="font-size:14px;">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;
/**
* ???Road??????????·????????12??·?????????????????12??Road???????
* ???·????????????μ???????????????????б??檔
* ???·????????????????·?????????????????·????泵??????е?????????????????????????·???
*
*/
public class Road {
List<String> vechicles = new  ArrayList<String>();
private String name;
public Road(String name){
this.name= name;
//??????????????·?????
//???????ExecutorService????
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
public void run(){
for(int i=1;i<1000;i++){
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
vechicles.add(Road.this.name + ":" + i);
}
}
});
//????????????????????????????????
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
if(vechicles.size()>0){
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if(lighted){
System.out.println(vechicles.remove(0) + " is traversing!");
}
}
}
}??
1??
1??
TimeUnit.SECONDS);
}
}</span>
????Lamp???????????????????????????????????????????????????
<span style="font-size:14px;">/**
* ???Lamp???????????????????????12???????????????12??Lamp????
* ???????Щ???????????????γ???飬??????????????????
* ????????????????????е??????????
* s2n??n2s
* s2w??n2e
* e2w??w2e
* e2s??w2n
* s2e??n2w
* e2n??w2s
* ????????е?????????????????????????????????????????????????????
* ????????????????????????
*
*/
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 Lamp(String opposite??String next??boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
private Lamp(){
}
/*???????????*/
private boolean lighted;
/*???????????????????*/
private String next;
/*?????????????????*/
private String opposite;
public boolean isLighted(){
return lighted;
}
/**
* ????????????????????????????
*/
public void light(){
this.lighted = true;
if(opposite != null){
Lamp.valueOf(opposite).light();
}
//6?4????????????????????
System.out.println(name() + "lamp is green??????????????6???????????????????");
}
/**
* ????????????????????????????????????????????
* @return ????????????
*/
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;
}
}</span>