ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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();



    }


}

 

posted on 2021-07-14 10:15  哦哟这个怎么搞  阅读(72)  评论(0编辑  收藏  举报