1. 程式人生 > >多執行緒處理list

多執行緒處理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;
        }
    }