自己写的简易交通灯管理系统,请大侠们指教
package trafficlmp;
public enum NextLamp {
GREEN("RED"),RED("YELLOW"),YELLOW("GREEN");
private NextLamp(String next){
this.next = next;
}
private String next;
public NextLamp nextLamp(){
NextLamp nextlamp = null;
if( next!=null){
nextlamp = NextLamp.valueOf(next);
//System.out.println("绿灯从"+nextlamp+"切换为"+next);
}
return nextlamp;
}
}
package trafficlmp;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class NextLampMain {
private NextLamp currentLamp;
private NextLampMain(){
currentLamp = NextLamp.GREEN;
ScheduledExecutorService timer =
Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
currentLamp = currentLamp.nextLamp();
System.out.println(currentLamp+"亮了");
}
},
1,
1,
TimeUnit.SECONDS);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new NextLampMain();
}
}
|