其中構造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一,需要的朋友可以參考下" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JUC之CountdownLatch使用詳解

 更新時間:2023年12月16日 08:50:59   作者:小晨想好好學習  
這篇文章主要介紹了JUC之CountdownLatch使用詳解,CountdownLatch 用來進行線程同步協(xié)作,等待所有線程完成倒計時,
其中構造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一,需要的朋友可以參考下

一、是什么?

CountdownLatch 用來進行線程同步協(xié)作,等待所有線程完成倒計時。

其中構造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一

二、demo演示

public class TestCountDownLatch {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        test5();
    }
    private static void test5() {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService service = Executors.newFixedThreadPool(4);
        service.submit(() -> {
            log.debug("begin...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        service.submit(() -> {
            log.debug("begin...");
            sleep(1.5);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        service.submit(() -> {
            log.debug("begin...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        service.submit(()->{
            try {
                log.debug("waiting...");
                latch.await();
                log.debug("wait end...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}

在這里插入圖片描述

三、應用之同步等待多線程準備完畢

 private static void test2() throws InterruptedException {
        AtomicInteger num = new AtomicInteger(0);
        ExecutorService service = Executors.newFixedThreadPool(10, (r) -> {
            return new Thread(r, "t" + num.getAndIncrement());
        });
        CountDownLatch latch = new CountDownLatch(10);
        String[] all = new String[10];
        Random r = new Random();
        for (int j = 0; j < 10; j++) {
            int x = j;
            service.submit(() -> {
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(r.nextInt(100));
                    } catch (InterruptedException e) {
                    }
                    all[x] = Thread.currentThread().getName() + "(" + (i + "%") + ")";
                    System.out.print("\r" + Arrays.toString(all));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n游戲開始...");
        service.shutdown();
    }

四、 應用之同步等待多個遠程調用結束

    private static void test3() throws InterruptedException, ExecutionException {
        RestTemplate restTemplate = new RestTemplate();
        log.debug("begin");
        ExecutorService service = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(4);
        service.submit(() -> {
            Map<String, Object> response = restTemplate.getForObject("http://localhost:8080/order/{1}", Map.class, 1);
            log.debug("{}",response);
            latch.countDown();
        });
       service.submit(() -> {
            Map<String, Object> response1 = restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 1);
           log.debug("{}",response1);
           latch.countDown();
        });
        service.submit(() -> {
            Map<String, Object> response2 = restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 2);
            log.debug("{}",response2);
            latch.countDown();
        });
        service.submit(() -> {
            Map<String, Object> response3 = restTemplate.getForObject("http://localhost:8080/logistics/{1}", Map.class, 1);
            log.debug("{}",response3);
            latch.countDown();
        });
        latch.await();
        log.debug("執(zhí)行完畢");
        service.shutdown();
    }

到此這篇關于JUC之CountdownLatch使用詳解的文章就介紹到這了,更多相關CountdownLatch使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • springboot自帶的緩存@EnableCaching用法

    springboot自帶的緩存@EnableCaching用法

    這篇文章主要介紹了springboot自帶的緩存@EnableCaching用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java泛型extends及super區(qū)別實例解析

    Java泛型extends及super區(qū)別實例解析

    這篇文章主要介紹了Java泛型extends及super區(qū)別實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Java?DelayQueue實現(xiàn)延時任務的示例詳解

    Java?DelayQueue實現(xiàn)延時任務的示例詳解

    DelayQueue是一個無界的BlockingQueue的實現(xiàn)類,用于放置實現(xiàn)了Delayed接口的對象,其中的對象只能在其到期時才能從隊列中取走。本文就來利用DelayQueue實現(xiàn)延時任務,感興趣的可以了解一下
    2022-08-08
  • eclipse創(chuàng)建一個基于maven的web項目詳細步驟

    eclipse創(chuàng)建一個基于maven的web項目詳細步驟

    開始學習maven,并用maven創(chuàng)建了第一個屬于自己的web項目,下面這篇文章主要給大家介紹了關于eclipse創(chuàng)建一個基于maven的web項目的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • 必須了解的高階JAVA枚舉特性!

    必須了解的高階JAVA枚舉特性!

    這篇文章主要介紹了必須了解的高階JAVA枚舉特性!幫助大家更好的理解和學習Java枚舉的相關知識,感興趣的朋友可以了解下
    2021-01-01
  • 為什么阿里巴巴要求日期格式化時必須有使用y表示年

    為什么阿里巴巴要求日期格式化時必須有使用y表示年

    這篇文章主要介紹了為什么阿里巴巴要求日期格式化時必須有使用y表示年,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • java利用時間格式生成唯一文件名的方法

    java利用時間格式生成唯一文件名的方法

    這篇文章主要介紹了java利用時間格式生成唯一文件名的方法,需要的朋友可以參考下
    2017-01-01
  • Redisson 分布式延時隊列 RedissonDelayedQueue 運行流程

    Redisson 分布式延時隊列 RedissonDelayedQueue 運行流程

    這篇文章主要介紹了Redisson分布式延時隊列 RedissonDelayedQueue運行流程,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 關于Scanner對象的輸入結束標記問題

    關于Scanner對象的輸入結束標記問題

    這篇文章主要介紹了關于Scanner對象的輸入結束標記問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java上傳文件進度條的實現(xiàn)方法(附demo源碼下載)

    Java上傳文件進度條的實現(xiàn)方法(附demo源碼下載)

    這篇文章主要介紹了Java上傳文件進度條的實現(xiàn)方法,可簡單實現(xiàn)顯示文件上傳比特數(shù)及進度的功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2015-12-12

最新評論