CountDownLatch如何在JAVA中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)是一家專業(yè)提供鐘祥企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、網(wǎng)站設(shè)計(jì)、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為鐘祥眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
方法說明:
public void countDown()
遞減鎖存器的計(jì)數(shù),如果計(jì)數(shù)到達(dá)零,則釋放所有等待的線程。如果當(dāng)前計(jì)數(shù)大于零,則將計(jì)數(shù)減少。如果新的計(jì)數(shù)為零,出于線程調(diào)度目的,將重新啟用所有的等待線程。
如果當(dāng)前計(jì)數(shù)等于零,則不發(fā)生任何操作。
public boolean await(long timeout, TimeUnit unit)throws InterruptedException
使當(dāng)前線程在鎖存器倒計(jì)數(shù)至零之前一直等待,除非線程被中斷或超出了指定的等待時(shí)間。如果當(dāng)前計(jì)數(shù)為零,則此方法立刻返回 true 值。
如果當(dāng)前計(jì)數(shù)大于零,則出于線程調(diào)度目的,將禁用當(dāng)前線程,且在發(fā)生以下三種情況之一前,該線程將一直處于休眠狀態(tài):
由于調(diào)用 countDown() 方法,計(jì)數(shù)到達(dá)零;或者其他某個(gè)線程中斷當(dāng)前線程;或者已超出指定的等待時(shí)間。
* 如果計(jì)數(shù)到達(dá)零,則該方法返回 true 值。
* 如果當(dāng)前線程,在進(jìn)入此方法時(shí)已經(jīng)設(shè)置了該線程的中斷狀態(tài);或者在等待時(shí)被中斷, 則拋出 InterruptedException,并且清除當(dāng)前線程的已中斷狀態(tài)。
* 如果超出了指定的等待時(shí)間,則返回值為 false。如果該時(shí)間小于等于零,則此方法根本不會(huì)等待。
參數(shù):
timeout - 要等待的最長(zhǎng)時(shí)間
unit - timeout 參數(shù)的時(shí)間單位。
返回:
如果計(jì)數(shù)到達(dá)零,則返回 true;如果在計(jì)數(shù)到達(dá)零之前超過了等待時(shí)間,則返回 false
拋出:
InterruptedException - 如果當(dāng)前線程在等待時(shí)被中斷
例子1:
主線程等待子線程執(zhí)行完成在執(zhí)行。
import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountdownLatchTest1 { public static void main(String[] args) { ExecutorService service = Executors. newFixedThreadPool(3); final CountDownLatch latch = new CountDownLatch(3); for (int i = 0; i < 3; i++) { Runnable runnable = new Runnable() { @Override public void run() { try { System. out.println("子線程" + Thread.currentThread().getName() + "開始執(zhí)行"); Thread. sleep((long) (Math. random() * 10000)); System. out.println("子線程" + Thread.currentThread().getName() + "執(zhí)行完成"); latch.countDown(); // 當(dāng)前線程調(diào)用此方法,則計(jì)數(shù)減一 } catch (InterruptedException e) { e.printStackTrace(); } } }; service.execute(runnable); } try { System. out.println("主線程" + Thread.currentThread().getName() + "等待子線程執(zhí)行完成..." ); latch.await(); // 阻塞當(dāng)前線程,直到計(jì)時(shí)器的值為0 System. out.println("主線程" + Thread.currentThread().getName() + "開始執(zhí)行..."); } catch (InterruptedException e) { e.printStackTrace(); } } }
例子2:
百米賽跑,4名運(yùn)動(dòng)員選手到達(dá)場(chǎng)地等待裁判口令,裁判一聲口令,選手聽到后同時(shí)起跑,當(dāng)所有選手到達(dá)終點(diǎn),裁判進(jìn)行匯總匯總排名。
import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountdownLatchTest2 { public static void main(String[] args) { ExecutorService service = Executors. newCachedThreadPool(); final CountDownLatch cdOrder = new CountDownLatch(1); final CountDownLatch cdAnswer = new CountDownLatch(4); for (int i = 0; i < 4; i++) { Runnable runnable = new Runnable() { public void run() { try { System. out.println("選手" + Thread.currentThread().getName() + "正等待裁判發(fā)布口令"); cdOrder.await(); System. out.println("選手" + Thread.currentThread().getName() + "已接受裁判口令"); Thread. sleep((long) (Math. random() * 10000)); System. out.println("選手" + Thread.currentThread().getName() + "到達(dá)終點(diǎn)"); cdAnswer.countDown(); } catch (Exception e) { e.printStackTrace(); } } }; service.execute(runnable); } try { Thread. sleep((long) (Math. random() * 10000)); System. out.println("裁判" + Thread.currentThread ().getName() + "即將發(fā)布口令" ); cdOrder.countDown(); System. out.println("裁判" + Thread.currentThread ().getName() + "已發(fā)送口令,正在等待所有選手到達(dá)終點(diǎn)" ); cdAnswer.await(); System. out.println("所有選手都到達(dá)終點(diǎn)" ); System. out.println("裁判" + Thread.currentThread ().getName() + "匯總成績(jī)排名" ); } catch (Exception e) { e.printStackTrace(); } service.shutdown(); } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享題目:CountDownLatch如何在JAVA中使用
本文地址:http://chinadenli.net/article18/pdsegp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、軟件開發(fā)、靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、電子商務(wù)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)