如何在主线程中等子线程执行完?
1 个回答
自己喜欢的东西又何必问别人好不好牧峰在 Java 中,可以通过以下几种方式在主线程中等待子线程执行完成: ### 1. 使用 `Thread.join()` `Thread.join()` 方法会使主线程等待子线程完成后再继续执行。这个方法是最常用的方式。 示例代码: ```java class MyThread extends Thread { @Override public void run() { System.out.println("子线程开始执行"); try { Thread.sleep(2000); // 模拟子线程执行任务 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("子线程执行完成"); } } public class Main { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); try { thread.join(); // 等待子线程完成 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("主线程继续执行"); } } ``` ### 2. 使用 `ExecutorService` 和 `Future` 如果使用线程池来执行任务,可以通过 `ExecutorService` 提交任务并使用 `Future` 获取子线程的执行结果。在主线程中,可以调用 `Future.get()` 来等待子线程完成。 示例代码: ```java import java.util.concurrent.*; public class Main { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<?> future = executor.submit(() -> { System.out.println("子线程开始执行"); try { Thread.sleep(2000); // 模拟子线程执行任务 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("子线程执行完成"); }); // 等待子线程完成 future.get(); // 阻塞直到子线程执行完毕 System.out.println("主线程继续执行"); executor.shutdown(); // 关闭线程池 } } ``` ### 3. 使用 `CountDownLatch` 如果有多个子线程,可以使用 `CountDownLatch` 来同步等待所有子线程完成。主线程可以调用 `await()` 等待子线程通过调用 `countDown()` 完成。 示例代码: ```java import java.util.concurrent.*; public class Main { public static void main(String[] args) throws InterruptedException { int threadCount = 1; CountDownLatch latch = new CountDownLatch(threadCount); // 设置需要等待的线程数 Thread thread = new Thread(() -> { System.out.println("子线程开始执行"); try { Thread.sleep(2000); // 模拟子线程执行任务 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("子线程执行完成"); latch.countDown(); // 子线程执行完后,减少计数 }); thread.start(); latch.await(); // 主线程等待,直到所有子线程执行完毕 System.out.println("主线程继续执行"); } } ``` ### 4. 使用 `CyclicBarrier` `CyclicBarrier` 用于在多个线程之间同步,主线程可以等待所有子线程到达屏障位置之后继续执行。 示例代码: ```java import java.util.concurrent.*; public class Main { public static void main(String[] args) throws InterruptedException { int threadCount = 1; CyclicBarrier barrier = new CyclicBarrier(threadCount, () -> { // 线程都到达后调用 System.out.println("所有子线程执行完成,主线程继续执行"); }); Thread thread = new Thread(() -> { System.out.println("子线程开始执行"); try { Thread.sleep(2000); // 模拟子线程执行任务 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("子线程执行完成"); try { barrier.await(); // 等待主线程继续执行 } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } }); thread.start(); // 等待所有线程到达屏障 barrier.await(); System.out.println("主线程继续执行"); } } ``` ### 总结 - 使用 `Thread.join()`:最直接的方式,让主线程等待单个子线程完成。 - 使用 `ExecutorService` 和 `Future.get()`:适用于线程池,主线程通过 `Future.get()` 获取子线程的结果,并阻塞等待。 - 使用 `CountDownLatch`:适合等待多个线程完成,主线程通过 `await()` 阻塞等待,直到所有子线程执行完成。 - 使用 `CyclicBarrier`:适合多个线程同步,主线程等所有子线程完成后继续执行。 通常情况下,`Thread.join()` 和 `ExecutorService` 是最常见且简单的方式。发布于 2024-12-02 03:59 •IP属地 西藏举报评论收藏
公众号进入网站体验更佳

