乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      CountDownLatch&CyclicBarrier&Semaphore

       貪挽懶月 2022-06-20 發(fā)布于廣東

      本文將介紹一下CountDownLatch 、 CyclicBarrier 、 Semaphore這幾個控制線程的類。


      CountDownLatch 

      1、是什么?
      這是一個計數(shù)器,而且是倒計時。就像火箭發(fā)射,10,9,8,……,0,到了0火箭才會發(fā)射。這個也一樣,當里面的線程執(zhí)行完,外面的線程才會執(zhí)行。用一句話講就是:秦滅六國,一統(tǒng)華夏。滅六國是是六個線程,等這六個線程執(zhí)行完,一統(tǒng)華夏這個線程才能進行。

      2、怎么用?

      • 創(chuàng)建CountDownLatch對象,CountDownLatch count = new CountDownLatch(6);參數(shù)表示需要執(zhí)行的線程的數(shù)量;

      • 每執(zhí)行完一個線程,count.countDown()

      • 需要等待的線程count.await()。

      3、用之前怎么樣?
      話不多說,直接擼代碼:

      1public static void main(String[] args) throws Exception{
      2        for (int i = 1; i <= 6; i++) {
      3            final int temp = i;
      4            new Thread(() -> {
      5                System.out.println("第 " + temp + "個國家被滅!");
      6            }, "i").start();
      7        }
      8        System.out.println("六國被滅,秦一統(tǒng)華夏!");
      9}

      我們的本意是:循環(huán)里面的六個線程執(zhí)行完,主線程才能輸出“六國被滅,秦一統(tǒng)華夏!”這句換,看看執(zhí)行結(jié)果是否如嘗所愿:

      運行結(jié)果

      才滅了三個國家,那統(tǒng)一個錘子。

      4、用之后怎么樣?

       1 public static void main(String[] args) throws Exception{
      2        CountDownLatch count = new CountDownLatch(6);
      3        for (int i = 1; i <= 6; i++) {
      4            final int temp = i;
      5            new Thread(() -> {
      6                System.out.println("第 " + temp + "個國家被滅!");
      7                count.countDown();
      8            }, "i").start();
      9        }
      10        count.await();
      11        System.out.println("六國被滅,秦一統(tǒng)華夏!");
      12 }
      運行結(jié)果


      這就符合了預(yù)期。

      CyclicBarrier

      1、是什么?
      上面說的CountDownLatch 是倒計數(shù),那么這個就是順數(shù)的。一句話:集齊七顆龍珠,才能召喚神龍。只有集龍珠這七個線程執(zhí)行完,召喚神龍的線程才能執(zhí)行。

      2、怎么用?

      • 創(chuàng)建對象,兩個參數(shù)(1、集多少顆龍珠,2、new 一個線程,執(zhí)行集齊龍珠后要做的事),CyclicBarrier barrier = new CyclicBarrier(7,() -> {System.out.println("集齊七顆龍珠,召喚神龍!");});

      • 每執(zhí)行完一個線程,barrier.await()。

      3、用之前怎么樣?

      1 public static void main(String[] args) throws Exception{
      2        for (int i = 1; i <= 7; i++) {
      3            final int temp = i;
      4            new Thread(() -> {
      5                System.out.println("收集到第 " + temp + "顆龍珠!");
      6            }, "i").start();
      7        }
      8        System.out.println("集齊七顆龍珠,召喚神龍!");
      9 }
      運行結(jié)果


      可以看到,才收集到一顆龍珠,就召喚神龍,那能召喚出來?別做夢了。

      4、用之后怎么樣?

       1 public static void main(String[] args) throws Exception{
      2        CyclicBarrier barrier = 
      3             new CyclicBarrier(7,() -> {System.out.println("集齊七顆龍珠,召喚神龍!");});
      4        for (int i = 1; i <= 7; i++) {
      5            final int temp = i;
      6            new Thread(() -> {
      7                System.out.println("收集到第 " + temp + "顆龍珠!");
      8                try {
      9                    barrier.await();
      10                } catch (Exception e) {
      11                    e.printStackTrace();
      12                }
      13            }, "i").start();
      14        }
      15    }
      運行結(jié)果


      這才是召喚神龍的正確操作!

      Semaphore

      1、是什么?
      順數(shù)計數(shù)和倒數(shù)技術(shù)都有了,那這個Semaphore是什么鬼?翻譯過來意思是信號燈。一句話:搶車位。有三個車位,現(xiàn)在有六輛車。那么先是六輛車去搶三個車位,沒搶到的就等著。等車位里面的車走了一輛,那么等待車就可以進去一輛。

      2、怎么用?

      • 創(chuàng)建對象,一個參數(shù)(車位的數(shù)量),

      • 搶車位,semaphore.acquire();

      • 離開車位,semaphore.release();

      代碼說話:

       1public static void main(String[] args) throws Exception{
      2        Semaphore semaphore = new Semaphore(3);
      3        for (int i = 1; i <= 6; i++) {
      4            final int temp = i;
      5            new Thread(() -> {
      6                try {
      7                    semaphore.acquire();
      8                    System.out.println("第 " + temp + "輛車搶到車位!");
      9                    TimeUnit.SECONDS.sleep(3);
      10                    System.out.println("3秒后第" + temp + "輛車離開車位!");
      11                    semaphore.release();
      12                } catch (InterruptedException e) {
      13                    e.printStackTrace();
      14                }
      15            }, "i").start();
      16        }
      17}
      運行結(jié)果

      符合預(yù)期,一開始進去三輛,然后等有車離開了其他的才能進去。

        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多