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

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

JUC之CountdownLatch使用詳解

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

一、是什么?

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

其中構(gòu)造參數(shù)用來初始化等待計(jì)數(shù)值,await() 用來等待計(jì)數(shù)歸零,countDown() 用來讓計(jì)數(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();
            }
        });
    }
}

在這里插入圖片描述

三、應(yīng)用之同步等待多線程準(zhǔn)備完畢

 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();
    }

四、 應(yīng)用之同步等待多個(gè)遠(yuǎn)程調(diào)用結(jié)束

    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();
    }

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

相關(guān)文章

最新評論