java---多线程(模拟多个人通过一个山洞的场景)

java—多线程(模拟多个人通过一个山洞的场景)

编写多线程程序,模拟多个人通过一个山洞的场景。这个山洞每次只能通过一个人,每个人通过山洞的时间为5秒,有10个人同时准备过此山洞,显示每次通过山洞的人的姓名和顺序。

代码演示:
public class a {

public static void main(String[] args) {
// TODO Auto-generated method stub
b peothread=new b();
new Thread(peothread,"第一个人").start();
new Thread(peothread,"第二个人").start();
new Thread(peothread,"第三个人").start();
new Thread(peothread,"第四个人").start();
new Thread(peothread,"第五个人").start();
new Thread(peothread,"第六个人").start();
new Thread(peothread,"第七个人").start();
new Thread(peothread,"第八个人").start();
new Thread(peothread,"第九个人").start();
new Thread(peothread,"第十个人").start();
//new Thread(peothread,"第一个人");
}

}
package a;

public class b extends Thread {
private int woor=1;
public void run()
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName()+"通过了山洞!!!");
}

}
发布了25 篇原创文章 · 获赞 3 · 访问量 316

猜你喜欢

转载自blog.csdn.net/weixin_45686974/article/details/103285414