黑马程序员技术交流社区

标题: 10号点招题 [打印本页]

作者: dd6434541    时间: 2016-5-10 23:47
标题: 10号点招题
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++;
                        }
                }
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2