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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© dd6434541 中级黑马   /  2016-5-10 23:47  /  254 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test2 {
        public static void main(String[] args) {
                YuDong yd = new YuDong();
                new Thread(yd, "zs").start();
                new Thread(yd, "ls").start();
        }
}

class YuDong implements Runnable {
        private String str[] = { "爬山", "游泳", "羽毛球", "乒乓", "网球" };
        private int i = str.length;//随机函数的限制。
        private int x = 0; //循环次数
        public void run() {
                while (true) {
                        synchronized (YuDong.class) {
                                if (x >= 5) {
                                        break;
                                }
                               
                                Random r = new Random();
                                int index = r.nextInt(i);
                                System.out.println(Thread.currentThread().getName() + "要去" + str[index]);
                                x++;
                        }
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马