国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

實(shí)例講解Java并發(fā)編程之閉鎖

2019-11-26 15:13:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

閉鎖相當(dāng)于一扇門,在閉鎖到達(dá)結(jié)束狀態(tài)之前,這扇門一直是關(guān)閉著的,沒(méi)有任何線程可以通過(guò),當(dāng)?shù)竭_(dá)結(jié)束狀態(tài)時(shí),這扇門才會(huì)打開并容許所有線程通過(guò)。它可以使一個(gè)或多個(gè)線程等待一組事件發(fā)生。閉鎖狀態(tài)包括一個(gè)計(jì)數(shù)器,初始化為一個(gè)正式,正數(shù)表示需要等待的事件數(shù)量。countDown方法遞減計(jì)數(shù)器,表示一個(gè)事件已經(jīng)發(fā)生,而await方法等待計(jì)數(shù)器到達(dá)0,表示等待的事件已經(jīng)發(fā)生。CountDownLatch強(qiáng)調(diào)的是一個(gè)線程(或多個(gè))需要等待另外的n個(gè)線程干完某件事情之后才能繼續(xù)執(zhí)行。

場(chǎng)景應(yīng)用:
10個(gè)運(yùn)動(dòng)員準(zhǔn)備賽跑,他們等待裁判一聲令下就開始同時(shí)跑,當(dāng)最后一個(gè)人通過(guò)終點(diǎn)的時(shí)候,比賽結(jié)束。10個(gè)運(yùn)動(dòng)相當(dāng)于10個(gè)線程,這里關(guān)鍵是控制10個(gè)線程同時(shí)跑起來(lái),還有怎么判斷最后一個(gè)線程到達(dá)終點(diǎn)。可以用2個(gè)閉鎖,第一個(gè)閉鎖用來(lái)控制10個(gè)線程等待裁判的命令,第二個(gè)閉鎖控制比賽結(jié)束。

import java.util.concurrent.CountDownLatch; class Aworker implements Runnable { private int num; private CountDownLatch begin; private CountDownLatch end;  public Aworker(int num, final CountDownLatch begin, final CountDownLatch end) { this.num = num; this.begin = begin; this.end = end; }  @Override public void run() { // TODO Auto-generated method stub try {  System.out.println(num + "th people is ready");  begin.await();  //準(zhǔn)備就緒 } catch (InterruptedException e) {  e.printStackTrace(); } finally {  end.countDown();  //計(jì)數(shù)器減一,到達(dá)終點(diǎn)  System.out.println(num + "th people arrive"); } }} public class Race { public static void main(String[] args) { int num = 10; CountDownLatch begin = new CountDownLatch(1); CountDownLatch end = new CountDownLatch(num);  for (int i = 1; i <= num; i++) {  new Thread(new Aworker(i, begin, end)).start(); }  try {  Thread.sleep((long) (Math.random() * 5000)); } catch (InterruptedException e1) {  // TODO Auto-generated catch block  e1.printStackTrace(); }  System.out.println("judge say : run !"); begin.countDown(); //裁判一聲令下開始跑 long startTime = System.nanoTime(); try {  end.await(); //等待結(jié)束 } catch (InterruptedException e) {  // TODO Auto-generated catch block  e.printStackTrace(); } finally {  long endTime = System.nanoTime();  System.out.println("judge say : all arrived !");  System.out.println("spend time: " + (endTime - startTime)); } }}

輸出

1th people is ready2th people is ready4th people is ready6th people is ready3th people is ready10th people is ready8th people is ready5th people is ready7th people is ready9th people is readyjudge say : run !1th people arrive4th people arrive10th people arrive5th people arrive2th people arrivejudge say : all arrived !9th people arrive7th people arrive8th people arrive3th people arrive6th people arrivespend time: 970933
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹阳市| 汝州市| 泰和县| 鹿邑县| 前郭尔| 当雄县| 石景山区| 黔南| 平遥县| 武胜县| 惠水县| 德化县| 南郑县| 明水县| 阿荣旗| 贵溪市| 长宁县| 天水市| 双城市| 济源市| 邹平县| 九龙坡区| 清丰县| 温泉县| 镇原县| 华蓥市| 黑河市| 黔西| 威海市| 财经| 济宁市| 张家界市| 秦皇岛市| 博客| 固始县| 广宁县| 广丰县| 抚州市| 石家庄市| 乌鲁木齐县| 汉中市|