首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >同步屏障CyclicBarrier

同步屏障CyclicBarrier

作者头像
IT云清
发布2019-01-22 15:05:27
发布2019-01-22 15:05:27
48300
代码可运行
举报
文章被收录于专栏:IT云清IT云清
运行总次数:0
代码可运行
CyclicBarrier,根据字面意思理解:循环屏障。屏障的意思:CyclicBarrier可以让一组线程到达某个屏障点时被阻塞,直到最后一个线程到达屏障点时,屏障才会放开,所有被屏障阻塞的线程才开始执行任务;循环:当所有线程被释放后,这个CyclicBarrier可以被重用。
有两个很常用的构造方法:
第一个:
代码语言:javascript
代码运行次数:0
运行
复制
    /**
     * 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自己到达了屏障点,然后阻塞住,等待其他线程,直到最后一个线程到达屏障点。
第二个:
代码语言:javascript
代码运行次数:0
运行
复制
    /**
     * 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。
简单示例:
代码语言:javascript
代码运行次数:0
运行
复制
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;
    }
}
代码语言:javascript
代码运行次数:0
运行
复制
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());
    }

}
执行结果为:
代码语言:javascript
代码运行次数:0
运行
复制
主线程正在执行前: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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年07月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CyclicBarrier,根据字面意思理解:循环屏障。屏障的意思:CyclicBarrier可以让一组线程到达某个屏障点时被阻塞,直到最后一个线程到达屏障点时,屏障才会放开,所有被屏障阻塞的线程才开始执行任务;循环:当所有线程被释放后,这个CyclicBarrier可以被重用。
  • 有两个很常用的构造方法:
  • 第一个:
  • 这个会创建一个同步屏障,参数表示屏障拦截的线程数量,这些线程需要调用await()方法告诉CyclicBarrier自己到达了屏障点,然后阻塞住,等待其他线程,直到最后一个线程到达屏障点。
  • 第二个:
  • 这个和上面的方法的区别是,当所有线程释放后,会随机选择一个线程来执行barrierAction。
  • 简单示例:
  • 执行结果为:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档