最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java CountDownLatch計(jì)數(shù)器與CyclicBarrier循環(huán)屏障

 更新時(shí)間:2023年04月06日 08:33:03   作者:Alphathur  
CountDownLatch是一種同步輔助,允許一個(gè)或多個(gè)線程等待其他線程中正在執(zhí)行的操作的ASET完成。它允許一組線程同時(shí)等待到達(dá)一個(gè)共同的障礙點(diǎn)

定義

CountDownLatch: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

CyclicBarrier: A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.

上述是Oracle官方定義。簡(jiǎn)單來(lái)說(shuō)

CountDownLatch:計(jì)數(shù)器,允許一個(gè)或多個(gè)線程等待,直到在其他線程中執(zhí)行的一組操作完成。

CyclicBarrier:循環(huán)屏障,它允許一組線程相互等待以達(dá)到一個(gè)共同的屏障點(diǎn)。

區(qū)別

  • CountDownLatch 是 AQS (AbstractQueuedSynchronizer) 的一員,但 CyclicBarrier 不是。
  • CountDownLatch 的使用場(chǎng)景中,有兩類(lèi)線程,一類(lèi)是調(diào)用await()方法的等待線程,另一類(lèi)是調(diào)用countDownl() 方法的操作線程。CyclicBarrier 的場(chǎng)景中,只有一類(lèi)線程,都是相互等待的等待線程。
  • CountDownLatch 是減計(jì)數(shù),遞減完后不能復(fù)位,CyclicBarrier 是加計(jì)數(shù),遞增完后自動(dòng)復(fù)位

CountDownLatch 示例

創(chuàng)建兩組線程,一組等待另一組執(zhí)行完才繼續(xù)進(jìn)行

CountDownLatch countDownLatch = new CountDownLatch(5);
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
    executorService.execute(() -> {
        countDownLatch.countDown();
        System.out.println("run..");
    });
}
for (int i = 0; i < 3; i++) {  //我們要等上面執(zhí)行完成才繼續(xù)
    executorService.execute(() -> {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("await..");
    });
}
executorService.shutdown();

打?。?/p>

run..
run..
run..
run..
run..
await..
await..
await..

等待累加線程執(zhí)行完,主線程再輸出累加結(jié)果

public class ThreadUnsafeExample {
    private int cnt = 0;
    public void add() {
        cnt++;
    }
    public int get() {
        return cnt;
    }
    public static void main(String[] args) throws InterruptedException {
        final int threadSize = 1000;
        ThreadUnsafeExample example = new ThreadUnsafeExample();
        final CountDownLatch countDownLatch = new CountDownLatch(threadSize);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < threadSize; i++) {
            executorService.execute(() -> {
                example.add();
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        System.out.println(example.get());
    }
}

打?。?/p>

997

3 模擬并發(fā)

ExecutorService executorService = Executors.newCachedThreadPool();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 5; i++) {
            executorService.submit( () -> {
                try {
                    countDownLatch.await();
                    System.out.println("【" + Thread.currentThread().getName() + "】開(kāi)始執(zhí)行……");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        Thread.sleep(2000);
        countDownLatch.countDown();//開(kāi)始并發(fā)
        executorService.shutdown();

打?。?/p>

【pool-2-thread-2】開(kāi)始執(zhí)行……
【pool-2-thread-5】開(kāi)始執(zhí)行……
【pool-2-thread-3】開(kāi)始執(zhí)行……
【pool-2-thread-1】開(kāi)始執(zhí)行……
【pool-2-thread-4】開(kāi)始執(zhí)行……

CyclicBarrier 示例

所有線程相互等待,直到某一步完成后再繼續(xù)執(zhí)行

        final int totalThread = 3;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < totalThread; i++) {
            executorService.execute(() -> {
                System.out.println("before..");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("after..");
            });
        }
        executorService.shutdown();

打?。?/p>

before..
before..
before..
after..
after..
after..

到此這篇關(guān)于Java CountDownLatch計(jì)數(shù)器與CyclicBarrier循環(huán)屏障的文章就介紹到這了,更多相關(guān)Java CountDownLatch與CyclicBarrier內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring boot與ktor整合的實(shí)現(xiàn)方法

    spring boot與ktor整合的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于spring boot與ktor整合的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • JAVA線程同步實(shí)例教程

    JAVA線程同步實(shí)例教程

    這篇文章主要介紹了JAVA線程同步實(shí)例教程,在Java程序設(shè)計(jì)中有著非常廣泛的應(yīng)用,需要的朋友可以參考下
    2014-08-08
  • IDEA實(shí)現(xiàn)遠(yuǎn)程調(diào)試步驟詳解

    IDEA實(shí)現(xiàn)遠(yuǎn)程調(diào)試步驟詳解

    這篇文章主要介紹了IDEA實(shí)現(xiàn)遠(yuǎn)程調(diào)試步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java實(shí)現(xiàn)周期性執(zhí)行(定時(shí)任務(wù))

    java實(shí)現(xiàn)周期性執(zhí)行(定時(shí)任務(wù))

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)周期性執(zhí)行定時(shí)任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Springboot整合logback的詳細(xì)教程

    Springboot整合logback的詳細(xì)教程

    這篇文章主要介紹了Springboot整合logback的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • mybatis之增刪改查

    mybatis之增刪改查

    本篇文章主要介紹了Mybatis實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例(CRUD),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-07-07
  • 解決spring boot 配置文件后綴的一個(gè)坑

    解決spring boot 配置文件后綴的一個(gè)坑

    這篇文章主要介紹了spring boot 配置文件后綴的一個(gè)坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java使用代理進(jìn)行網(wǎng)絡(luò)連接方法示例

    Java使用代理進(jìn)行網(wǎng)絡(luò)連接方法示例

    這篇文章主要介紹了Java使用代理進(jìn)行網(wǎng)絡(luò)連接方法示例,內(nèi)容十分詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例

    Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例

    今天小編就為大家分享一篇關(guān)于Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • 詳解java中static關(guān)鍵詞的作用

    詳解java中static關(guān)鍵詞的作用

    這篇文章主要介紹了java中static關(guān)鍵詞的作用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01

最新評(píng)論

龙岩市| 宜君县| 伊宁市| 阿克苏市| 苍山县| 通城县| 肥城市| 当阳市| 利川市| 射阳县| 平潭县| 抚州市| 林西县| 新疆| 墨玉县| 赞皇县| 开江县| 洛阳市| 冀州市| 合江县| 奉新县| 石景山区| 龙陵县| 平和县| 咸阳市| 新乡县| 彰化市| 龙岩市| 丹巴县| 哈尔滨市| 改则县| 图们市| 渝北区| 石城县| 锦屏县| 车险| 武冈市| 贵南县| 济南市| 洛浦县| 长春市|