Java 多线程之join

Java多线程之join

1.join方法只有在继承了Thread类的线程中才有。

2.线程必须要start()后再join才能起作用。

JDK的解释join public final void joinlong millisthrows InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.

字面意思是等待一段时间直到这个线程死亡


为什么要用join()方法

    join的用法, 在某些情况下,如果子线程里要进行大量的耗时的运算,主线程可能会在子线程执行完之前结束,但是如果主线程又需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()。

使用实例:

写一个简单的例子来看一下join()的用法,一共三个类:

1.CustomThread

2. CustomThread1

3. JoinTestDemo 类,main方法所在的类。


代码1

package wxhx.csdn2;  
class CustomThread1 extends Thread {  
    public CustomThread1() {  
        super("[CustomThread1] Thread");  
    };  
    public void run() {  
        String threadName = Thread.currentThread().getName();  
        System.out.println(threadName + " start.");  
        try {  
            for (int i = 0; i < 5; i++) {  
                System.out.println(threadName + " loop at " + i);  
                Thread.sleep(1000);  
            }  
            System.out.println(threadName + " end.");  
        } catch (Exception e) {  
            System.out.println("Exception from " + threadName + ".run");  
        }  
    }  
}  
class CustomThread extends Thread {  
    CustomThread1 t1;  
    public CustomThread(CustomThread1 t1) {  
        super("[CustomThread] Thread");  
        this.t1 = t1;  
    }  
    public void run() {  
        String threadName = Thread.currentThread().getName();  
        System.out.println(threadName + " start.");  
        try {  
            t1.join();  
            System.out.println(threadName + " end.");  
        } catch (Exception e) {  
            System.out.println("Exception from " + threadName + ".run");  
        }  
    }  
}  
public class JoinTestDemo {  
    public static void main(String[] args) {  
        String threadName = Thread.currentThread().getName();  
        System.out.println(threadName + " start.");  
        CustomThread1 t1 = new CustomThread1();  
        CustomThread t = new CustomThread(t1);  
        try {  
            t1.start();  
            Thread.sleep(2000);  
            t.start();  
            t.join();//在代碼2里,將此處注釋掉  
        } catch (Exception e) {  
            System.out.println("Exception from main");  
        }  
        System.out.println(threadName + " end!");  
    }  
}  

运行结果:

main start.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。

[CustomThread1] Thread start.//线程CustomThread1起动

[CustomThread1] Thread loop at 0//线程CustomThread1执行

[CustomThread1] Thread loop at 1//线程CustomThread1执行

[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。

[CustomThread1] Thread loop at 2//线程CustomThread1继续执行

[CustomThread1] Thread loop at 3//线程CustomThread1继续执行

[CustomThread1] Thread loop at 4//线程CustomThread1继续执行

[CustomThread1] Thread end. //线程CustomThread1结束了

[CustomThread] Thread end.// 线程CustomThreadt1.join();阻塞处起动,向下继续执行的结果

main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。

代码2:

public class JoinTestDemo {  
    public static void main(String[] args) {  
        String threadName = Thread.currentThread().getName();  
        System.out.println(threadName + " start.");  
        CustomThread1 t1 = new CustomThread1();  
        CustomThread t = new CustomThread(t1);  
        try {  
            t1.start();  
            Thread.sleep(2000);  
            t.start();  
//          t.join();//在代碼2里,將此處注釋掉  
        } catch (Exception e) {  
            System.out.println("Exception from main");  
        }  
        System.out.println(threadName + " end!");  
    }  
}
运行结果:

main start. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);

[CustomThread1] Thread start. //线程CustomThread1起动

[CustomThread1] Thread loop at 0//线程CustomThread1执行

[CustomThread1] Thread loop at 1//线程CustomThread1执行

main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)

[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。

[CustomThread1] Thread loop at 2//线程CustomThread1继续执行

[CustomThread1] Thread loop at 3//线程CustomThread1继续执行

[CustomThread1] Thread loop at 4//线程CustomThread1继续执行

[CustomThread1] Thread end. //线程CustomThread1结束了

[CustomThread] Thread end. // 线程CustomThreadt1.join();阻塞处起动,向下继续执行的结果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值