JAVA并发编程之CyclicBarrier

[size=medium]一.概述[/size]
[size=small]
使用JAVA编写并发程序的时候,我们需要仔细去思考一下并发流程的控制,如何让各个线程之间协作完成某项工作。有时候,我们启动N个线程去做一件事情,只有当这N个线程都达到某一个临界点的时候,我们才能继续下面的工作,就是说如果这N个线程中的某一个线程先到达预先定义好的临界点,它必须等待其他N-1线程也到达这个临界点,接下来的工作才能继续,只要这N个线程中有1个线程没有到达所谓的临界点,其他线程就算抢先到达了临界点,也只能等待,只有所有这N个线程都到达临界点后,接下来的事情才能继续。
在日常工作中,我们经常和别人一起合作做一些项目,项目中有一些关键点,比如说代码联调,假如有M个开发人员,要想进行联调,这M个开发人员都必须到达这个临界点——把自己的功能模块完成,否则就无法进行联调。
[/size]
[size=medium]二.CyclicBarrier的图示[/size]
[img]http://dl.iteye.com/upload/attachment/0081/6348/16f7800e-61ca-3073-b753-7b3d9755a87c.png[/img]
[size=small]
关于上面的图片,首先A,B,C,D四个线程跑起来之后,发现向前推进的速度不一样,在某一时刻发现A,B,C已经到达临界点,但是D还没有到达临界点,此时A,B,C只能等待,当D到达临界点之后,A,B,C,D才能一起越过屏障,进行下面的工作。
[/size]
[size=medium]三.分析一段CyclicBarrier的代码[/size]

import java.util.concurrent.CyclicBarrier;

class PerformTest {

private int threadCount; // 线程数目
private CyclicBarrier cycliBarrier;
private int loopCount = 10;

public PerformTest(int threadCount){
this.threadCount = threadCount;
// 创建一个新的屏障,只有所有线程达到临界点之后,这里的匿名线程对象才会被执行
this.cycliBarrier = new CyclicBarrier(this.threadCount, new Runnable() {

/**
* <pre>
* 当所有线程都达到某个点的时候,这个方法就会被调用
* </pre>
*/
@Override
public void run() {
collectTestResult();
}
});

for (int i = 0; i < this.threadCount; ++i) {
Thread t = new Thread("working thread " + i) {

@Override
public void run() {
for (int j = 0; j < loopCount; ++j) {
doTest();
try {
/* 在这里如果发现计数值不是0的话,就会发生线程的阻塞,注意体会这里对于
* 对于cycliBarrier的循环利用
*/
cycliBarrier.await();
} catch (Exception e) {
System.out.println(e);
}
}
}
};

t.start();
}
}

private void collectTestResult() {
System.out.println("All Threads have been tested, and the result has been collected!");
}

private void doTest() {
System.out.println(Thread.currentThread().getName() + " is tested!");
}
}

public class CyclicBarrierTest1 {

public static void main(String[] args) {
new PerformTest(2);
}
}

[size=small]
输出结果如下:
working thread 0 is tested!
working thread 1 is tested!
All Threads have been tested, and the result has been collected!
working thread 1 is tested!
working thread 0 is tested!
All Threads have been tested, and the result has been collected!
working thread 0 is tested!
working thread 1 is tested!
All Threads have been tested, and the result has been collected!
working thread 1 is tested!
working thread 0 is tested!
All Threads have been tested, and the result has been collected!
working thread 0 is tested!
working thread 1 is tested!
All Threads have been tested, and the result has been collected!
working thread 1 is tested!
working thread 0 is tested!
All Threads have been tested, and the result has been collected!
working thread 0 is tested!
working thread 1 is tested!
All Threads have been tested, and the result has been collected!
working thread 1 is tested!
working thread 0 is tested!
All Threads have been tested, and the result has been collected!
working thread 0 is tested!
working thread 1 is tested!
All Threads have been tested, and the result has been collected!
working thread 1 is tested!
working thread 0 is tested!
All Threads have been tested, and the result has been collected!
[/size]
[size=small]
下面我们来分析一下上述这段代码的输出结果:
在程序中我们创建的两个线程,working thread 1和working thread 2。这两个线程内部都有10次for循环,cycliBarrier的总数是线程数2。线程1运行起来,在进行一次for循环之后,线程就处于等待状态,此时cycliBarrier中等待的数目变为1,由于线程处于等待状态,此时线程1无法继续向下进行,过一会working thread 2开始运行,在第一次for循环中cycliBarrier中的等待数目变为0,此时线程working thread 1被唤醒,working thread 1继续第二次循环,在第二次循环开始时,cycliBarrier就开始重复利用了,当cycliBarrier的等待数目变为0后,cycliBarrier就会恢复初始状态,进行再一次的并发流程控制。后面的运行过程是一样的,至于cycliBarrier的等待计数为0后,working thread 1和working thread 2那个先开始运行,要看具体的调度机制了。
[/size]
[size=medium]四.CycliBarrier和CountDownLatch的区别[/size]
[size=small]
CycliBarrier和CountDownLatch都可以用来控制并发执行的流程,保证所有线程都做完自己的工作后在继续一些共性的工作。
[/size]
[size=small]
1.CycliBarrier可以重复利用,但是CountDownLatch不可以重复利用。
[/size]
[size=small]
2.CountDownLatch中计数值的递减需要自己手动去触发,但是CycliBarrier中计数值的减小是自动减小的。
[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值