A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 常佳杰 中级黑马   /  2012-6-29 23:13  /  1891 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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:53

2 个回复

倒序浏览
楼主漏写了吧,估计是太晚了,累了,呵呵。
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
回复 使用道具 举报
唉...脑子糊了..谢了,嘿嘿老师我也感觉..
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马