CountDownLatch(倒計時計數器)使用說明


方法說明:
 
public void countDown()
     遞減鎖存器的計數,如果計數到達零,則釋放所有等待的線程。如果當前計數大於零,則將計數減少。如果新的計數為零,出於線程調度目的,將重新啟用所有的等待線程。
     如果當前計數等於零,則不發生任何操作。

public boolean await(long timeout, TimeUnit unit)throws InterruptedException
     使當前線程在鎖存器倒計數至零之前一直等待,除非線程被中斷或超出了指定的等待時間。如果當前計數為零,則此方法立刻返回 true 值。
     如果當前計數大於零,則出於線程調度目的,將禁用當前線程,且在發生以下三種情況之一前,該線程將一直處於休眠狀態:
         由於調用 countDown() 方法,計數到達零;或者其他某個線程中斷當前線程;或者已超出指定的等待時間。
          * 如果計數到達零,則該方法返回 true 值。
          * 如果當前線程,在進入此方法時已經設置了該線程的中斷狀態;或者在等待時被中斷, 則拋出 InterruptedException,並且清除當前線程的已中斷狀態。
          * 如果超出了指定的等待時間,則返回值為 false。如果該時間小於等於零,則此方法根本不會等待。
    
     參數:
         timeout - 要等待的最長時間
         unit - timeout 參數的時間單位。
     返回:
         如果計數到達零,則返回 true;如果在計數到達零之前超過了等待時間,則返回 false
     拋出:
          InterruptedException - 如果當前線程在等待時被中斷
 
例子1:
     主線程等待子線程執行完成在執行。
     
 1 import java.util.concurrent.CountDownLatch;
 2 import java.util.concurrent.ExecutorService;
 3 import java.util.concurrent.Executors;
 4  
 5 public class CountdownLatchTest1 {
 6  
 7         public static void main(String[] args) {
 8               ExecutorService service = Executors. newFixedThreadPool(3);
 9                final CountDownLatch latch = new CountDownLatch(3);
10                for (int i = 0; i < 3; i++) {
11                      Runnable runnable = new Runnable() {
12                            
13                             @Override
14                             public void run() {
15                                    try {
16                                          System. out.println("子線程" + Thread.currentThread().getName() + "開始執行");
17                                          Thread. sleep((long) (Math. random() * 10000));
18                                          System. out.println("子線程" + Thread.currentThread().getName() + "執行完成");
19                                          latch.countDown(); // 當前線程調用此方法,則計數減一
20                                   } catch (InterruptedException e) {
21                                          e.printStackTrace();
22                                   }
23                            }
24                      };
25                      service.execute(runnable);
26               }
27               
28                try {
29                      System. out.println("主線程" + Thread.currentThread().getName() + "等待子線程執行完成..." );
30                      latch.await(); // 阻塞當前線程,直到計時器的值為0
31                      System. out.println("主線程" + Thread.currentThread().getName() + "開始執行...");
32               } catch (InterruptedException e) {
33                      e.printStackTrace();
34               }
35        }
36 }

 

例子2:
     百米賽跑,4名運動員選手到達場地等待裁判口令,裁判一聲口令,選手聽到后同時起跑,當所有選手到達終點,裁判進行匯總匯總排名。
 
 1 import java.util.concurrent.CountDownLatch;
 2 import java.util.concurrent.ExecutorService;
 3 import java.util.concurrent.Executors;
 4  
 5 public class CountdownLatchTest2 {
 6  
 7         public static void main(String[] args) {
 8               ExecutorService service = Executors. newCachedThreadPool();
 9                final CountDownLatch cdOrder = new CountDownLatch(1);
10                final CountDownLatch cdAnswer = new CountDownLatch(4);
11                for (int i = 0; i < 4; i++) {
12                      Runnable runnable = new Runnable() {
13                             public void run() {
14                                    try {
15                                          System. out.println("選手" + Thread.currentThread().getName() + "正等待裁判發布口令");
16                                          cdOrder.await();
17                                          System. out.println("選手" + Thread.currentThread().getName() + "已接受裁判口令");
18                                          Thread. sleep((long) (Math. random() * 10000));
19                                          System. out.println("選手" + Thread.currentThread().getName() + "到達終點");
20                                          cdAnswer.countDown();
21                                   } catch (Exception e) {
22                                          e.printStackTrace();
23                                   }
24                            }
25                      };
26                      service.execute(runnable);
27               }
28                try {
29                      Thread. sleep((long) (Math. random() * 10000));
30  
31                      System. out.println("裁判" + Thread.currentThread ().getName() + "即將發布口令" );
32                      cdOrder.countDown();
33                      System. out.println("裁判" + Thread.currentThread ().getName() + "已發送口令,正在等待所有選手到達終點" );
34                      cdAnswer.await();
35                      System. out.println("所有選手都到達終點" );
36                      System. out.println("裁判" + Thread.currentThread ().getName() + "匯總成績排名" );
37               } catch (Exception e) {
38                      e.printStackTrace();
39               }
40               service.shutdown();
41  
42        }
43 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM