package cn.jiedada.controller; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class MyTest { public static void main(String[] args) { CountDownLatch latch = new CountDownLatch(2); new Thread(() -> { System.out.println("线程C开启,等待线程A、B执行完成才继续执行"); try { latch.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("线程C执行完成"); }).start(); new Thread(() -> { System.out.println("线程A开始执行"); try { TimeUnit.SECONDS.sleep(new Random().nextInt(10)); latch.countDown(); } catch (Exception e) { e.printStackTrace(); } System.out.println("线程A执行完成"); }).start(); new Thread(() -> { System.out.println("线程B开始执行"); try { TimeUnit.SECONDS.sleep(new Random().nextInt(10)); latch.countDown(); } catch (Exception e) { e.printStackTrace(); } System.out.println("线程B执行完成"); }).start(); } }