public class Road {
List<String> vehicle = new ArrayList<String>();
private String name;
public Road(String name){
this.name = name;
//设计定时器(调度池)对车辆放行,每隔一秒发生一次。
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleAtFixedRate(new Runnable(){
public void run() {
if(vehicle.size()>0){
boolean lighted = TrafficLamp.valueOf(Road.this.name).isLight();
if(lighted){
System.out.println(vehicle.remove(0)+"已经通过路口");
}
}
}
},
100,
10,
TimeUnit.MILLISECONDS);
}
}
Road.this.name,此处按照eclipse的提示,可以将String name加上final,此处为什么不能加fianl而要用Road.this.name这个来引用?好像改了之后运行结果也没什么不一样。
上面写法和这样写public Road(final String name){....name....}有什么区别? |