【Java】线程等待 join()方法

join方法的作用是让一个线程等待另一个线程执行结束,再继续执行。
调用t.join的线程会被阻塞,一直阻塞到t线程执行结束为止。

1、主线程调用t.join,被阻塞

public class Demo {  
    public static void main(String[] args) {  
        Thread thread = new Thread(()->{  
            System.out.println("t1进行");  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException e) {  
                throw new RuntimeException(e);  
            }  
        }); 
  
        thread1.start();  
        System.out.println("t开始");  
        try {  
            thread1.join();  
        } catch (InterruptedException e) {  
            throw new RuntimeException(e);  
        }  
        System.out.println("t结束"); 
    }  
}

请添加图片描述

2、t2调用t1.join,t2被阻塞

public class Demo {  
    public static void main(String[] args) {  
        Thread t1 = new Thread(()->{  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException e) {  
                throw new RuntimeException(e);  
            }  
            for (int i = 0; i < 5; i++) {  
                System.out.println("t1进行");  
            }  
        });  
  
        Thread t2 = new Thread(()->{  
            try {  
                t1.join();  
            } catch (InterruptedException e) {  
                throw new RuntimeException(e);  
            }  
            for (int i = 0; i < 5; i++) {  
                System.out.println("t2进行");  
            }  
        });  
  
        t1.start();  
        t2.start();  
        System.out.println("main进行");  
    }  
}

请添加图片描述
这里main线程未被阻塞,反而是t2线程被阻塞,说明调用t.join()方法的线程会被t线程阻塞

3、start和join的位置

只有当调用start之后,join 才会生效

public class Demo {  
    public static void main(String[] args) {  
        Thread thread1 = new Thread(()->{  
            System.out.println("t1进行");  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException e) {  
                throw new RuntimeException(e);  
            }  
        });  
  
        Thread thread2 = new Thread(()->{  
            System.out.println("t2进行");  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException e) {  
                throw new RuntimeException(e);  
            }  
        });  
  
         thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        thread2.start();

        try {
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }  
}

执行结果如下
请添加图片描述
如果代码换一种写法

public class Demo {
    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                System.out.println("t1进行");
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread thread2 = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                System.out.println("t2进行");
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        thread1.start();
        thread2.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}

那么t1线程和t2线程的执行顺序依然是随机的请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙河板混

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值