最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年04月13日 10:50:45   投稿:junjie  
這篇文章主要介紹了實例講解Java并發(fā)編程之閉鎖,閉鎖相當(dāng)于一扇門,在閉鎖到達結(jié)束狀態(tài)之前,這扇門一直是關(guān)閉著的,沒有任何線程可以通過,當(dāng)?shù)竭_結(jié)束狀態(tài)時,這扇門才會打開并容許所有線程通過,需要的朋友可以參考下

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

場景應(yīng)用:
10個運動員準(zhǔn)備賽跑,他們等待裁判一聲令下就開始同時跑,當(dāng)最后一個人通過終點的時候,比賽結(jié)束。10個運動相當(dāng)于10個線程,這里關(guān)鍵是控制10個線程同時跑起來,還有怎么判斷最后一個線程到達終點??梢杂?個閉鎖,第一個閉鎖用來控制10個線程等待裁判的命令,第二個閉鎖控制比賽結(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();  //計數(shù)器減一,到達終點
  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 ready
2th people is ready
4th people is ready
6th people is ready
3th people is ready
10th people is ready
8th people is ready
5th people is ready
7th people is ready
9th people is ready
judge say : run !
1th people arrive
4th people arrive
10th people arrive
5th people arrive
2th people arrive
judge say : all arrived !
9th people arrive
7th people arrive
8th people arrive
3th people arrive
6th people arrive
spend time: 970933

相關(guān)文章

  • SpringBoot與SpringSecurity整合方法附源碼

    SpringBoot與SpringSecurity整合方法附源碼

    這篇文章主要介紹了SpringBoot與SpringSecurity整合,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Java?OpenCV圖像處理之自定義圖像濾波算子

    Java?OpenCV圖像處理之自定義圖像濾波算子

    這篇文章主要為大家介紹了如何利用Java?OpenCV實現(xiàn)自定義圖像濾波(降噪)?算子,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2022-02-02
  • java實現(xiàn)簡單的汽車租賃系統(tǒng)

    java實現(xiàn)簡單的汽車租賃系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)簡單的汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot解決Required?String?parameter?xxx?is?not?present問題

    SpringBoot解決Required?String?parameter?xxx?is?not?prese

    這篇文章主要介紹了SpringBoot解決Required?String?parameter?xxx?is?not?present問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring 4.1+JSONP的使用指南

    Spring 4.1+JSONP的使用指南

    在解釋JSONP之前,我們需要了解下”同源策略“,這對理解跨域有幫助?;诎踩脑?瀏覽器是存在同源策略機制的,同源策略阻止從一個源加載的文檔或腳本獲取或設(shè)置另一個源加載額文檔的屬性。說的簡單點就是瀏覽器限制腳本只能和同協(xié)議、同域名、同端口的腳本進行交互。
    2016-04-04
  • SpringBoot 普通類調(diào)用Bean對象的一種方式推薦

    SpringBoot 普通類調(diào)用Bean對象的一種方式推薦

    這篇文章主要介紹了SpringBoot 普通類調(diào)用Bean對象的一種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot使用外置的Servlet容器的方法步驟

    SpringBoot使用外置的Servlet容器的方法步驟

    SpringBoot 是一個非常流行的 Java 開發(fā)框架,它提供了一個簡單而強大的方式來創(chuàng)建基于 Servlet 容器的 Web 應(yīng)用程序,本文將介紹 SpringBoot 中如何使用 Servlet 容器,需要的朋友可以參考下
    2024-12-12
  • 使用Maven配置Spring的方法步驟

    使用Maven配置Spring的方法步驟

    這篇文章主要介紹了使用Maven配置Spring的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • 深入理解Java設(shè)計模式之適配器模式

    深入理解Java設(shè)計模式之適配器模式

    這篇文章主要介紹了JAVA設(shè)計模式之適配器模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2021-11-11
  • SpringBoot多環(huán)境打包與配置文件排除實踐記錄

    SpringBoot多環(huán)境打包與配置文件排除實踐記錄

    本文介紹了SpringBoot項目多環(huán)境打包與配置文件排除實踐,包括多環(huán)境配置的實現(xiàn)方法、打包時排除配置文件的方法以及動態(tài)加載外部配置文件的最佳實踐,感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評論

读书| 湘阴县| 贡山| 云南省| 阜城县| 林口县| 项城市| 茂名市| 丹江口市| 双鸭山市| 青浦区| 搜索| 肃宁县| 赣州市| 聊城市| 建阳市| 屯昌县| 赤城县| 西乌| 剑河县| 桐梓县| 衡山县| 安乡县| 金山区| 田林县| 婺源县| 山阳县| 四平市| 呼图壁县| 施甸县| 乐清市| 湾仔区| 夏邑县| 上林县| 泸定县| 双柏县| 韩城市| 陵川县| 青铜峡市| 乌拉特后旗| 永济市|