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
    >>>>>计算结束>>>>>
  • 相关阅读:
    BootstrapBlazor 组件库介绍
    BootstrapBlazor 组件库使用体验---Table篇
    【转载】Bootstrap Blazor 组件介绍 Table (一)自动生成列功能介绍
    【转载】Bootstrap Blazor 组件介绍 Table (二)自定义模板列功能介绍
    【转载】Bootstrap Blazor 组件介绍 Table (三)列数据格式功能介绍
    使用acme.sh从Let's Encrypt申请SSL证书
    Docker一些基本操作
    Nginx配置https以及配置说明
    vi操作
    CentOS 7下安装Docker
  • 原文地址:https://www.cnblogs.com/yzuzhang/p/6098116.html
Copyright © 2011-2022 走看看