Java CountDownLatch和CyclicBarrier 学习

CountDownLatch : 一个线程(或者多个), 等待另外N个线程完成某个事情之后才能执行。   CyclicBarrier        : N个线程相互等待,任何一个线程完成之前,所有的线程都必须等待。

 

 

 

CountDownLatch 是计数器, 线程完成一个就记一个, 就像 报数一样, 只不过是递减的.
而CyclicBarrier更像一个水闸, 线程执行就想水流, 在水闸处都会堵住, 等到水满(线程到齐)了, 才开始泄流.

 

 

/**  
 * Project Name:rop-client-P4Ux  
 * File Name:CyclicBarrierTest.java  
 * Package Name:com.zte.test  
 * Date:2015-4-15下午1:11:09  
 * Copyright (c) 2015,   All Rights Reserved.  
 *  
*/

package com.zte.test;

/**  
 * ClassName:CyclicBarrierTest <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2015-4-15 下午1:11:09 <br/>  
 * @author    
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicBarrierTest
{
    public static void main(String[] args)
    {
        ExecutorService service = Executors.newCachedThreadPool();
        final CyclicBarrier cb = new CyclicBarrier(3); //三个线程同时到达
        for (int i = 0; i < 3; i++)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    try
                    {
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("线程" + Thread.currentThread().getName() + "即将到达集合地点1,当前已有"
                                + (cb.getNumberWaiting() + 1) + "个已到达"
                                + (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊" : "正在等候"));
                        try
                        {
                            cb.await();
                        }
                        catch (BrokenBarrierException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("线程" + Thread.currentThread().getName() + "即将到达集合地点2,当前已有"
                                + (cb.getNumberWaiting() + 1) + "个已到达"
                                + (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊" : "正在等候"));
                        try
                        {
                            cb.await();
                        }
                        catch (BrokenBarrierException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("线程" + Thread.currentThread().getName() + "即将到达集合地点3,当前已有"
                                + (cb.getNumberWaiting() + 1) + "个已到达"
                                + (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊" : "正在等候"));
                        try
                        {
                            cb.await();
                        }
                        catch (BrokenBarrierException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            service.execute(runnable);
        }
        service.shutdown();
    }
}

 

 

你可能感兴趣的:(CountDownLatch)