public class Road {
private List<String> vechicles = new ArrayList<String>();
private String name = null;
public Road(String name) {
this.name = name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable() {
@Override
public void run() {
for (int i = 1; i < 1000; i++) {
try {
Thread.sleep((new Random.nextInt(10) + 1) * 1000);//怎么这总是提示错误Random.nextInt cannot be resolved to a type
} catch (InterruptedException ex) {
Logger.getLogger(Road.class.getName()).log(
Level.SEVERE, null, ex);
}
vechicles.add(Road.this.name + "_" + i);
}
}
});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (vechicles.size() > 0) {
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if (lighted == true) {
System.out.println(vechicles.remove(0)
+ "is traversing !");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
|
|