并发编程之join 方法的详细解析

3.8 join 方法详解

为什么需要 join

下面的代码执行,打印 r 是什么?

static int r = 0;
public static void main(String[] args) throws InterruptedException {
    test1();
}
private static void test1() throws InterruptedException {
    log.debug("开始");
    Thread t1 = new Thread(() -> {
        log.debug("开始");
        sleep(1);
        log.debug("结束");
        r = 10;
    });
    t1.start();
    log.debug("结果为:{}", r);
    log.debug("结束");
}

分析

  • 因为主线程和线程 t1 是并行执行的,t1 线程需要 1 秒之后才能算出 r=10

  • 而主线程一开始就要打印 r 的结果,所以只能打印出 r=0

解决方法

  • 用 sleep 行不行?为什么?

    可以,但是不推荐,不知道要sleep多久

  • 用 join,加在 t1.start() 之后即可

应用之同步(案例)

以调用方角度来讲,如果

  • 需要等待结果返回,才能继续运行就是同步

  • 不需要等待结果返回,就能继续运行就是异步

等待多个结果

问,下面代码 cost 大约多少秒?

static int r1 = 0;
static int r2 = 0;
public static void main(String[] args) throws InterruptedException {
    test2();
}
private static void test2() throws InterruptedException {
    Thread t1 = new Thread(() -> {
        sleep(1);
        r1 = 10;
    });
    Thread t2 = new Thread(() -> {
        sleep(2);
        r2 = 20;
    });
    long start = System.currentTimeMillis();
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    long end = System.currentTimeMillis();
    log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);
}  

分析如下

  • 第一个 join:等待 t1 时, t2 并没有停止, 而在运行

  • 第二个 join:1s 后, 执行到此, t2 也运行了 1s, 因此也只需再等待 1s

如果颠倒两个 join 呢?

最终都是输出

20:45:43.239 [main] c.TestJoin - r1: 10 r2: 20 cost: 2005

有时效的join

当线程执行时间没有超过join设定时间

static int r1 = 0;
static int r2 = 0;
public static void main(String[] args) throws InterruptedException {
    test3();
}
public static void test3() throws InterruptedException {
    Thread t1 = new Thread(() -> {
        sleep(1);
        r1 = 10;
    });
    long start = System.currentTimeMillis();
    t1.start();
    // 线程执行结束会导致 join 结束
    t1.join(1500);
    long end = System.currentTimeMillis();
    log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);
}

输出

20:48:01.320 [main] c.TestJoin - r1: 10 r2: 0 cost: 1010

当执行时间超时

static int r1 = 0;
static int r2 = 0;
public static void main(String[] args) throws InterruptedException {
    test3();
}
public static void test3() throws InterruptedException {
    Thread t1 = new Thread(() -> {
        sleep(2);
        r1 = 10;
    });
    long start = System.currentTimeMillis();
    t1.start();
    // 线程执行结束会导致 join 结束
    t1.join(1500);
    long end = System.currentTimeMillis();
    log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);
}

输出

20:52:15.623 [main] c.TestJoin - r1: 0 r2: 0 cost: 1502

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-17 06:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-17 06:22:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-17 06:22:02       82 阅读
  4. Python语言-面向对象

    2024-03-17 06:22:02       91 阅读

热门阅读

  1. 在一个程序页面中加几个字段用于增删改查

    2024-03-17 06:22:02       35 阅读
  2. 大模型近1年的发展所思

    2024-03-17 06:22:02       35 阅读
  3. iguana 库 C++ 反射原理

    2024-03-17 06:22:02       41 阅读
  4. web蓝桥杯真题:搜一搜呀

    2024-03-17 06:22:02       40 阅读
  5. Python自动化测试之使用pytest-mock模拟用户输入

    2024-03-17 06:22:02       37 阅读
  6. ubuntu 安装配置 ollama

    2024-03-17 06:22:02       39 阅读