Java并發(fā)編程之CountDownLatch解讀
更新時間:2023年12月31日 09:07:40 作者:_Romeo
這篇文章主要介紹了Java并發(fā)編程之CountDownLatch解讀,是通過一個計數器來實現的,計數器的初始值是線程的數量,countDownLatch這個類使一個線程等待其他線程各自執(zhí)行完畢后再執(zhí)行,需要的朋友可以參考下
概念
- countDownLatch這個類使一個線程等待其他線程各自執(zhí)行完畢后再執(zhí)行。
- 是通過一個計數器來實現的,計數器的初始值是線程的數量。每當一個線程執(zhí)行完畢后,計數器的值就-1,當計數器的值為0時,表示所有線程都執(zhí)行完畢,然后在閉鎖上等待的線程就可以恢復工作了。
源碼
countDownLatch類中只提供了一個構造器:
//參數count為計數值 public CountDownLatch(int count) { };
類中有三個方法是最重要的:
//調用await()方法的線程會被掛起,它會等待直到count值為0才繼續(xù)執(zhí)行 public void await() throws InterruptedException { }; //和await()類似,只不過等待一定的時間后count值還沒變?yōu)?的話就會繼續(xù)執(zhí)行 public boolean await(long timeout, TimeUnit unit) throws InterruptedException { }; //將count值減1 public void countDown() { };
自定義實現一個CountDownLatch
代碼如下:
public class KaneCountDownLatch { private KaneCountDownLatch.Sync sync; public KaneCountDownLatch(int count) { this.sync = new KaneCountDownLatch.Sync(count); } public void countDown() { this.sync.releaseShared(1); } public void await() { this.sync.acquireShared(1); } class Sync extends AbstractQueuedSynchronizer { public Sync(int count) { this.setState(count); } protected int tryAcquireShared(int arg) { return this.getState() == 0 ? 1 : -1; } protected boolean tryReleaseShared(int arg) { int c; int nextc; do { c = this.getState(); if (c == 0) { return false; } nextc = c - 1; } while(!this.compareAndSetState(c, nextc)); return nextc == 0; } } }
測試代碼如下:
public class CountDownLatch_Demo { public static void main(String[] args) throws InterruptedException { KaneCountDownLatch latch = new KaneCountDownLatch(6); //計數為6 for (int i = 0; i <6 ; i++) { new Thread(()->{ System.out.println("開始準備....."); latch.countDown();//計數減一 }).start(); Thread.sleep(1000); } latch.await(); //每個線程執(zhí)行一次,則-1,在latch為0的時候開始向下運行 這是這些線程都準備就緒,然后去一起干同一件事 //還有一種方式, 將一個活分為多段,每個線程去干一段 // for (int i = 0; i <6 ; i++) { // new Thread(()->{ // latch.countDown(); // 計數減一 // try { // latch.await(); // 阻塞 -- > 0 // System.out.println("線程:"+Thread.currentThread().getName()+"執(zhí)行完畢"); // } catch (InterruptedException e) { // e.printStackTrace(); // } // }).start(); // } System.out.println("開始干活...."); } }
CountDownLatch和CyclicBarrier區(qū)別
- countDownLatch是一個計數器,線程完成一個記錄一個,計數器遞減,只能只用一次
- CyclicBarrier的計數器更像一個閥門,需要所有線程都到達,然后繼續(xù)執(zhí)行,計數器遞增,提供reset功能,可以多次使用
到此這篇關于Java并發(fā)編程之CountDownLatch解讀的文章就介紹到這了,更多相關CountDownLatch解讀內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Shiro整合Springboot和redis,jwt過程中的錯誤shiroFilterChainDefinition問
這篇文章主要介紹了Shiro整合Springboot和redis,jwt過程中的錯誤shiroFilterChainDefinition問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04Springboot?集成spring?cache緩存的解決方案
這篇文章主要介紹了Springboot?集成spring?cache緩存,使用緩存最關鍵的一點就是保證緩存與數據庫的數據一致性,本文給大家介紹最常用的緩存操作模式,對Springboot?集成spring?cache緩存操作流程感興趣的朋友一起看看吧2022-06-06