packagecom.wp.join;public classJoinTest2 {private static int num = 0;static class JoinThreadA extendsThread {
@Overridepublic voidrun() {while (true) {
num++;long now =System.currentTimeMillis();while (System.currentTimeMillis() - now < 200) {
}
System.out.println("选手A已跑" + num + "米");if (num >= 100) {break;
}
}
}
}static class JoinThreadB extendsThread {protectedJoinThreadA joina;publicJoinThreadB(JoinThreadA joina) {this.joina =joina;
}
@Overridepublic voidrun() {try{
joina.join();
}catch(InterruptedException e) {
e.printStackTrace();
}while (true) {
num++;long now =System.currentTimeMillis();while (System.currentTimeMillis() - now < 100) {
}
System.out.println("选手B已跑" + num + "米");if (num >= 200) {break;
}
}
}
}static class JoinThreadC extendsThread {protectedJoinThreadB joinb;publicJoinThreadC(JoinThreadB joinb) {this.joinb =joinb;
}
@Overridepublic voidrun() {try{
joinb.join();
}catch(InterruptedException e) {
e.printStackTrace();
}while (true) {
num++;long now =System.currentTimeMillis();while (System.currentTimeMillis() - now < 50) {
}
System.out.println("选手C已跑" + num + "米");if (num >= 300) {break;
}
}
}
}static class JoinThreadD extendsThread {protectedJoinThreadC joinc;publicJoinThreadD(JoinThreadC joinc) {this.joinc =joinc;
}
@Overridepublic voidrun() {try{
joinc.join();
}catch(InterruptedException e) {
e.printStackTrace();
}while (true) {
num++;long now =System.currentTimeMillis();while (System.currentTimeMillis() - now < 20) {
}
System.out.println("选手D已跑" + num + "米");if (num >= 400) {break;
}
}
}
}public static void main(String[] args) throwsException {
System.out.println("比赛开始");
JoinThreadA joina= newJoinThreadA();
JoinThreadB joinb= newJoinThreadB(joina);
JoinThreadC joinc= newJoinThreadC(joinb);
JoinThreadD joind= newJoinThreadD(joinc);
joina.start();
joinb.start();
joinc.start();
joind.start();
joind.join();
System.out.println("比赛结束,最终跑完" + num + "米");
}
}