多线程----sleep
程序员文章站
2022-05-02 12:52:49
...
package java.thread;
/**
* Created by Administrator on 2017/7/27.
*/
public class ThreadDemo2 {
public static void main(String[] args) throws InterruptedException {
//庄家
Player p1 = new Player("成龙",2) ;
Player p2 = new Player("李小龙",5) ;
Player p3 = new Player("狄龙",3) ;
Player p4 = new Player("马龙",4) ;
p1.start();
p2.start();
p3.start();
p4.start();
// try {
p1.join();
p2.join();
p3.join();
p4.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println("开局!!!");
}
}
/**
* 玩家
*/
class Player extends Thread{
private String name0 ;
private int sleep ;
public Player(String name0,int sleep) {
this.name0 = name0 ;
this.sleep = sleep ;
}
public String getName0() {
return name0;
}
public void setName0(String name0) {
this.name0 = name0;
}
public void run() {
try {
System.out.println(name0 + " : 出发了!!!");
Thread.sleep(sleep * 1000);
System.out.println(name0 + " : 到了 , 耗时 : " + sleep + "秒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
上一篇: 朴素贝叶斯(原理与实现)