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++;
}
}
}
}
|
|