博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
同步屏障CyclicBarrier
阅读量:1952 次
发布时间:2019-04-27

本文共 4139 字,大约阅读时间需要 13 分钟。

CyclicBarrier,根据字面意思理解:循环屏障。屏障的意思:CyclicBarrier可以让一组线程到达某个屏障点时被阻塞,直到最后一个线程到达屏障点时,屏障才会放开,所有被屏障阻塞的线程才开始执行任务;循环:当所有线程被释放后,这个CyclicBarrier可以被重用。
有两个很常用的构造方法:
第一个:
/**     * Creates a new {@code CyclicBarrier} that will trip when the     * given number of parties (threads) are waiting upon it, and     * does not perform a predefined action when the barrier is tripped.     *     * @param parties the number of threads that must invoke {@link #await}     *        before the barrier is tripped     * @throws IllegalArgumentException if {@code parties} is less than 1     */    public CyclicBarrier(int parties) {        this(parties, null);    }
这个会创建一个同步屏障,参数表示屏障拦截的线程数量,这些线程需要调用await()方法告诉CyclicBarrier自己到达了屏障点,然后阻塞住,等待其他线程,直到最后一个线程到达屏障点。
第二个:
/**     * Creates a new {@code CyclicBarrier} that will trip when the     * given number of parties (threads) are waiting upon it, and which     * will execute the given barrier action when the barrier is tripped,     * performed by the last thread entering the barrier.     *     * @param parties the number of threads that must invoke {@link #await}     *        before the barrier is tripped     * @param barrierAction the command to execute when the barrier is     *        tripped, or {@code null} if there is no action     * @throws IllegalArgumentException if {@code parties} is less than 1     */    public CyclicBarrier(int parties, Runnable barrierAction) {        if (parties <= 0) throw new IllegalArgumentException();        this.parties = parties;        this.count = parties;        this.barrierCommand = barrierAction;    }
这个和上面的方法的区别是,当所有线程释放后,会随机选择一个线程来执行barrierAction。
简单示例:
package com.java4all.mypoint;import java.time.LocalTime;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;/** * Author: yunqing * Date: 2018/7/23 * Description: */public class MyRunnable implements Runnable{
public CyclicBarrier cyclicBarrier; @Override public void run() { try { Thread.sleep(20000); System.out.println("子线程正在执行任务,当前线程为:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString()); cyclicBarrier.await(); }catch (BrokenBarrierException bbnex){ bbnex.printStackTrace(); }catch (InterruptedException inex){ inex.printStackTrace(); } } public CyclicBarrier getCyclicBarrier() { return cyclicBarrier; } public void setCyclicBarrier(CyclicBarrier cyclicBarrier) { this.cyclicBarrier = cyclicBarrier; }}
package com.java4all.mypoint;import java.time.LocalTime;import java.util.concurrent.CyclicBarrier;/** * Author: yunqing * Date: 2018/7/18 * Description:线程测试 * 测试点:一组线程执行完后再执行某任务 */public class ThreadTest {
private static CyclicBarrier cyclicBarrier; public static void main(String[] args)throws Exception{ System.out.println("主线程正在执行前:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString()); cyclicBarrier = new CyclicBarrier(10, new Runnable() { @Override public void run() { System.out.println("------子线程都执行完了吧!执行任务A!当前线程为:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString()); } }); for(int i = 1;i <= 10;i++){ MyRunnable myRunnable = new MyRunnable(); myRunnable.setCyclicBarrier(cyclicBarrier); new Thread(myRunnable).start(); } System.out.println("主线程正在执行后:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString()); }}
执行结果为:
主线程正在执行前:main      时间:17:30:47.103主线程正在执行后:main      时间:17:30:47.105子线程正在执行任务,当前线程为:Thread-5      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-4      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-6      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-0      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-1      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-2      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-3      时间:17:31:07.105子线程正在执行任务,当前线程为:Thread-9      时间:17:31:07.106子线程正在执行任务,当前线程为:Thread-7      时间:17:31:07.106子线程正在执行任务,当前线程为:Thread-8      时间:17:31:07.106------子线程都执行完了吧!执行任务A!当前线程为:Thread-4      时间:17:31:07.107

转载地址:http://qghif.baihongyu.com/

你可能感兴趣的文章
js字符串反转
查看>>
vue学习笔记(组件)
查看>>
vscode编辑器汉化
查看>>
Vue事件修饰符的使用
查看>>
解决Chrome无法从该网站添加应用、扩展程序或脚本问题
查看>>
前端不定宽高的水平垂直居中方案
查看>>
bootstrap固定导航条模板
查看>>
bootstrap轮播广告demo
查看>>
Section %Packages Does Not End With %End. Pane Is Dead
查看>>
centos8如何重启网络服务
查看>>
vue-router入门
查看>>
手动安装nginx
查看>>
Vue子组件触发父组件事件
查看>>
centos8安装docker
查看>>
vue自定义指令
查看>>
Element ui入门
查看>>
weex入门
查看>>
linux中使用sed命令注释某特征字符
查看>>
CentOS8.0通过yum安装ntp同步时间
查看>>
vim安装插件Vundle,NerdTree
查看>>