zoukankan      html  css  js  c++  java
  • CyclicBarrier实现流水处理服务类

    package com.yzu.zhang.thread.concurrent;
    
    import java.util.Map.Entry;
    import java.util.Random;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.CyclicBarrier;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 银行流水处理服务类
     * @date 2016年11月21日
     */
    public class BankCountService implements Runnable{
    
        private int threadCount = 4;
        
        private Random random = new Random();
        
        /**
         * 创建4个屏障类,都处理完之后执行当前类的run方法
         */
        private CyclicBarrier c = new CyclicBarrier(threadCount, this);
        
        private ExecutorService executor = Executors.newFixedThreadPool(threadCount);
        
        private ConcurrentHashMap<String, Integer> countMap = new ConcurrentHashMap<String, Integer>();
        
        /**
         * 开启线程池进行计算
         */
        private void count() {
            System.out.println(">>>>>开始计算>>>>>");
            for (int i = 0; i < threadCount; i++) {
                executor.execute(new Runnable() {
                    
                    @Override
                    public void run() {
                        //计算当前sheet的银行流水,模拟计算
                        int value = random.nextInt(10000);
                        try {
                            Thread.sleep(value);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        String threadName = Thread.currentThread().getName();
                        countMap.put(threadName, value);
                        System.out.println("["+threadName+"]计算完成:"+value+", 等待汇总...");
                        
                        //银行流水计算完成,插入一个屏蔽,等待其他线程的计算
                        try {
                            c.await();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    
                });
            }
        }
        
        @Override
        public void run() {
            int total = 0;
            System.out.println("开始汇总...");
            //汇总结果
            for( String key : countMap.keySet() ) {
                total += countMap.get(key);
            }
            
            for(Entry<String,Integer> entry : countMap.entrySet()) {
                //total += entry.getValue().intValue();
            }
            
            //将结果输出
            System.out.println("银行总流水==="+total);
            
            //关闭线程池
            if (executor != null) {
                executor.shutdown();
                System.out.println(">>>>>计算结束>>>>>");
            }
        }
        
        public static void main(String[] args) {
            BankCountService service = new BankCountService();
            service.count();
        }
    }

    运行结果:

    >>>>>开始计算>>>>>
    [pool-1-thread-1]计算完成:750, 等待汇总...
    [pool-1-thread-2]计算完成:6426, 等待汇总...
    [pool-1-thread-4]计算完成:6538, 等待汇总...
    [pool-1-thread-3]计算完成:9430, 等待汇总...
    开始汇总...
    银行总流水===23144
    >>>>>计算结束>>>>>
  • 相关阅读:
    bootstap 折叠
    AtCoder AGC019E Shuffle and Swap (DP、FFT、多项式求逆、多项式快速幂)
    Codeforces Gym 101630J Journey from Petersburg to Moscow (最短路)
    BZOJ 4042 Luogu P4757 [CERC2014]Parades (树形DP、状压DP)
    BZOJ 2734 [HNOI2012]集合选数 (状压DP、时间复杂度分析)
    BZOJ 2759 一个动态树好题 (LCT)
    Codeforces 1205C Palindromic Paths (交互题、DP)
    getopt实现传参自动识别
    powershell笔记
    bat语法需要注意的地方
  • 原文地址:https://www.cnblogs.com/yzuzhang/p/6098116.html
Copyright © 2011-2022 走看看