Java中多線程同步類(lèi) CountDownLatch
在多線程開(kāi)發(fā)中,常常遇到希望一組線程完成之后在執(zhí)行之后的操作,java提供了一個(gè)多線程同步輔助類(lèi),可以完成此類(lèi)需求:
類(lèi)中常見(jiàn)的方法:
其中構(gòu)造方法:
CountDownLatch(int count) 參數(shù)count是計(jì)數(shù)器,一般用要執(zhí)行線程的數(shù)量來(lái)賦值。
long getCount():獲得當(dāng)前計(jì)數(shù)器的值。
void countDown():當(dāng)計(jì)數(shù)器的值大于零時(shí),調(diào)用方法,計(jì)數(shù)器的數(shù)值減少1,當(dāng)計(jì)數(shù)器等數(shù)零時(shí),釋放所有的線程。
void await():調(diào)所該方法阻塞當(dāng)前主線程,直到計(jì)數(shù)器減少為零。
代碼例子:
線程類(lèi):
import java.util.concurrent.CountDownLatch; public class TestThread extends Thread{ CountDownLatch cd; String threadName; public TestThread(CountDownLatch cd,String threadName){ this.cd=cd; this.threadName=threadName; } @Override public void run() { System.out.println(threadName+" start working..."); dowork(); System.out.println(threadName+" end working and exit..."); cd.countDown();//告訴同步類(lèi)完成一個(gè)線程操作完成 } private void dowork(){ try { Thread.sleep(2000); System.out.println(threadName+" is working..."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
測(cè)試類(lèi):
import java.util.concurrent.CountDownLatch; public class TsetCountDownLatch { public static void main(String[] args) { try { CountDownLatch cd = new CountDownLatch(3);// 表示一共有三個(gè)線程 TestThread thread1 = new TestThread(cd, "thread1"); TestThread thread2 = new TestThread(cd, "thread2"); TestThread thread3 = new TestThread(cd, "thread3"); thread1.start(); thread2.start(); thread3.start(); cd.await();//等待所有線程完成 System.out.println("All Thread finishd"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
輸出結(jié)果:
thread1 start working... thread2 start working... thread3 start working... thread2 is working... thread2 end working and exit... thread1 is working... thread3 is working... thread3 end working and exit... thread1 end working and exit... All Thread finishd
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- java多線程CountDownLatch與線程池ThreadPoolExecutor/ExecutorService案例
- Java countDownLatch如何實(shí)現(xiàn)多線程任務(wù)阻塞等待
- 如何使用CountDownLatch同步j(luò)ava多線程
- java使用CountDownLatch等待多線程全部執(zhí)行完成
- JAVA多線程CountDownLatch使用詳解
- Java中CountDownLatch進(jìn)行多線程同步詳解及實(shí)例代碼
- 詳解Java多線程編程中CountDownLatch阻塞線程的方法
- Java多線程編程之CountDownLatch同步工具使用實(shí)例
- Java多線程之同步工具類(lèi)CountDownLatch
相關(guān)文章
淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏
脫敏是指在不改變?cè)瓟?shù)據(jù)結(jié)構(gòu)的前提下,通過(guò)某種方式處理數(shù)據(jù),使數(shù)據(jù)不能直接暴露用戶的真實(shí)信息,下面我們就來(lái)看看SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏的具體方法吧2024-03-03SpringBoot FailureAnalyzer實(shí)例使用教程
FailureAnalyzer是一種在啟動(dòng)時(shí)攔截exception并將其轉(zhuǎn)換為human-readable消息的好方法,包含在故障分析中。SpringBoot為application context相關(guān)的exceptions,JSR-303驗(yàn)證等提供了這樣的分析器,實(shí)際上很容易創(chuàng)建自己的2022-12-12Java如何獲取對(duì)象屬性及對(duì)應(yīng)值
這篇文章主要介紹了Java如何獲取對(duì)象屬性及對(duì)應(yīng)值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Java concurrency之公平鎖(二)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency之公平鎖的第二篇內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Java中的InputStreamReader和OutputStreamWriter源碼分析_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本文通過(guò)示例代碼給大家解析了Java中的InputStreamReader和OutputStreamWriter知識(shí),需要的的朋友參考下吧2017-05-05jsp、struts、spring、mybatis實(shí)現(xiàn)前端頁(yè)面功能模塊化拆分的方案
這篇文章主要介紹了 jsp、struts、spring、mybatis實(shí)現(xiàn)前端頁(yè)面功能模塊化拆分的方案,非常不錯(cuò),需要的朋友參考下2017-01-01基于mybatis中test條件中單引號(hào)雙引號(hào)的問(wèn)題
這篇文章主要介紹了基于mybatis中test條件中單引號(hào)雙引號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01詳解java==運(yùn)算符和equals()方法的區(qū)別
這篇文章主要介紹了java==運(yùn)算符和equals()方法的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03java批量導(dǎo)入Excel數(shù)據(jù)超詳細(xì)實(shí)例
這篇文章主要給大家介紹了關(guān)于java批量導(dǎo)入Excel數(shù)據(jù)的相關(guān)資料,EXCEL導(dǎo)入就是文件導(dǎo)入,操作代碼是一樣的,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-08-08