• Java多线程使用 CountDownLatch等待其他线程执行完成


    Java多线程使用 CountDownLatch等待其他线程执行完成

    CountDownLatch允许一个或者多个线程去等待其他线程完成操作。

    java.lang.Concurrent包下的可阻塞类CountDownLatch(倒数计数器),基于基类AQS(AbstractQueuedSynchronizer)标准队列同步器类。在同步状态state中保存的是当前的计数值。

    CountDownLatch接收一个int型参数,表示要等待的工作线程的个数。

    countDown()调用releaseShared(int arg),从而导致计数器值递减,并且计数器值为零时,解除所有等待线程的阻塞。

    await()调用acquireSharedInterruptibly(int arg),当计数器值为零时,acquire将立即返回,否则将阻塞。

    await():阻塞当前线程,将当前线程加入AQS的阻塞队列;
    countDown():对计数器进行递减操作,当计数器递减至0时,当前线程会取唤醒阻塞队列中的所有线程。

    简单示例:
    主线程等待其他线程执行完成之后执行业务:

    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    import static java.lang.Thread.sleep;
    
    public class countdownLatchTest {
        public static void main(String[] args) {
            // 新建CountDownLatch
            CountDownLatch latch = new CountDownLatch(3);
            // 新建线程池
            ExecutorService service = Executors.newFixedThreadPool(4);
            // 提交任务
            service.submit(()->{
                System.out.println("1-begin....");
                try {
                    sleep(1);
                    // CountDownLatch - 1
                    latch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("1-end....");
            });
    
            service.submit(()->{
                System.out.println("2-begin....");
                try {
                    sleep((long) 1.5);
                    latch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("2-end....");
            });
    
            service.submit(()->{
                System.out.println("3-begin....");
                try {
                    sleep(2);
                    latch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("3-end....");
            });
    
            service.submit(()->{
                System.out.println("waiting...");
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("等待结束");
            });
            service.shutdown();
        }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    在这里插入图片描述
    写的比较粗糙,只是简单记录一下如何使用。

  • 相关阅读:
    【SLAM】IMU预积分的理解、手把手推导(2/4)
    micropython 可视化音频 频谱解析(应该是全网首家)(预告,还没研究完成)
    redis底层都有哪些数据结构?带你了解redis是如何存储数据的
    实验2 SQL的多表查询
    14-Kafka-Day03
    『 Linux 』 进程间通信概述
    ESP8285 RTOS SDK OTA
    Springboot+Websocket+JWT实现的即时通讯模块
    JavaScript【Text 节点、Node 节点属性(nodeName、nodeValue 、textContent、nextSibling、previousSibling)】(十二)
    NVIDIA安装最新显卡驱动
  • 原文地址:https://blog.csdn.net/qq_36944952/article/details/126196175