CountDownLatch理解

CountDownLatch

CountDownLatch是在java1.5被引入的,跟它一起被引入的併發工具類還有CyclicBarrier、Semaphore、ConcurrentHashMapBlockingQueue,它們都存在於java.util.concurrent包下。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

     主線程等待子線程執行完成在執行。

     

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountdownLatchTest1 {
   
public static void main(String[] args) {
        ExecutorService service = Executors. newFixedThreadPool(
3);
       
final CountDownLatch latch = new CountDownLatch(3);
       
for (int i = 0; i < 3; i++) {
            Runnable runnable =
new Runnable() {
               
@Override
               
public void run() {
                   
try {
                        System.
out.println("子線程" + Thread.currentThread().getName() + "開始執行");
                        Thread. sleep((
long) (Math. random() * 10000));
                        System.
out.println("子線程" + Thread.currentThread().getName() + "執行完成");
                       
latch.countDown(); // 當前線程調用此方法,則計數減一
                    
} catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            service.execute(runnable);
        }

       
try {
            System.
out.println("主線程" + Thread.currentThread().getName() + "等待子線程執行完成..." );
            latch.await();
// 阻塞當前線程,直到計時器的值爲0
           
System. out.println("主線程" + Thread.currentThread().getName() + "開始執行...");
        }
catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

輸出結果:

子線程pool-1-thread-1開始執行

主線程main等待子線程執行完成...

子線程pool-1-thread-2開始執行

子線程pool-1-thread-3開始執行

子線程pool-1-thread-3執行完成

子線程pool-1-thread-2執行完成

子線程pool-1-thread-1執行完成

主線程main開始執行...

例子2:

     百米賽跑,4名運動員選手到達場地等待裁判口令,裁判一聲口令,選手聽到後同時起跑,當所有選手到達終點,裁判進行彙總彙總排名。

 

package countdownlauch;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

public class CountdownLatchTest2 {

    public static void main(String[] args) {

        ExecutorService service = Executors. newCachedThreadPool();

        final CountDownLatch cdOrder = new CountDownLatch(1);

        final CountDownLatch cdAnswer = new CountDownLatch(4);

        for (int i = 0; i < 4; i++) {

            Runnable runnable = new Runnable() {

                public void run() {

                    try {

                        System. out.println("選手" + Thread.currentThread().getName() + "正等待裁判發佈口令");

                        cdOrder.await();

                        System. out.println("選手" + Thread.currentThread().getName() + "已接受裁判口令");

                        Thread. sleep((long) (Math. random() * 10000));

                        System. out.println("選手" + Thread.currentThread().getName() + "到達終點");

                        cdAnswer.countDown();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

            };

            service.execute(runnable);

        }

        try {

            Thread. sleep((long) (Math. random() * 10000));

            System. out.println("裁判" + Thread.currentThread ().getName() + "即將發佈口令" );

            cdOrder.countDown();

            System. out.println("裁判" + Thread.currentThread ().getName() + "已發送口令,正在等待所有選手到達終點" );

            cdAnswer.await();

            System. out.println("所有選手都到達終點" );

            System. out.println("裁判" + Thread.currentThread ().getName() + "彙總成績排名" );

        } catch (Exception e) {

            e.printStackTrace();

        }

        service.shutdown();

    }

}

輸出結果:

選手pool-1-thread-1正等待裁判發佈口令

選手pool-1-thread-2正等待裁判發佈口令

選手pool-1-thread-4正等待裁判發佈口令

選手pool-1-thread-3正等待裁判發佈口令

裁判main即將發佈口令

選手pool-1-thread-1已接受裁判口令

裁判main已發送口令,正在等待所有選手到達終點

選手pool-1-thread-4已接受裁判口令

選手pool-1-thread-2已接受裁判口令

選手pool-1-thread-3已接受裁判口令

選手pool-1-thread-1到達終點

選手pool-1-thread-3到達終點

選手pool-1-thread-4到達終點

選手pool-1-thread-2到達終點

所有選手都到達終點

裁判main彙總成績排名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章