• java并发初探CyclicBarrier


    java并发初探CyclicBarrier

    CyclicBarrier的作用

    CyclicBarrier,“循环屏障”的作用就是一系列的线程等待直至达到屏障的“瓶颈点”。
    具体的使用就是就是一些列的线程调用CyclicBarrier得await()方法进入等待,直至达到
    CyclicBarrier设置的barrier size,其他的线程会继续运行。

        /**
         * A synchronization aid that allows a set of threads to all wait for
         * each other to reach a common barrier point.  CyclicBarriers are
         * useful in programs involving a fixed sized party of threads that
         * must occasionally wait for each other. The barrier is called
         * <em>cyclic</em> because it can be re-used after the waiting threads
         * are released.
         */
    

    CyclicBarrier源码

    成员变量

        /** The lock for guarding barrier entry */
        private final ReentrantLock lock = new ReentrantLock();
        /** Condition to wait on until tripped */
        private final Condition trip = lock.newCondition();
        /** The number of parties */
        private final int parties;
        /* The command to run when tripped */
        private final Runnable barrierCommand;
        /** The current generation */
        private Generation generation = new Generation();
    

    构造器

        public CyclicBarrier(int parties, Runnable barrierAction) {
            if (parties <= 0) throw new IllegalArgumentException();
            this.parties = parties;
            this.count = parties;
            this.barrierCommand = barrierAction;
        }
        public CyclicBarrier(int parties) {
            this(parties, null);
        }
    

    例子

    package com.java.javabase.thread.base.concurrent.lock;
    
    import lombok.extern.slf4j.Slf4j;
    
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    
    /**
     * @author
     */
    @Slf4j
    public class CyclicBarrierTest {
        public static int size = 5;
        private static CyclicBarrier cyclicBarrier = new CyclicBarrier(size
                , () -> log.info("thread :{} run after barrier broken", Thread.currentThread().getName()));
    
    
        public static void main(String[] args) {
            for (int i = 0; i < size; i++) {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            log.info("thread : {}, begin", Thread.currentThread().getName());
                            cyclicBarrier.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (BrokenBarrierException e) {
                            e.printStackTrace();
                        }
                        log.info("thread : {}, end", Thread.currentThread().getName());
                    }
                }.start();
            }
        }
    }
    
    

    例子结果

    2019-08-12 11:04:07,511   [Thread-0] INFO  CyclicBarrierTest  - thread : Thread-0, begin
    2019-08-12 11:04:07,511   [Thread-2] INFO  CyclicBarrierTest  - thread : Thread-2, begin
    2019-08-12 11:04:07,511   [Thread-4] INFO  CyclicBarrierTest  - thread : Thread-4, begin
    2019-08-12 11:04:07,511   [Thread-1] INFO  CyclicBarrierTest  - thread : Thread-1, begin
    2019-08-12 11:04:07,511   [Thread-3] INFO  CyclicBarrierTest  - thread : Thread-3, begin
    2019-08-12 11:04:07,511   [Thread-3] INFO  CyclicBarrierTest  - thread :Thread-3 run after barrier broken
    2019-08-12 11:04:07,511   [Thread-3] INFO  CyclicBarrierTest  - thread : Thread-3, end
    2019-08-12 11:04:07,511   [Thread-0] INFO  CyclicBarrierTest  - thread : Thread-0, end
    2019-08-12 11:04:07,511   [Thread-2] INFO  CyclicBarrierTest  - thread : Thread-2, end
    2019-08-12 11:04:07,511   [Thread-4] INFO  CyclicBarrierTest  - thread : Thread-4, end
    2019-08-12 11:04:07,511   [Thread-1] INFO  CyclicBarrierTest  - thread : Thread-1, end
    
  • 相关阅读:
    MySQL性能优化的最佳20+条经验
    memcached demo 应用例子
    关于 MySQL 主从复制的配置(转)
    java date 日期 利用 Calendar 实现增加一年月日时分秒
    Struts2中s:set标签和s:if标签小结
    hibernate oracle 配置序列 实现自动增长
    mysql5.1.47二进制版本的安装(转)
    Confluence3.4的安装和配置
    linux MemCache安装手册
    Java实现文件拷贝的4种方法(转)
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11338669.html
Copyright © 2020-2023  润新知