Java实现多个子线程执行完成后执行主线程

在 Java 面试中,“如何让主线程等待所有子线程执行完毕后再继续?” 是一个高频多线程问题。下面给出 4 种主流实现方式 + 代码示例,覆盖从基础到进阶,面试时按需选择即可。


1. 使用 Thread.join()(最基础)

public class ThreadJoinDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> System.out.println("T1"));
        Thread t2 = new Thread(() -> System.out.println("T2"));

        t1.start();
        t2.start();

        t1.join();  // 主线程等待 t1 完成
        t2.join();  // 主线程等待 t2 完成

        System.out.println("主线程继续执行...");
    }
}

关键点

  • join() 会让当前线程(主线程)阻塞,直到目标线程结束。

2. 使用 CountDownLatch(灵活计数)

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        int threadCount = 3;
        CountDownLatch latch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 完成");
                latch.countDown();  // 计数器减1
            }).start();
        }

        latch.await();  // 主线程等待计数器归零
        System.out.println("所有子线程完成,主线程继续...");
    }
}

适用场景

  • 需要等待 固定数量 的子线程完成,不关心顺序。

3. 使用 CyclicBarrier(可复用栅栏)

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        int threadCount = 3;
        CyclicBarrier barrier = new CyclicBarrier(threadCount, () -> {
            System.out.println("所有子线程到达栅栏,主线程继续...");
        });

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 到达");
                try {
                    barrier.await();  // 等待其他线程
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

特点

  • 可复用(Cyclic),适合 多次同步 的场景。

4. 使用线程池 + Future/CompletionService(推荐)

import java.util.List;
import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            pool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + " 完成");
                return "OK";
            });
        }
        pool.shutdown();  // 关闭线程池(不再接受新任务)
        pool.awaitTermination(1, TimeUnit.MINUTES);  // 等待所有任务完成
        System.out.println("所有子线程完成,主线程继续...");
    }
}

优势

  • 资源可控(复用线程池),避免频繁创建和销毁线程。

面试加分回答

“如果子线程需要返回结果,我会用 ExecutorService 提交 Callable 任务,通过 Future.get() 阻塞获取结果;如果只是等待完成,CountDownLatchCompletableFuture.allOf() 会更优雅。”


总结对比表

方法 适用场景 是否可复用 是否阻塞主线程
Thread.join() 简单线程
CountDownLatch 固定数量线程同步
CyclicBarrier 多次同步或分组协作 否(主线程可继续)
ExecutorService 线程池批量任务 是(awaitTermination

面试反问
“如果子线程异常退出,join()CountDownLatch 会怎么处理?”
(可以答:需手动检查线程状态或使用 Future 捕获异常)

掌握这 4 种方式,面试稳了!

你可能感兴趣的:(Java实现多个子线程执行完成后执行主线程)