<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    新的起點 新的開始

    快樂生活 !

    學習實踐 JDK5 concurrent 并行包之CyclicBarrier

    本部分介紹CyclicBarrier類,該類通ReentrantLock 跟Condition。Barrier 顧名思義,只有所有線程數達到規定的數量時,它才會被觸發執行。
    如下段代碼 只有線程12都執行時,才會執行它定義的線程。

    1 package net.vincent.study.other;
     2 
     3 import java.util.concurrent.BrokenBarrierException;
     4 import java.util.concurrent.CyclicBarrier;
     5 
     6 public class part1 {
     7 
     8     /**
     9      * This static mehthod create CyclicBarrier, and this barrier will sleep 1000 if number of count arrive 
    10      * @param number of count 
    11      * @return a CyclicBarrier that will sleep if number of await thread invoke. 
    12      */
    13 
    14     public static CyclicBarrier getCyclicBarrier(int count){
    15         if(count <=0)return null;
    16          final CyclicBarrier cyclicBarrier = new CyclicBarrier(count,new Runnable(){
    17             public void run(){
    18                 try {
    19                     Thread.sleep(1000);
    20                 } catch (InterruptedException e) {
    21                     e.printStackTrace();
    22                 }
    23                 System.out.println("conditon is arrive and CycleBarrier is running");
    24             }
    25         });
    26         return cyclicBarrier;
    27     }
    28     /**
    29      * Create
    30      * @param nameOfThread
    31      * @param cyclicBarrier
    32      * @return
    33      */
    34     public static Thread getThread(String nameOfThread ,final CyclicBarrier  cyclicBarrier ){
    35         Thread thread= new Thread(nameOfThread){
    36         public void run(){
    37                 System.out.println(this.getName()+"is begin; and count is "+(++count));
    38             try {
    39                 cyclicBarrier.await();
    40             } catch (InterruptedException e) {
    41                 // TODO Auto-generated catch block
    42                 e.printStackTrace();
    43             } catch (BrokenBarrierException e) {
    44                 // TODO Auto-generated catch block
    45                 e.printStackTrace();
    46             }
    47             System.out.println(this.getName()+"finished");
    48             }
    49         };
    50         return thread;
    51         
    52     }
    53     
    54     static  int count = 0;
    55     public static void main(String[] args) {
    56         /** define a cyclicBarrier and number of barrier is 2.*/
    57         CyclicBarrier cyclicBarrier  = getCyclicBarrier(2);
    58         Thread threadOne = getThread("threadOne",cyclicBarrier);
    59         threadOne.start();
    60         Thread threadTwo = getThread("threadTwo",cyclicBarrier);
    61         threadTwo.start();
    62 
    63 
    64     }
    65 
    66 
    67 }
    68 

    posted on 2007-09-28 14:51 advincenting 閱讀(1461) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    公告

    Locations of visitors to this pageBlogJava
  • 首頁
  • 新隨筆
  • 聯系
  • 聚合
  • 管理
  • <2007年9月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    統計

    常用鏈接

    留言簿(13)

    隨筆分類(71)

    隨筆檔案(179)

    文章檔案(13)

    新聞分類

    IT人的英語學習網站

    JAVA站點

    優秀個人博客鏈接

    官網學習站點

    生活工作站點

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 最新免费jlzzjlzz在线播放| 亚洲精品资源在线| 亚洲日韩精品无码专区加勒比☆| 91久久成人免费| 亚洲欧洲日产国码二区首页 | 色婷婷六月亚洲婷婷丁香| 天黑黑影院在线观看视频高清免费| 国产aⅴ无码专区亚洲av麻豆| 皇色在线免费视频| 国产亚洲美女精品久久久2020| 国产精品永久免费视频| 亚洲精品午夜国产VA久久成人| 国产免费拔擦拔擦8X高清在线人| 亚洲妇熟XXXX妇色黄| 99久在线国内在线播放免费观看 | 亚洲人成人无码网www国产| 免费夜色污私人影院网站| 亚洲色欲久久久久综合网| 国产免费MV大全视频网站| 亚洲AV无码久久精品狠狠爱浪潮| 免费无遮挡无码永久视频 | 亚洲精品资源在线| 啦啦啦中文在线观看电视剧免费版| 亚洲一区二区免费视频| 国产精品免费看久久久久| 高清永久免费观看| 久久久亚洲欧洲日产国码aⅴ| 97性无码区免费| 边摸边吃奶边做爽免费视频99 | 亚洲精品乱码久久久久久下载| 99精品视频在线观看免费播放 | 亚洲欧洲精品一区二区三区| 日本免费A级毛一片| aaa毛片视频免费观看| 亚洲人成在线免费观看| 午夜一级毛片免费视频| 中国极品美軳免费观看| 亚洲欧洲高清有无| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 国产精品高清免费网站| 亚洲网站在线播放|