最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
基于多线程中join()的用法实例讲解
时间:2022-06-29 01:16:39 编辑:袖梨 来源:一聚教程网
Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行。
public class TestThread5 {
public static void main(String[] args) throws InterruptedException {
Runner0 run5 = new Runner0();
Thread th5 = new Thread(run5);
th5.start();
th5.join();//join()方法用在此处是为了等待主线程结束后运行子线程
for(int i=0;i<5;i++){
System.out.println("子线程:"+i);
}
}
}
class Runner0 implements Runnable{
public void run(){
for(int i=0;i<5;i++)
System.out.println("主线程:"+i);
}
}
上述代码的运行结构如下所示:
当然,如果不使用join()方法
public class TestThread6{
public static void main(String[] args) throws InterruptedException {
Runner0 run5 = new Runner0();
Thread th5 = new Thread(run5);
th5.start();
// th5.join();
for(int i=0;i<4;i++){
System.out.println("子线程:"+i);
}
}
}
class Runner0 implements Runnable{
public void run(){
for(int i=0;i<4;i++)
System.out.println("主线程:"+i);
}
}
如上代码注释掉jion()方法,
根据上面两个不同的代码,输出的不同,很容易就能理解join()方法。
相关文章
- 抖音官方充值入口-抖音充值活动任务奖励领取入口 12-18
- 哔哩哔哩在线免费畅看-2025哔哩哔哩b站网页版最新入口速览 12-18
- 一人之下漫画免费在线观看入口 | 实时缓存无延迟,网页直读免下载 12-18
- 免费PPT成品网站直播推荐-超全免费PPT模板网站盘点 12-18
- 学信网查学历-官方学历查询入口(一键验证学历信息) 12-18
- 虫虫漫画-免费漫画观看入口_海量正版漫画在线畅读平台 12-18

