Java Thread join()的使用场景和原理详解

 更新时间:2025年09月05日 10:17:58   作者:干净的坏蛋  
在Java编程语言中,Thread.join()方法是一个非常重要的同步工具,它允许一个线程(调用者)等待另一个线程(被调用者)执行完成,这篇文章主要介绍了Java Thread join()的使用场景和原理的相关资料,需要的朋友可以参考下

1、使用场景

一般情况下,主线程创建并启动子线程,如果子线程中执行大量耗时运算,主线程可能早于子线程结束。如果主线程需要知道子线程的执行结果时,就需要等待子线程执行结束。主线程可以sleep(xx),但这样的xx时间不好确定,因为子线程的执行时间不确定,join()方法比较合适这个场景。

2、join()方法定义和使用

join()是Thread类的一个方法。根据jdk文档的定义:

public final void join()throws InterruptedException: Waits for this thread to die.

join()方法的作用,是等待这个线程结束;这个定义比较模糊,个人认为"Java 7 Concurrency Cookbook"的定义较为清晰:

Waiting for the finalization of a thread

In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. 
We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. 
For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.

解释一下,是主线程等待子线程的终止。也就是说主线程的代码块中,如果碰到了t.join()方法,此时主线程需要等待(阻塞),等待子线程结束了(Waits for this thread to die.),才能继续执行t.join()之后的代码块。

来看一个join()的案例demo:

public class JoinDemo {

    public static void main(String[] args) throws InterruptedException {
        //获取当前线程信息
       Thread previousThread= Thread.currentThread();
        for(int i=0;i<10;i++){
            Thread thread=new Thread(new Domino(previousThread));
            thread.start();
            previousThread=thread;
        }
        TimeUnit.SECONDS.sleep(5);
        System.out.println("Thread.currentThread().getName()+\" terminate.\" = " + Thread.currentThread().getName()+" terminate.");

    }

    static class Domino implements Runnable{
        private Thread thread;
        public Domino(Thread thread){
            this.thread=thread;
        }
        @Override
        public void run() {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " terminate.");
        }
    }
}

这段demo的逻辑就是当前线程等待它前面的线程执行结束再执行,输出如下:

Thread.currentThread().getName()+" terminate." = main terminate.
Thread-0 terminate
Thread-1 terminate
Thread-2 terminate
Thread-3 terminate
Thread-4 terminate
Thread-5 terminate
Thread-6 terminate
Thread-7 terminate
Thread-8 terminate
Thread-9 terminate

3、源码分析

public final void join() throws InterruptedException {
    join(0);
}

再看join(long millis)方法:

public final synchronized void join(long millis) throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

isAlive() 是 Thread 内部的一个 native 方法,只要当线程处于 NEW 和 TERMINATED 状态时返回false,其余都返回true。join() 其实就是通过调用 wait() 方法(Object定义的,详见线程间通信),wait(0) 表示当前线程立即释放锁(这里的锁指的就是前一个线程)并进入 waiting 状态,等待其他线程唤醒(notify/notifyAll)。

join() 一共有三个重载版本,分别是无参、一个参数、两个参数:

public final void join() throws InterruptedException;

 public final synchronized void join(long millis) throws InterruptedException;
 
 public final synchronized void join(long millis, int nanos) throws InterruptedException;

(1) 三个方法都被final修饰,无法被子类重写。

(2) join(long), join(long, long) 是synchronized method,同步的对象是当前线程实例。

(3) join() 和 join(0) 是等价的,表示一直等下去;join(非0)表示等待一段时间。

从源码可以看到 join(0) 调用了Object.wait(0),其中Object.wait(0) 会一直等待,直到被notify/中断才返回。

while(isAlive())是为了防止子线程伪唤醒(spurious wakeup),只要子线程没有TERMINATED的,父线程就需要继续等下去。

(4) join() 和 sleep() 一样,可以被中断(被中断时,会抛出 InterrupptedException 异常);不同的是,join() 内部调用了 wait(),会出让锁,而 sleep() 会一直保持锁。

4、join() 方法注意点

4.1、join() 与 start() 的调用顺序

package com.dxz.join;
class BThread extends Thread {
    public BThread() {
        super("[BThread] 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");
        }
    }
}

public class TestDemo {
    public static void main(String[] args) {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start.");
        BThread bt = new BThread();
        try {
            bt.join();
            bt.start();
            Thread.sleep(2000);
        } catch (Exception e) {
            System.out.println("Exception from main");
        }
        System.out.println(threadName + " end!");
    }
}

执行结果:

 main start.
[BThread] Thread start.
[BThread] Thread loop at 0
[BThread] Thread loop at 1
main end!
[BThread] Thread loop at 2
[BThread] Thread loop at 3
[BThread] Thread loop at 4
[BThread] Thread end.

