黑马程序员技术交流社区
标题:
交通灯里的Road类里的小问题
[打印本页]
作者:
常佳杰
时间:
2012-6-29 23:13
标题:
交通灯里的Road类里的小问题
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);
}
}
作者:
谭立文
时间:
2012-6-30 00:43
楼主漏写了吧,估计是太晚了,累了,呵呵。
Thread.sleep((new Random
()
.nextInt(10) + 1) * 1000);
作者:
常佳杰
时间:
2012-6-30 07:24
唉...脑子糊了..谢了,嘿嘿老师我也感觉..
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2