* 编写多线程应用程序,模拟多个人通过山洞。这个山洞每次只能通过一个人,每个人
* 通过山洞的时间为5秒,随机生成10个人,名字分别是"ONE"~"TEN",同时准备过此山洞,
* 显示一下每次通过山洞人的姓名。
* 提示:1.获取当前线程名字可用Thread.currentThread().getName()方法
* 2.输出结果应该是乱序的*/
public class Pass implements Runnable{
private Object lock=new Object();
public void run(){
synchronized(lock){
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
Pass p=new Pass();
new Thread(p,"ONE").start();
new Thread(p,"TWO").start();
new Thread(p,"THREE").start();
new Thread(p,"FOUR").start();
new Thread(p,"FIVE").start();
new Thread(p,"SIX").start();
new Thread(p,"SEVEN").start();
new Thread(p,"EIGHT").start();
}
}