zoukankan      html  css  js  c++  java
  • 多线程处理list

    package com.zhx.web.invoice.service;
    
    import java.util.*;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    /**
     * @author SimonHu
     * @Description:
     * @Created on 2018/10/17 13:33
     */
    public class test {
        public static void main(String[] args) {
            test a= new test();
            a.dealListWithMutiThread();
        }
        public  void dealListWithMutiThread(){
            LinkedList<Map> dataJson = new LinkedList<>();
            List<Object> list = new ArrayList<Object>(10000);
            for (int i = 0; i < 10000; i++) {
                Map map = new HashMap<>();
                map.put(i,"simon"+i);
                list.add(map);
            }
            int index = 0;
            ExecutorService ex = Executors.newFixedThreadPool(5);
            int dealSize = 2000;
            List<Future<List<Object>>> futures = new ArrayList<>(5);
            //分配
            for(int i=0;i<5;i++,index+=dealSize){
                int start = index;
                if(start>=list.size()) {
                    break;
                }
                int end = start + dealSize;
                end = end>list.size() ? list.size() : end;
                futures.add(ex.submit(new Task(list,dataJson,start,end)));
            }
            try {
                //处理
                List<Object>  result = new ArrayList<>();
                for(Future<List<Object>> future : futures){
                    //合并操作
                    result.addAll(future.get());
                }
                System.out.println(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private class Task implements Callable<List<Object>> {
    
            private List<Object> list;
            private LinkedList<Map> data;
            private int start;
            private int end;
    
            public Task(List<Object> list,LinkedList<Map> data,int start,int end){
                this.list = list;
                this.data = data;
                this.start = start;
                this.end = end;
            }
    
            @Override
            public List<Object> call() throws Exception {
                Object obj = null;
    
                List<Object> retList = new ArrayList<Object>();
                for(int i=start;i<end;i++){
                    obj = list.get(i);
                    //你的处理逻辑
                    retList.add(obj);
                }
                //返回处理结果
                return retList;
            }
        }
    }

     =========================================================

    public  void dealListWithMutiThread(LinkedList<Map> dataJson,List<Map> list,List<Map> mapList){
            int index = 0;
            ExecutorService ex = Executors.newFixedThreadPool(5);
            int dealSize = 2000;
            List<Future<List<Map>>> futures = new ArrayList<>(5);
            //分配
            for(int i=0;i<5;i++,index+=dealSize){
                int start = index;
                if(start>=list.size()) {
                    break;
                }
                int end = start + dealSize;
                end = end>list.size() ? list.size() : end;
                futures.add(ex.submit(new Task(list,dataJson,mapList,start,end)));
            }
            try {
                //处理
                List<Map>  result = new ArrayList<>();
                for(Future<List<Map>> future : futures){
                    //合并操作
                    result.addAll(future.get());
                }
                dataJson.addAll(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private class Task implements Callable<List<Map>> {
    
            private List<Map> list;
            private List<Map> mapList;
            private LinkedList<Map> data;
            private int start;
            private int end;
    
            public Task(List<Map> list,LinkedList<Map> data, List<Map> mapList,int start,int end){
                this.list = list;
                this.data = data;
                this.mapList = mapList;
                this.start = start;
                this.end = end;
            }
    
            @Override
            public List<Map> call() throws Exception {
                for(int i=start;i<end;i++){
                    //你的处理逻辑
                    for (Map map : list) {
                        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                        String openTime = String.valueOf(map.get("open_time"));
                        LinkedList<Map> maps = new LinkedList<>();
                        for (Map map1 : mapList) {
                            if (String.valueOf(map1.get("openTime")).equals(openTime)) {
                                maps.add(map1);
                                stringObjectHashMap.put("dateMouth", openTime);
                                stringObjectHashMap.put("expenseInfo", maps);
                            }
                        }
                        if (!stringObjectHashMap.isEmpty()) {
                            data.add(stringObjectHashMap);
                        }
                    }
                }
                //返回处理结果
                return data;
            }
        }
  • 相关阅读:
    Power BI性能提升的5大秘密武器
    释放低代码小宇宙,微软 Power Platform 震撼来袭!
    浅谈数据仓库建设中的数据建模方法
    微软连续13年被评为Gartner 2020年分析和BI平台魔力象限的领导者
    Power BI 演示新玩法:幻灯片播放
    DAX中按列排序的另一种结果
    Power BI能否做帕累托分析
    2018 ,请领取您Power BI 年终报告
    DAX和Power BI中的参考日期表
    Microsoft宣布为Power BI提供AI模型构建器,关键驱动程序分析和Azure机器学习集成
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/9803939.html
Copyright © 2011-2022 走看看