public class GuoShanDong {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("赵");
list.add("钱");
list.add("孙");
list.add("李");
list.add("周");
list.add("吴");
list.add("郑");
list.add("王");
list.add("上官");
list.add("慕容");
//打乱集合
Collections.shuffle(list);
/*
//一个线程
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(5);
System.out.println(list.get(i) + "过山洞了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
*/
//10个线程
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (GuoShanDong.class){
System.out.println(Thread.currentThread().getName() + "过山洞了");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, list.get(i) + "").start();
}
}
}
十个人随机过山洞,每过一个,休眠5秒
最新推荐文章于 2019-09-15 20:27:54 发布