java 多线程 Thread.join子线程结束父线程再运行;join(long):等待超时毫秒数

Join的使用

目的:当子线程运行结束后,父线程才能再继续运行

复制代码
/**
 * @ClassName ThreadJoinExample
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/24.
 */
public class ThreadJoinExample {
    public static void main(String[] args) throws InterruptedException {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String tName = Thread.currentThread().getName();
                try {
                    System.out.println(tName + "开始运行");
                    Thread.sleep(4000);
                    System.out.println(tName +"运行结束!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(r,"子任务1");
        Thread thread2 = new Thread(r,"子任务2");
        thread.start();
        thread2.start();
        thread.join(); //子线程阻塞主线程,待子线程运行结束
        thread2.join(); //子线程阻塞主线程,待子线程运行结束
        Thread.sleep(1000);
        System.out.println("主线程运行结束!");
    }
}
复制代码

 不能interrupt中断一个有join()子线程的父线程

复制代码
/**
 * @ClassName ThreadInterruptJoinThread
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/24.
 */
public class ThreadInterruptJoinThread {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread("parent"){
            @Override
            public void run() {
                System.out.println(getName() + "开始运行");
                Thread thread1 = new Thread("child"){
                    @Override
                    public void run() {
                        System.out.println(getName() + "开始运行");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(getName() + "运行结束");
                    }
                };
                thread1.start();
                //thread1.join();
                try {
                    thread1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName() + "运行结束");
            }
        };
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
        thread.join();
        System.out.println("主线程运行结束");
    }
}
复制代码

如上代码执行结果:join的child子线程还没结束,父线程就被interrupt中断,会导致父线程异常,且不再等待子线程child运行结束。

 

join(long):等待指定时间不结束就不等待了

复制代码
/**
 * @ClassName ThreadJoinTime
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/24.
 */
public class ThreadJoinTime {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread("child"){
            @Override
            public void run() {
                System.out.println("子线程开始运行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程运行结束");
            }
        };
        thread.start();
        thread.join(1000);
        System.out.println("主线程代码运行结束");
    }
}
复制代码

 

posted on   zhangmingda  阅读(190)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-04-24 js Date()获取时间,格式化输出,时间比较大小
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示