91名师指路-头部
91名师指路

跟着波波老师学多线程高并发(五)多线程中join方法练习题

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

题目:现在有T1, T2, T3 三个线程,如何保证T2在T1执行完成后执行,T3在T2执行完成后执行。


代码如下:

package com.mszl.controller;

/**
* 功能:多线程中的join方法
* 更多资料请访问http://www.91mszl.com
* @author bobo teacher
*/
public class ThreadDemo03 {

public static void main(String[] args) throws InterruptedException {

final Thread t1=new Thread(new Runnable() {
public void run() {
for(int i=0; i<10; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T1线程:" + i);
}
}
});
t1.start();

final Thread t2=new Thread(new Runnable() {
public void run() {
try {
t1.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int i=0; i<10; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T2线程:" + i);
}
}
});
t2.start();

Thread t3=new Thread(new Runnable() {
public void run() {
try {
t2.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int i=0; i<10; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T3线程:" + i);
}
}
});
t3.start();

}

}


执行结果:


2019-11-19 18:23:37     阅读(860)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部