public static void main(String[] args) { CountDownLatch countDownLatch = new CountDownLatch(3); for (int i = 0 ; i < 3 ; i++) { int count = i; new Thread(() -> { System.out.println("Count down thread id : " + count); countDownLatch.countDown(); }).start(); } try { System.out.println("Main thread await!"); countDownLatch.await(); System.out.println("Main thread finish!"); } catch (InterruptedException e) { e.printStackTrace(); } }
從日誌輸出能夠看到,主線程進入等待,當全部子線程均調用了countDown後,主線程繼續執行:java
Main thread await! Count down thread id : 1 Count down thread id : 0 Count down thread id : 2 Main thread finish!
public static void main(String[] args) { CyclicBarrier cyclicBarrier = new CyclicBarrier(3); for (int i = 0; i < 3; i++) { int count = i; new Thread(() -> { try { Thread.sleep(3L); System.out.println("Thread id waiting : " + count); cyclicBarrier.await(); System.out.println("Thread id finish : " + count); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }).start(); } }
從日誌能夠看到,全部線程首先輸出waiting,而後再輸出finish:ui
Thread id waiting : 2 Thread id waiting : 0 Thread id waiting : 1 Thread id finish : 1 Thread id finish : 2 Thread id finish : 0
public static void main(String[] args) { Semaphore semaphore = new Semaphore(3); for (int i = 0 ; i < 5 ; i++) { int count = i; new Thread(() -> { try { semaphore.acquire(); System.out.println("Thread got semaphore : " + count); Thread.sleep(2L); semaphore.release(); System.out.println("Thread release semaphore : " + count); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } }
從日誌中能夠看出,0、一、2共3個線程首先獲取了鎖,隨後1首先釋放鎖,釋放後3獲取了鎖,獲取鎖的總線程仍爲3。隨後0釋放了鎖,此時4就能夠獲取鎖,執行的總線程仍爲3。線程
Thread got semaphore : 0 Thread got semaphore : 1 Thread got semaphore : 2 Thread release semaphore : 1 Thread got semaphore : 3 Thread release semaphore : 0 Thread got semaphore : 4 Thread release semaphore : 2 Thread release semaphore : 4 Thread release semaphore : 3