关于CountDownLatch 和CountDownLatch 与 CyclicBarrier的区别

CountDownLatch是同步工具类之一,可以指定一个计数值,在并发环境下由线程进行减1操作,当计数值变为0之后,被await方法阻塞的线程将会唤醒,实现线程间的同步。主线程启动10个子线程后阻塞在await方法,需要等子线程都执行完毕,主线程才能唤醒继续执行。

测试代码如下:

	public static void main(String[] args) throws InterruptedException {
		int threadNum = 10;
		final CountDownLatch countDownLatch = new CountDownLatch(threadNum);

		for (int i = 0; i < threadNum; i++) {
			final int finalI = i + 1;
			new Thread(new Runnable() {

				public void run() {
					System.out.println("thread " + finalI + " start");
					Random random = new Random();
					try {
						Thread.sleep(random.nextInt(10000) + 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("thread " + finalI + " finish");

					countDownLatch.countDown();
				}
			}).start();
		}

		try {
			countDownLatch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(threadNum + " thread finish");
	}

测试结果:

thread 1 start
thread 3 start
thread 4 start
thread 5 start
thread 9 start
thread 7 start
thread 2 start
thread 6 start
thread 8 start
thread 10 start
thread 10 finish
thread 5 finish
thread 8 finish
thread 3 finish
thread 7 finish
thread 9 finish
thread 2 finish
thread 4 finish
thread 6 finish
thread 1 finish
10 thread finish

从上面例子可以看出,启动打印 线程1 启动,执行 countDown方法,等待线程数减1 ,for循环继续跑,线程1 遇到await方法进行等待countDownLatch等待数为0,才会被唤醒,唤醒为随机


关于 CyclicBarrier和CountDownLatch区别



CyclicBarrier和CountDownLatch 都位于java.util.concurrent 这个包下


CountDownLatchCyclicBarrier
减计数方式加计数方式
计算为0时释放所有等待的线程计数达到指定值时释放所有等待线程
计数为0时,无法重置计数达到指定值时,计数置为0重新开始
调用countDown()方法计数减一,调用await()方法只进行阻塞,对计数没任何影响调用await()方法计数加1,若加1后的值不等于构造方法的值,则线程阻塞
不可重复利用可重复利用

具体参考 :http://blog.csdn.net/tolcf/article/details/50925145





                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值