溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Jav中CountDownLatch是什么

發(fā)布時(shí)間:2020-07-02 14:35:37 來源:億速云 閱讀:208 作者:清晨 欄目:開發(fā)技術(shù)

小編給大家分享一下Jav中CountDownLatch是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討方法吧!

CountDownLatch 是一個(gè)非常實(shí)用的多線程控制工具類。” Count Down ” 在英文中意為倒計(jì)數(shù), Latch 為門問的意思。如果翻譯成為倒計(jì)數(shù)門閥, 我想大家都會(huì)覺得不知所云吧! 因此,這里簡(jiǎn)單地稱之為倒計(jì)數(shù)器。在這里, 門問的含義是:把門鎖起來,不讓里面的線程跑出來。因此,這個(gè)工具通常用來控制線程等待,它可以讓某一個(gè)線程等待直到倒計(jì)時(shí)結(jié)束, 再開始執(zhí)行。

CountDown Latch 的構(gòu)造函數(shù)接收一個(gè)整數(shù)作為參數(shù),即當(dāng)前這個(gè)計(jì)數(shù)器的計(jì)數(shù)個(gè)數(shù)。

public CountDownLatch(int count)

CountDownLatch是一個(gè)同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待。一個(gè)CountDownLatch初始化為給定的計(jì)數(shù) 。 調(diào)用await方法阻塞,直到當(dāng)前計(jì)數(shù)為零,在調(diào)用countDown()方法之后,所有等待的線程被釋放,任何后續(xù)調(diào)用await立即返回。 這是一次性的現(xiàn)象 - 計(jì)數(shù)不能重置。 如果需要重置計(jì)數(shù),考慮使用CyclicBarrier ,CyclicBarrier的計(jì)數(shù)器可以被重置后使用,因此它被稱為是循環(huán)的barrier。

主要方法:

// 使當(dāng)前線程在鎖存器倒計(jì)數(shù)至零之前一直等待,除非線程被中斷。
void await()
// 使當(dāng)前線程在鎖存器倒計(jì)數(shù)至零之前一直等待,除非線程被中斷或超出了指定的等待時(shí)間。
boolean await(long timeout, TimeUnit unit)
// 遞減鎖存器的計(jì)數(shù),如果計(jì)數(shù)到達(dá)零,則釋放所有等待的線程。
void countDown()
// 返回當(dāng)前計(jì)數(shù)。
long getCount()

我們來看一個(gè)例子:

public class TestCountDownLatch {
  private static final int RUNNER_NUMBER = 5; // 運(yùn)動(dòng)員個(gè)數(shù)
  private static final Random RANDOM = new Random();
  public static void main(String[] args) {
    // 用于判斷發(fā)令之前運(yùn)動(dòng)員是否已經(jīng)完全進(jìn)入準(zhǔn)備狀態(tài),需要等待5個(gè)運(yùn)動(dòng)員,所以參數(shù)為5
    CountDownLatch readyLatch = new CountDownLatch(RUNNER_NUMBER);
    // 用于判斷裁判是否已經(jīng)發(fā)令,只需要等待一個(gè)裁判,所以參數(shù)為1
    CountDownLatch startLatch = new CountDownLatch(1);
    for (int i = 0; i < RUNNER_NUMBER; i++) {
      Thread t = new Thread(new Runner((i + 1) + "號(hào)運(yùn)動(dòng)員", readyLatch, startLatch));
      t.start();
    }
    try {
      readyLatch.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    startLatch.countDown();
    System.out.println("裁判:所有運(yùn)動(dòng)員準(zhǔn)備完畢,開始...");
  }
  static class Runner implements Runnable {
    private CountDownLatch readyLatch;
    private CountDownLatch startLatch;
    private String name;
    public Runner(String name, CountDownLatch readyLatch, CountDownLatch startLatch) {
      this.name = name;
      this.readyLatch = readyLatch;
      this.startLatch = startLatch;
    }
    public void run() {
      int readyTime = RANDOM.nextInt(1000);
      System.out.println(name + ":我需要" + readyTime + "秒時(shí)間準(zhǔn)備.");
      try {
        Thread.sleep(readyTime);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println(name + ":我已經(jīng)準(zhǔn)備完畢.");
      readyLatch.countDown();
      try {
        startLatch.await(); // 等待裁判發(fā)開始命令
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println(name + ":開跑...");
    }
  }
}

打印結(jié)果:

1號(hào)運(yùn)動(dòng)員:我需要547秒時(shí)間準(zhǔn)備.
2號(hào)運(yùn)動(dòng)員:我需要281秒時(shí)間準(zhǔn)備.
4號(hào)運(yùn)動(dòng)員:我需要563秒時(shí)間準(zhǔn)備.
5號(hào)運(yùn)動(dòng)員:我需要916秒時(shí)間準(zhǔn)備.
3號(hào)運(yùn)動(dòng)員:我需要461秒時(shí)間準(zhǔn)備.
2號(hào)運(yùn)動(dòng)員:我已經(jīng)準(zhǔn)備完畢.
3號(hào)運(yùn)動(dòng)員:我已經(jīng)準(zhǔn)備完畢.
1號(hào)運(yùn)動(dòng)員:我已經(jīng)準(zhǔn)備完畢.
4號(hào)運(yùn)動(dòng)員:我已經(jīng)準(zhǔn)備完畢.
5號(hào)運(yùn)動(dòng)員:我已經(jīng)準(zhǔn)備完畢.
裁判:所有運(yùn)動(dòng)員準(zhǔn)備完畢,開始...
3號(hào)運(yùn)動(dòng)員:開跑...
2號(hào)運(yùn)動(dòng)員:開跑...
1號(hào)運(yùn)動(dòng)員:開跑...
4號(hào)運(yùn)動(dòng)員:開跑...
5號(hào)運(yùn)動(dòng)員:開跑...

Process finished with exit code 0

注意:計(jì)數(shù)器必須大于等于0,只是等于0時(shí)候,計(jì)數(shù)器就是零,調(diào)用await方法時(shí)不會(huì)阻塞當(dāng)前線程。

看完了這篇文章,相信你對(duì)Jav中CountDownLatch是什么有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI