Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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()方法
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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()方法。
以上这篇基于多线程中join()的用法实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/xiangpeng/p/7656780.html
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-05 105
-
2025-06-04 61
-
Java中HashTable和HashMap的区别_动力节点Java学院整理
2025-05-29 59 -
2025-06-04 73
-
Adobe Reader因不再吸引Linux用户决定退出Linux系统
2025-05-27 13
热门评论



