本帖最后由 走遍世界找寻你 于 2013-11-17 20:42 编辑
package itcast.interview.traffic;
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;
public class Road {
private List<String> vechicles = new ArrayList<String>();
private String name=null;
public Road(final String name){
this.name = name;
//模拟车辆不断随机上路的情况
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)*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
vechicles.add(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 travelsing !");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
请问第十行name前如果不加不加final该怎么改才对?
|