------- android培训、java培训、期待与您交流! ----------
一、需求
package com.isoftstone.interview.traffic;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Road
{
//存储这条路线车辆的集合
private List vehicles=new ArrayList();
private String name =null;
public Road( String name)
{
this.name = name;
//下面开启一个线程,功能是:1-10秒出现一辆车,接着添加进集合。
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
@Override
public void run()
{
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++)
{
try
{
Thread.sleep((new Random().nextInt(10)+1)*1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
vehicles.add(Road.this.name+"_"+i);//访问外部类的成员变量:外部类名.this.变量名
}
}
});
//下面定义一个调度线程,固定频率,移除集合中的首辆车,返回:路名+编号
ScheduledExecutorService timer= Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
@Override
public void run()
{
// TODO Auto-generated method stub
//Lamp lamp = Lamp.valueOf(direction);
if(vehicles.size()>0)
{
//这条路上的灯是否是亮的。
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if (lighted)//有车,绿灯,则通行
{
System.out.println(vehicles.remove(0)+"is traversing!");
}
}
}},
1,
1,
TimeUnit.SECONDS
);
}
}
灯:
package com.isoftstone.interview.traffic;
public enum Lamp
{ //只有第一行四个灯是重要的,南向上、向左。东向上、向左。只要控制好这四个灯的循环,所有灯就被控制了。
//第一行第一个参数给String,可以转成Lamp。直接Lamp类型不行,因为作为参数的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 boolean lighted;//灯的状态,true为红,false为暗
private String opposite;//对面的灯。类型为String可以恢复为Lamp,
private String next;//下一个灯。
public boolean isLighted() //灯是否亮?
{
return lighted;
}
public void light()//变绿方法
{
this.lighted = true;
if(opposite != null)//对面的灯为null,不能有对应的灯接着回来找我了,不让会死循环
{
Lamp.valueOf(opposite).light();//对面也跟着变绿
}
System.out.println(name()+"lamp is green,下面共应该有6个方向能看到汽车通过!");
}
public Lamp blackOut()//变红方法。
{
this.lighted = false;
if(opposite != null)//对面的灯为null,不能有对应的灯接着回来找我了,不让会死循环
{
Lamp.valueOf(opposite).blackOut();//对面也跟着变红
}
//当前灯灭,则把下一个灯变亮作为当前灯,返回。
//Lamp nextLamp = Lamp.valueOf(next);
Lamp nextLamp = null;
if(next != null)
{
nextLamp = Lamp.valueOf(next);
//Lamp.valueOf(next).light();
System.out.println("绿灯从"+name()+"---->切换为"+next);
nextLamp.light();
}
return nextLamp;
}
}
灯控制器类
package com.isoftstone.interview.traffic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LampController
{
private Lamp currentLamp;
public LampController()
{
//初始化灯并且为绿(亮light)
currentLamp = Lamp.S2N;
currentLamp.light();
//建一个定时器,每过一段时间,让当前的灯变红,下一个灯变绿。
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run()
{
//当前灯变红,返回值为下一个灯,下一个灯作为当前的灯。
System.out.println("来啊");
currentLamp = currentLamp.blackOut();
}},
10,
10,
TimeUnit.SECONDS);
}
}
MainClass
package com.isoftstone.interview.traffic;
public class MainClass
{
/**
* @param args
*/
public static void main(String[] args)
{
//创建12条路线对象,和1个控制器。
/* 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);*/
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 LampController();
}
}
------- android培训、java培训、期待与您交流! ----------