新的起點(diǎn) 新的開始

          快樂生活 !

          學(xué)習(xí)實(shí)踐 JDK5 concurrent 并行包之CyclicBarrier

          本部分介紹CyclicBarrier類,該類通ReentrantLock 跟Condition。Barrier 顧名思義,只有所有線程數(shù)達(dá)到規(guī)定的數(shù)量時(shí),它才會被觸發(fā)執(zhí)行。
          如下段代碼 只有線程12都執(zhí)行時(shí),才會執(zhí)行它定義的線程。

          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 閱讀(1462) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          公告

          Locations of visitors to this pageBlogJava
        1. 首頁
        2. 新隨筆
        3. 聯(lián)系
        4. 聚合
        5. 管理
        6. <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          統(tǒng)計(jì)

          常用鏈接

          留言簿(13)

          隨筆分類(71)

          隨筆檔案(179)

          文章檔案(13)

          新聞分類

          IT人的英語學(xué)習(xí)網(wǎng)站

          JAVA站點(diǎn)

          優(yōu)秀個(gè)人博客鏈接

          官網(wǎng)學(xué)習(xí)站點(diǎn)

          生活工作站點(diǎn)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 华容县| 晋中市| 威远县| 云龙县| 黄浦区| 昭通市| 罗江县| 道真| 行唐县| 七台河市| 康保县| 广安市| 铜鼓县| 右玉县| 洛扎县| 东海县| 张掖市| 金湖县| 自贡市| 江源县| 小金县| 武清区| 锦屏县| 昔阳县| 十堰市| 开江县| 绥阳县| 彰化市| 长兴县| 进贤县| 雷州市| 安阳市| 海口市| 曲水县| 深圳市| 宜阳县| 洪泽县| 囊谦县| 衡东县| 项城市| 漳平市|