main线程没有等待 [BThread] 线程执行完再执行。join方法必须在线程start方法调用之后调用才有意义。这个也很容易理解:线程没有执行,isAlive() 方法就不会返回true,也就不会执行 wait(0) 方法了。

4.2、join() 与中断异常

在join()过程中,如果当前线程被中断,则当前线程出现异常。(注意是调用thread.join()的线程被中断才会进入异常,比如a线程调用b.join(),a中断会报异常而b中断不会异常)

下面总结下中断与 wait()/join()/sleep() 的使用:

调用interrupt()方法,立刻改变的是中断状态,但如果不是在阻塞态,就不会抛出异常;如果在进入阻塞态后,中断状态为已中断,就会立刻抛出异常,但同时中断标志也会被系统复位。

(1)sleep() &interrupt()

线程A正在使用sleep()暂停着: Thread.sleep(100000),如果要取消它的等待状态,可以在正在执行的线程里(比如这里是B)调用a.interrupt()[a是线程A对应到的Thread实例],令线程A放弃睡眠操作。即,在线程B中执行a.interrupt(),处于阻塞中的线程a将放弃睡眠操作。

当在sleep中时线程被调用interrupt()时,就马上会放弃暂停的状态并抛出InterruptedException。抛出异常的,是A线程。

(2)wait() &interrupt()

线程A调用了wait()进入了等待状态,也可以用interrupt()取消。不过这时候要注意锁定的问题。线程在进入等待区,会把锁定解除,当对等待中的线程调用interrupt()时,会先重新获取锁定,再抛出异常。在获取锁定之前,是无法抛出异常的。

(3)join() &interrupt()

当线程以join()等待其他线程结束时,当它被调用interrupt(),它与sleep()时一样,会马上跳到catch块里.。

注意,调用的interrupt()方法,一定是调用被阻塞线程的interrupt方法。如在线程a中调用线程t.interrupt()。

总结

到此这篇关于Java Thread join()的使用场景和原理的文章就介绍到这了,更多相关Java Thread join()使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Nacos快速安装部署教程

    Nacos快速安装部署教程

    文章简要介绍Nacos作为阿里巴巴开源的微服务管理组件,涵盖其核心功能及单机模式部署步骤:下载稳定版、配置MySQL、执行数据库脚本、启动服务并查看日志,最后通过指定地址和账号登录控制台
    2025-07-07
  • springboot过滤器执行两次的解决及跨域过滤器问题

    springboot过滤器执行两次的解决及跨域过滤器问题

    这篇文章主要介绍了springboot过滤器执行两次的解决及跨域过滤器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • 详解JAVA 字节流和字符流

    详解JAVA 字节流和字符流

    这篇文章主要介绍了JAVA 字节流和字符流的的相关资料,文中讲解非常的细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • Spring Security实现5次密码错误触发账号自动锁定功能

    Spring Security实现5次密码错误触发账号自动锁定功能

    在现代互联网应用中,账号安全是重中之重,然而,暴力 破解攻击依然是最常见的安全威胁之一,攻击者通过自动化脚本尝试大量的用户名和密码组合,试图找到漏洞进入系统,所以为了解决这一问题,账号锁定机制被广泛应用,本文介绍了Spring Security实现5次密码错误触发账号锁定功能
    2024-12-12
  • SpringBoot+logback默认日志的配置和使用方式

    SpringBoot+logback默认日志的配置和使用方式

    这篇文章主要介绍了SpringBoot+logback默认日志的配置和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • Java后端向前端返回文件流实现下载功能的方法

    Java后端向前端返回文件流实现下载功能的方法

    这篇文章主要给大家介绍了关于Java后端向前端返回文件流实现下载功能的相关资料,Java后端可以通过调用接口返回文件流来实现文件传输功能,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-10-10
  • Java实战之实现用户登录

    Java实战之实现用户登录

    这篇文章主要介绍了Java实战之实现用户登录,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • mybatisplus之Wrappers.ne踩坑记录解决

    mybatisplus之Wrappers.ne踩坑记录解决

    这篇文章主要为大家介绍了mybatisplus之Wrappers.ne踩坑记录解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Spring Boot 实例代码之通过接口安全退出

    Spring Boot 实例代码之通过接口安全退出

    这篇文章主要介绍了Spring Boot 实例代码之通过接口安全退出的相关资料,需要的朋友可以参考下
    2017-09-09
  • Java虚拟机GC的各种缺点汇总

    Java虚拟机GC的各种缺点汇总

    Java通过垃圾收集器(Garbage Collection,简称GC)实现自动内存管理,这样可有效减轻Java应用开发人员的负担,也避免了更多内存泄露的风险,这篇文章主要介绍了Java虚拟机GC的种种缺点,感兴趣的朋友一起看看吧
    2025-05-05

最新评论