CountDownLatch應用實戰
1. CountDownLatch簡介
一個同步輔助類,在完成一組正在其他線程中執行的操作之前,它允許一個或多個線程一直等待。其本質就是一個共享鎖。
他最主要的作用是用來同步java的線程。
主要有一下三個方法:
主要方法:
public CountDownLatch(int count); public void countDown(); public void await();
構造方法參數指定了計數的次數
countDown方法,當前線程調用此方法,則計數減一
await方法,調用此方法會一直阻塞當前線程,直到計時器的值為0
2. 實戰應用的例子
一個模擬運動員比賽的例子,首先要等所有的運動員全部給準備好,然后發起“起跑指令”所有的運動員開始起跑,等所有的運動員跑完才結束本次筆試。
代碼如下:
運動員線程代碼:
package it_cast.day01;
import java.util.concurrent.CountDownLatch;
public class Player implements Runnable {
private int id;
private CountDownLatch begin;
private CountDownLatch end;
public Player(int id, CountDownLatch begin, CountDownLatch end) {
this.id=id;
this.begin=begin;
this.end=end;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("Play" + id + "ready");
begin.await();// 所有的運動員在這準備者,當一聲令下就開始跑
Thread.sleep((long) (Math.random() * 100));// 隨機分配時間,即運動員完成時間
System.out.println("Play" + id + " arrived.");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
end.countDown();// 每個運動員跑完,使end狀態減1,最終減至0
}
}
}
主代碼:
package it_cast.day01;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo {
private static final int PLAYER_AMOUNT = 5;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//對于每位運動員,CountDownLatch減1后即結束比賽
CountDownLatch begin = new CountDownLatch(1);
//對于整個比賽,所有運動員結束后才算結束
CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT);
Player [] plays=new Player[PLAYER_AMOUNT];
//初始化5個運動員
for (int i = 0; i < plays.length; i++) {
plays[i]=new Player(i, begin, end);
}
//設置特定的線程池,大小為5
ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT);
for(Player p:plays)
exe.execute(p);
Thread.sleep(1000);
System.out.println("Race begins!");
begin.countDown();
end.await();; //等待end狀態變為0,即為所有人都跑完了,即為比賽結束
System.out.println("Race ends!");
exe.shutdown();
}
}
運行結果如下:

本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!