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
    
  • 相关阅读:
    用SQL实现统计报表中的“小计”和“合计”
    GROUPPING和ROLLUP的基本知识
    DECODE函数
    C#调用Python,报错No module named os
    Database differential backup差异备份和还原
    C#多线程和线程同步总结
    TF30042: The database is full. Contact your Team Foundation Server administrator.
    C#算法面试题
    sql分区文件删不的可能解决方法
    word建立统一的表格样式
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11338669.html
Copyright © 2011-2022 走看看