<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### CountDownLatch CountDownLatch是閉鎖的一種實現;CountDownLatch是在java1.5被引入; CountDownLatch這個類能夠使一個線程等待其他線程完成各自的工作后再執行; ``` public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } ``` 主要API: * countDown\(\):該方法遞減計數器,表示有一個事件已經發生; * await\(\):該方法等待計時器達到零,達到零后表示需要等待的所有事件都已發生; 如果計數器的值非零,await方法會一直阻塞直到計數器為零,或者等待中的線程中斷,或者等待超時; ##### 起始門\(Starting Gate\) 所有子線程等待計數器為零后一起執行 ``` public class Appliction { private final static int NUM = 10; public static void main(String[] args) { CountDownLatch countDownLatch = new CountDownLatch(1); for (int i = 0; i < NUM; i++) { new Thread(() -> { try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.err.println(Thread.currentThread().getName() + " started:" + System.currentTimeMillis()); }).start(); } countDownLatch.countDown(); System.err.println("main thread exec end"); } } ``` ##### 結束門\(Ending Gate\) 等待所有子任務或子線程結束后\(計數器為零\),對執行結果進行統計或匯總 ``` /** * 假設有10塊磁盤,需要10個線程同時統計磁盤空間大小,統計完成后由主線程進行匯總 */ public class Appliction { private final static int NUM = 10; public static void main(String[] args) throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(NUM); List<Disk> tasks = new ArrayList<>(NUM); for (int i = 0; i < NUM; i++) { tasks.add(new Disk()); } for (Disk dk : tasks) { new Thread(new DiskCountTask(countDownLatch, dk)).start(); } countDownLatch.await(); int size = tasks.stream().mapToInt(Disk::getSize).sum(); System.err.println("All disk space size:" + size); } } class Disk { private Integer size; public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } } class DiskCountTask implements Runnable { private Disk disk; private CountDownLatch downLatch; public DiskCountTask(CountDownLatch downLatch, Disk disk) { this.downLatch = downLatch; this.disk = disk; } @Override public void run() { int size = new Random().nextInt(10); try { TimeUnit.SECONDS.sleep(size); } catch (InterruptedException e) { e.printStackTrace(); } disk.setSize(size); System.err.println(Thread.currentThread().getName() + " exec end[" + System.currentTimeMillis() + "], size:" + size); downLatch.countDown(); } } ``` 設想有這樣一個功能需要Thread1、Thread2、Thread3、Thread4四條線程分別統計C、D、E、F四個盤的大小,所有線程都統計完畢交給主線程去做匯總,利用CountDownLatch來完成就非常輕松
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看