<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                #### CountDownLatch CountDownLatch對象指定一個基數,每次執行countDown()方法,基數減一,直到這個基數為0時,執行await()方法后的代碼。一般await用于主方法中。 - 構造器方法 ``` 構造器: CountDownLatch(int num); // 給定義的CountDownLatch指定一個數值 方法: await(); // 等待直到傳入CountDownLatch對象中數值為0,再執行后面代碼。 await(long time, TimeUnit timeUnit); // 設置等待超時時間,如果再規定時間內CountDownLatch對象中數值沒有為0,則報錯,CountDownLatch失效。 countDown(); // 傳入CountDownLatch對象中數值減一 ``` - 例子 ``` public void CountDownLatchTest(){ int threadCount = 4; CountDownLatch countDownLatch = new CountDownLatch(threadCount); ExecutorService executorService = Executors.newSingleThreadExecutor(); for (int i = 0; i < threadCount; i++) { executorService.execute(new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } countDownLatch.countDown(); System.out.println("當前線程"+Thread.currentThread().getName()+"執行完畢"); } })); } executorService.shutdown(); System.out.println("等待線程執行完畢....."); countDownLatch.await(); System.out.println("所有線程執行完"); } ``` #### CyclicBarrier CyclicBarrier對象指定一個基數,每次執行await()方法時,基數減一,直到這個基數為0時,執行await()方法后的代碼。一般await用于線程方法中。 - 構造器方法 ``` 構造器: CyclicBarrier(int num); // 給定義的CountDownLatch指定一個數值 CyclicBarrier(int num, Runnable r); // 給定義的CountDownLatch指定一個數值, 第二個參數指當基數為0時,創建一個線程參數為r的線程并執行,直到該線程執行結束后開始執行await方法后面的代碼 方法: await(); // 每次執行await()方法時,傳入CyclicBarrier基數num減一,直到num為0,才執行await方法后續的代碼。 await(long time, TimeUnit timeUnit); // 同await方法,設定了等待的超時時間,如果超時,則會報超時錯誤,并且CyclicBarrier失效。 ``` - 例子 ``` public void CyclicBarrierTeset(){ for(int i = 0 ; i < 10 ; i++) { Thread.sleep(1000); new Thread(() ->{ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "執行"); try { cyclicBarrier.await(); } catch (BrokenBarrierException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("達到定義數目,wait方法后代碼執行"); }).start(); } System.in.read(); } ```
                  <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>

                              哎呀哎呀视频在线观看