zoukankan      html  css  js  c++  java
  • 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常用基本命令
    mysql8.0.13下载与安装图文教程
    k8s ingress 增加跨域配置
    Jenkins 备份恢复插件 thinBackup 使用
    k8s HA master 节点宕机修复
    nginx 跨域问题解决
    mongodb 3.4.24 主从复制
    k8s 线上安装 jenkins并结合 jenkinsfile 实现 helm 自动化部署
    k8s helm 运用与自建helm仓库chartmuseum
    centos6 源码安装 unzip
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11338669.html
Copyright © 2011-2022 走看看