JAVA CountDownLatch(倒計時計數(shù)器)用法實例
這篇文章主要介紹了JAVA CountDownLatch(倒計時計數(shù)器)用法實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
方法說明:
public void countDown()
遞減鎖存器的計數(shù),如果計數(shù)到達零,則釋放所有等待的線程。如果當前計數(shù)大于零,則將計數(shù)減少。如果新的計數(shù)為零,出于線程調(diào)度目的,將重新啟用所有的等待線程。
如果當前計數(shù)等于零,則不發(fā)生任何操作。
public boolean await(long timeout, TimeUnit unit)throws InterruptedException
使當前線程在鎖存器倒計數(shù)至零之前一直等待,除非線程被中斷或超出了指定的等待時間。如果當前計數(shù)為零,則此方法立刻返回 true 值。
如果當前計數(shù)大于零,則出于線程調(diào)度目的,將禁用當前線程,且在發(fā)生以下三種情況之一前,該線程將一直處于休眠狀態(tài):
由于調(diào)用 countDown() 方法,計數(shù)到達零;或者其他某個線程中斷當前線程;或者已超出指定的等待時間。
* 如果計數(shù)到達零,則該方法返回 true 值。
* 如果當前線程,在進入此方法時已經(jīng)設置了該線程的中斷狀態(tài);或者在等待時被中斷, 則拋出 InterruptedException,并且清除當前線程的已中斷狀態(tài)。
* 如果超出了指定的等待時間,則返回值為 false。如果該時間小于等于零,則此方法根本不會等待。
參數(shù):
timeout - 要等待的最長時間
unit - timeout 參數(shù)的時間單位。
返回:
如果計數(shù)到達零,則返回 true;如果在計數(shù)到達零之前超過了等待時間,則返回 false
拋出:
InterruptedException - 如果當前線程在等待時被中斷
例子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(); // 當前線程調(diào)用此方法,則計數(shù)減一 } catch (InterruptedException e) { e.printStackTrace(); } } }; service.execute(runnable); } try { System. out.println("主線程" + Thread.currentThread().getName() + "等待子線程執(zhí)行完成..." ); latch.await(); // 阻塞當前線程,直到計時器的值為0 System. out.println("主線程" + Thread.currentThread().getName() + "開始執(zhí)行..."); } catch (InterruptedException e) { e.printStackTrace(); } } }
例子2:
百米賽跑,4名運動員選手到達場地等待裁判口令,裁判一聲口令,選手聽到后同時起跑,當所有選手到達終點,裁判進行匯總匯總排名。
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() + "到達終點"); 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ā)送口令,正在等待所有選手到達終點" ); cdAnswer.await(); System. out.println("所有選手都到達終點" ); System. out.println("裁判" + Thread.currentThread ().getName() + "匯總成績排名" ); } catch (Exception e) { e.printStackTrace(); } service.shutdown(); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
spring aop之@AfterReturning不生效問題及解決
這篇文章主要介紹了spring aop之@AfterReturning不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05使用位運算、值交換等方式反轉java字符串的多種方法(四種方法)
這篇文章主要介紹了使用位運算、值交換等方式反轉java字符串,本文通過四種方式給大家講解,給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Java中Collection和Collections的區(qū)別
Collection是一個集合接口,集合類的頂級接口,Collections是一個包裝類,本文主要介紹了Java中Collection和Collections的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-04-04Java并發(fā)編程之LongAdder執(zhí)行情況解析
這篇文章主要為大家介紹了Java并發(fā)編程之LongAdder執(zhí)行情況解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04