在java语言中,join()方法的作用是让调用该方法的线程在执行完run()方法后,再执行join 方法后面的代码。

简单点说就是,将两个线程合并,用于实现同步的功能。

具体而言:可以通过线程A的join() 方法来等待线程A的结束,或者使用线程A的join(1000)方法来等到线程A的结束,但是最多只等待1s.(时间数自己随便改。)

请看如下具体的例子,相信就会明白了

1.最原始的线程

 public class TestJoin {

     public static void main(String[] args) {
Thread t = new Thread(new ThreadImp());
t.start();
try {
//主线程的执行
for(int i=0;i<=5;i++){
System.out.println("当前为主线程,此时i为"+i);
Thread.sleep(1000);
} System.out.println("join Finished");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } class ThreadImp implements Runnable{ @Override
public void run() {
try{
System.out.println("ThreadImp is begining");
Thread.sleep(5000); System.out.println("ThreadImp End"); }catch(InterruptedException e){
e.printStackTrace();
} } }

执行结果为:

当前为主线程,此时i为0
ThreadImp is begining
当前为主线程,此时i为1
当前为主线程,此时i为2
当前为主线程,此时i为3
当前为主线程,此时i为4
ThreadImp End
当前为主线程,此时i为5
join Finished

看以看到,最基本的线程之间来回切换执行。

2.当在主线程中代用 t线程的join方法后(不设置等待时间)

public class TestJoin {

    public static void main(String[] args) {
Thread t = new Thread(new ThreadImp());
t.start();
try {
t.join();//判断t线程是否还在
if(t.isAlive()){
System.out.println("the t has not finished.");
}else{
System.out.println("the t has finished.");
}
//主线程的执行
for(int i=0;i<=5;i++){
System.out.println("当前为主线程,此时i为"+i);
Thread.sleep(1000);
} System.out.println("join Finished");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } class ThreadImp implements Runnable{ @Override
public void run() {
try{
System.out.println("ThreadImp is begining");
Thread.sleep(5000); System.out.println("ThreadImp End"); }catch(InterruptedException e){
e.printStackTrace();
} } }

运行结果为:

ThreadImp is begining
ThreadImp End
the t has finished.
当前为主线程,此时i为0
当前为主线程,此时i为1
当前为主线程,此时i为2
当前为主线程,此时i为3
当前为主线程,此时i为4
当前为主线程,此时i为5
join Finished

可以看出 ,在主线程中调用t.join()方法, 导致t线程结束后再执行后面的主线程。

3. 设置join()等待的时间后

 public class TestJoin {

     public static void main(String[] args) {
Thread t = new Thread(new ThreadImp());
t.start();
try {
t.join(2000);//主线程只等待t线程结束,只等待2秒
//判断t线程是否还在
if(t.isAlive()){
System.out.println("the t has not finished.");
}else{
System.out.println("the t has finished.");
}
//主线程的执行
for(int i=0;i<=5;i++){
System.out.println("当前为主线程,此时i为"+i);
Thread.sleep(1000);
} System.out.println("join Finished");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } class ThreadImp implements Runnable{ @Override
public void run() {
try{
System.out.println("ThreadImp is begining");
Thread.sleep(5000); System.out.println("ThreadImp End"); }catch(InterruptedException e){
e.printStackTrace();
} } }

运行结果:

ThreadImp is begining
the t has not finished.
当前为主线程,此时i为0
当前为主线程,此时i为1
当前为主线程,此时i为2
当前为主线程,此时i为3
ThreadImp End
当前为主线程,此时i为4
当前为主线程,此时i为5
join Finished

可以得到,t线程运行,等待t线程2s时间后,主线程便迫不及待的执行,此时t线程还没结束,然后t线程和主线程交替执行。

最新文章

  1. SAP CRM 使用Javascript触发SAP Server Event
  2. 【转】使用Python matplotlib绘制股票走势图
  3. Google和Baidu常用的搜索技巧--转
  4. Html5 Egret游戏开发 成语大挑战(九)设置界面和声音管理
  5. 【面试题】BD
  6. Java Consumer and Producer demo
  7. mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication解决办法
  8. 在安卓下使用python连接蓝牙串口模块(HC-06)
  9. 安装php时,make步骤报错make: *** [ext/gd/gd.lo] Error 1
  10. WebStorm、IntelliJ IDEA、JetBrains、PhpStorm、RubyMine、PyCharm
  11. Linux下安装单机版zookeeper(和dubbo配合验证)和redis(用图形化界面连接验证)
  12. hdu3340 线段树+多边形
  13. 面向对象,更适合JavaScript
  14. 设计模式系列之过滤器模式(Chriteria Pattern)
  15. P1262 间谍网络
  16. jboos下载地址记录
  17. 【SQL】176. Second Highest Salary
  18. 建立Spring项目的基础
  19. Android stutdio2.2 启动模拟器出现“/dev/kvm is not found.”解决方法
  20. BZOJ2028:[SHOI2009]会场预约(线段树版)

热门文章

  1. 新手村,学会做人选数 https://www.luogu.org/problemnew/show/P1036
  2. ios各个屏幕
  3. jqGrid添加删除功能(不和数据库交互)
  4. 07:清泉-改(prime+堆)
  5. 注解@SuppressWarnings
  6. System.getProperty可以获取的参数
  7. 洛谷2055 [ZJOI2009]假期的宿舍
  8. HDU-2896 病毒侵袭 字符串问题 AC自动机
  9. POJ 1821 Fence(单调队列优化DP)
  10. 学习参考《父与子的编程之旅python【第二版】》高清中文版PDF+高清英文版PDF+源代码