1. CountDownLatch簡介java
一個同步輔助類,在完成一組正在其餘線程中執行的操做以前,它容許一個或多個線程一直等待。其本質就是一個共享鎖。dom
他最主要的做用是用來同步java的線程。ide
主要有如下三個方法:this
主要方法:spa
public CountDownLatch(int count); public void countDown(); public void await();
構造方法參數指定了計數的次數線程
countDown方法,當前線程調用此方法,則計數減一code
await方法,調用此方法會一直阻塞當前線程,直到計時器的值爲0同步
2. 實戰應用的例子it
一個模擬運動員比賽的例子,首先要等全部的運動員所有給準備好,而後發起「起跑指令」全部的運動員開始起跑,等全部的運動員跑完才結束本次筆試。io
代碼以下:
運動員線程代碼:
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(); } }
運行結果以下: