CyclicBarrier是java推出的一个并发编程工具,它用在多个线程之间协同工作。线程约定到达某个点,到达这个点之后的线程都停下来,直到最后一个线程也到达了这个点之后,所有的线程才会得到释放。常用的场景是:多个worker线程,每个线程都在循环地做一部分工作,并在最后用cyclicBarrier.await()设下约定点,当最后一个线程做完了工作也到达约定点后,所有线程得到释放,开始下一轮工作。也就是下面这样:

 while(!done()){
//working
cyclicBarrier.await();
}

CyclicBarrier还支持一个回调函数,每当一轮工作结束后,下一轮工作开始前,这个回调函数都会被调用一次。

但是,使用CyclicBarrier必须准守最佳实践的使用方法,否则,就可能达不到想要的效果。比如,下面这样,就是一种典型的错误使用方法:

    private void process(CyclicBarrier cyclicBarrier) {
final int n = 100;
Runnable worker= new Runnable() {
@Override
public void run() { try {
//模拟工作
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
cyclicBarrier.await();
} catch (BrokenBarrierException | InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("Worker is done");
System.out.println("Thread of Worker is "+ Thread.currentThread().getId()); }; for (int i = 0; i < n; i++) {
Thread t1 = new Thread(worker);
Thread t2 = new Thread(worker);
t1.start();
t2.start();
} }

在上面的代码中,工作不在worker线程中循环,而是在开启工作的线程中循环,也就是说,它会不断地开启新的worker线程。这会导致的一个问题是,上一轮的回调还没执行完成,下一轮的工作就已经开始了。

那么为什么呢?下面来分析一下原因。

首先,要知道CyclicBarrier是如何做到在上一轮工作结束后下一轮工作开始前执行回调函数的。查看jdoc文档,里面有这么一句话“A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. ”这是描述回调函数的,从描述中可以看到,回调函数是在最后一个线程到达约定点后,线程释放前被执行的。也就是说,回调函数的执行时间发生在下一轮工作前,这是通过在执行完回调函数再释放工作线程来实现的。

然后,我们再来看看上面错误的使用方法。在错误的使用方法中,主线程的每一轮循环中都开启了新的worker线程,这样在回调函数结束之前,前面开启的worker线程确实没有得到释放,但是,新开启的工作线程却完全可以执行下一轮工作,这就是为什么在回调函数执行完毕之前,新一轮的工作就已经开始了的原因。并且,错误方法中的每一个工作线程只执行一轮工作就结束了,每一轮工作之间的线程互不影响,这也就失去了协作性,因此,千万要避免写出这种代码。

关于CyclicBarrier使用的最佳时间,基本上就是官方示例中的用法了,如下:

 class Solver {
final int N;
final float[][] data;
final CyclicBarrier barrier; class Worker implements Runnable {
int myRow;
Worker(int row) { myRow = row; }
public void run() {
while (!done()) {
processRow(myRow); try {
barrier.await();
} catch (InterruptedException ex) {
return;
} catch (BrokenBarrierException ex) {
return;
}
}
}
} public Solver(float[][] matrix) {
data = matrix;
N = matrix.length;
barrier = new CyclicBarrier(N,
new Runnable() {
public void run() {
mergeRows(...);
}
});
for (int i = 0; i < N; ++i)
new Thread(new Worker(i)).start(); waitUntilDone();
}
}

最后在有一个问题是,回调函数是在哪一个线程里执行的?

根据我的demo测试发现,是在第一个到达的线程中执行的。当然,官方并没有明确规定这一点,也许以后会有变化吧,所以,我们也不能以来这一特征。我的demo如下:

public class Demo1 {
  public static main(String[] args){
Demo1 demo = new Demo1();
demo1.showInfThreadWhenDirectly();
}
private void process(CyclicBarrier cyclicBarrier) {
final int n = 100;
Runnable worker= new Runnable() {
@Override
public void run() {
for (int i = 0; i < n; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
int arrival_index=cyclicBarrier.await();
if(0==arrival_index){
System.out.println("first arrival Thread in this iteration is: "
+Thread.currentThread().getId());
}
} catch (BrokenBarrierException | InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("Worker is done");
System.out.println("Thread of Worker is "+ Thread.currentThread().getId());
}
}; Thread t1 = new Thread(worker);
Thread t2 = new Thread(worker);
t1.start();
t2.start();
} public void showInfThreadWhenDirectly(){
CyclicBarrier cyclicBarrier = new CyclicBarrier(2, () ->
System.out.println("[Directly] Thread in invert call function is"
+ Thread.currentThread().getId()));
process(cyclicBarrier);
System.out.println("[Directly] main Thread is "+ Thread.currentThread().getId());
} }

输出结果如下:

[Directly] main Thread is 1
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is11
first arrival Thread in this iteration is: 11
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is10
first arrival Thread in this iteration is: 10
[Directly] Thread in invert call function is11
first arrival Thread in this iteration is: 11

另外,官方还有一段:“

If the barrier action does not rely on the parties being suspended when it is executed, then any of the threads in the party could execute that action when it is released. To facilitate this, each invocation of await() returns the arrival index of that thread at the barrier. You can then choose which thread should execute the barrier action, for example:

  if (barrier.await() == 0) {
// log the completion of this iteration
}

意思是说,如果回调动作“arrier action”不需要在所有工作线程都停止的状态下执行的话,那么可以随便找一个工作线程去做这个动作。为了支持这个,CyclicBarrier 的await( )方法有一个返回值,返回的就是当前线程是第几个到达约定点(barrier)的。

参考https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

CyclicBarrier正确的使用方法和错误的使用方法的更多相关文章

  1. Servlet常见错误及解决方法

    常见错误及解决方法 1. 404产生的原因为Web服务器(容器)根据请求地址找不到对应资源,以下情况都会出现404的错误提示: 输入的地址有误(应用名大小写不正确,名称拼写不正确) 在web.xml文 ...

  2. 关于启动Visual Studio 2010 旗舰版的几个错误的解决方法。

    关于启动Visual Studio 2010 旗舰版的几个错误的解决方法.亲测. 重做系统之后,今天是第一次打开Visual Studio 2010 旗舰版码代码,结果遇到几个弹出的对话框,现在与大家 ...

  3. http 500错误怎么解决方法

    出现500错误的原因是很多的,一般来说,如果程序出错,那么在浏览器内会返回给用户一个友好的错误提示,统一称之为服务器500错误. 解决的方法就是您必须在http中能够正确的获得错误信息,方法为:请打开 ...

  4. 微信jssdk常见错误及解决方法

    调用config 接口的时候传入参数 debug: true 可以开启debug模式,页面会alert出错误信息.以下为常见错误及解决方法: invalid url domain当前页面所在域名与使用 ...

  5. Web服务器(容器)请求常见的错误及其解决方法

    首先我们来看看容器如何找到service()方法?(1)当在浏览器中输入 http://localhost:8080/firstweb/sayHi 这个地址后,容器是如何找到 HelloServlet ...

  6. centos linux 系统日常管理4 scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法 第十七节课

    centos linux 系统日常管理4  scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法  第十七节课 rsync可以增量同步,scp不行 ...

  7. 编程中遇到的Python错误和解决方法汇总整理

    这篇文章主要介绍了自己编程中遇到的Python错误和解决方法汇总整理,本文收集整理了较多的案例,需要的朋友可以参考下   开个贴,用于记录平时经常碰到的Python的错误同时对导致错误的原因进行分析, ...

  8. VC6.0开发中一些链接错误的解决方法

    (1)error LNK2001: unresolved external symbol _main 编号:LNK2001 直译:未解决的外部符号:_main. 错误分析:缺少main函数.看看mai ...

  9. 常见的SQL错误和解决方法

    前言 今天你会看到每个人——从新手到专家——在使用SQL时犯的各种常见错误.你不能永远避免犯任何错误,但是熟悉广泛的错误将帮助你在尽可能短的时间内解决这些错误. 注:在我们的例子中我们使用的是Orac ...

随机推荐

  1. IKAnalyzer兼容Lucene 5.4.0版本抛出异常?

    ava.lang.AbstractMethodError: org.apache.lucene.analysis.Analyzer.createComponents(Ljava/lang/String ...

  2. Javascript的事件模型和Promise实现

    1. Javascript的运行时模型——事件循环 JS的运行时是个单线程的运行时,它不像其他编程语言,比如C++,Java,C#这些可以进行多线程操作的语言.当它执行一个函数时,它只会一条路走到黑, ...

  3. Angular之constructor和ngOnInit差异及适用场景

    constructor会在类生成实例时调用,Angular无法控制constructor,constructor中应该只进行依赖注入而不是进行真正的业务操作 ngOnInit属于Angular生命周期 ...

  4. .Net WebApi部署问题

    在IIS上部署web api 完成后,浏览时出现了“The compiler failed with error code -2146232576.”的错误(有时会出现这个情况).主要是 我们在.Ne ...

  5. Pi 实现VLC网络视频监控

    安装摄像头 将Pi的原装摄像头安装在pi上面 ssh连接pi 通过xshell或者putty等工具连接ssh 安装VLC $ sudo apt-get update $ sudo apt-get in ...

  6. artemplate模板

    artTemplate 介绍 最近使用了一款模板,学习成本比较低,性能也不错.觉得很好用,所以推荐给大家. artTemplate 是新一代 javascript 模板引擎,它采用预编译方式让性能有了 ...

  7. iOS 界面布局

    1. auto layout http://www.devtalking.com/articles/adaptive-layout-for-iphone6-1/ http://blog.sina.co ...

  8. mysql--pymysql 模块

    pymysql模块 一.安装 cmd中使用pip或者, pycharm中控制台选择Terminal输入下面的命令,即可安装pymysql模块 pip3 install pymysql 二. 连接,执行 ...

  9. “全栈2019”Java第九十章:内部类可以向上或向下转型吗?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  10. Get 和 Post 方法的选择和URL的设计

    原文链接:http://yifei.me/note/540 HTTP 中常用的方法有 GET/POST/PUT/DELETE 等,在设计API或者表单的时候我们需要选择合适的方法.一般有两种方案: 只 ...