CyclicBarrier的使用实例

CyclicBarrier允许给定数量的线程全部到达关卡点时,关卡就被成功突破,关卡被重置以备下一次使用。跟CountDownlatch特别类似,但是CountDownLatch不同的是不能被重新重置再使用。

 

前面的blog说到用countdownlatch去完成方法里面的3个service的调用。用cyclicBarrier同样可以实现。

 

package cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 *
 *<p>Test</p>
 *<p>Description:</P>
 *<p>Company:</p>
 *<p>Department:CAS</p>
 *@Author: Tommy Zhou
 *@Since: 1.0
 *@Version:Date:2011-4-26
 *
 **/
public class CyclicBarrierSample {
   
    public static void main(String[] args) {
        String[] strs = getResult();
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
      
    }
  
    public static String[] getResult(){
        String[] strs = new String[3];
        CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
        Work1 work1 = new Work1(cyclicBarrier,strs[0]);
        Work2 work2 = new Work2(cyclicBarrier,strs[1]);
        Work3 work3 = new Work3(cyclicBarrier,strs[2]);
        work1.start();
        work2.start();
        work3.start();
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        strs[0] = work1.str1;
        strs[1] = work2.str2;
        strs[2] = work3.str3;
        return strs;
    }
}

class Work1 extends Thread{
    public String str1;
    public CyclicBarrier cyclicBarrier1;
    public Work1(CyclicBarrier cyclicBarrier1,String str1){
        this.cyclicBarrier1 = cyclicBarrier1;
        this.str1 = str1;
    }
  
  
    public void run(){
        str1="work1";
        try {
            cyclicBarrier1.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class Work2 extends Thread{
    public String str2;
    public CyclicBarrier cyclicBarrier2;
    public Work2(CyclicBarrier cyclicBarrier2,String str2){
        this.cyclicBarrier2 = cyclicBarrier2;
        this.str2 = str2;
    }
  
  
    public void run(){
        try {
            Thread.sleep(3000);
            str2="work2";
            cyclicBarrier2.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
}

class Work3 extends Thread{
    public String str3;
    public CyclicBarrier cyclicBarrier3;
    public Work3(CyclicBarrier cyclicBarrier3,String str3){
        this.cyclicBarrier3 = cyclicBarrier3;
        this.str3 = str3;
    }
  
  
    public void run(){
        try {
            Thread.sleep(3000);
            str3="work3";
            cyclicBarrier3.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
    }
}

你可能感兴趣的:(thread,Blog)