多线程

 
 -- 开启多线程:
 public Map<String, String> getVehicleLeastTion(List<String> midList) {
        int num = size;
        int size = midList.size();
        List<String> midListTwo = new ArrayList<>();
        for (int i = 0; i < ((num + i) > size ? size : (num + i)); i = i + num) {
            int endIndex = (num + i) > size ? size : (num + i);
            midListTwo.add(StringUtils.join(midList.subList(i, endIndex), ","));
        }
        Map<Integer, Future<List<Map<String, Object>>>> futureMap = new HashMap<>();
        CountDownLatch countDownLatch = new CountDownLatch(midListTwo.size());
        List<Map<String, Object>> listResult = new ArrayList<>();

        for (int k = 0; k < midListTwo.size(); k++) {
            Future<List<Map<String, Object>>> submit = ExecutorFactory.submit(new vLastLocationMultiV5(countDownLatch, midListTwo.get(k)));
            futureMap.put(k, submit);
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            logger.error("await thread execution error", e);
        }
        Map<String, String> result = new HashMap<>();
        for (int k = 0; k < midListTwo.size(); k++) {
            try {
                List<Map<String, Object>> mapone = futureMap.get(k).get();
                if (!CollectionUtils.isEmpty(mapone)) {
                    listResult.addAll(mapone);
                }

            } catch (Exception e) {
                logger.error("get vehicleNo error", e);
            }
        }
       
    }
    
    
    class vLastLocationMultiV5 implements Callable<List<Map<String, Object>>> {
        private CountDownLatch countDownLatch;
        private String vclNs;

        public vLastLocationMultiV5(CountDownLatch countDownLatch, String vclNs) {
            this.countDownLatch = countDownLatch;
            this.vclNs = vclNs;
        }

        @Override
        public List<Map<String, Object>> call() {
            Map<String, String> param = new HashMap<>();
            String results = null;
            try {
                param.put("vclNs", vclNs);
                param.put("timeNearby", timeNearby);
                String call = openApiCommunalService.call(OpenApiMethodEnum.vLastLocationMultiV5.name(), param);
                results = OpenApiUtils.getResult(call, String.class);
                if (StringUtils.isEmpty(results)) {
                    JSONObject resultJson = JSON.parseObject(call);
                    Integer status = resultJson.getInteger("status");
                    logger.info("vLastLocationMultiV5 status:{},result msg:{}", status, OpenApiUtils.getMessageByStatus(status));
                }

            } catch (Exception e) {
                logger.error("query vLastLocationMultiV4 error", e);
            } finally {
                countDownLatch.countDown();
            }
            List<Map<String, Object>> maps = JSON.parseObject(results, List.class);
            return maps;
        }
    }
    
    
2:   批量取值:
             int num = 2;
             int size = FenceAlarmLists.size();
            for (int i = 0; i < ((num + i) > size ? size : (num + i)); i = i + num) {
                int endIndex = (num + i) > size ? size : (num + i);
                fenceAlarmMapper.batchInsert(FenceAlarmLists.subList(i, endIndex));
            }
3:
 ExecutorFactory.execute(() -> {
                    batchInsert(list);
                });
4:


public class ExecutorFactory {

    private static ExecutorService pool;

    static {
        initPool();
    }

    private static void initPool() {
        pool = new ThreadPoolExecutor(100, 200, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    public static void execute(Runnable command) {
        pool.execute(command);
    }

    public static <T> Future<T> submit(Callable<T> task) {
        return pool.submit(task);
    }
}

 CountDownLatch countDownLatch = new CountDownLatch(lngLats.size());
        Map<Integer, Future<List<String>>> futureMap = new HashMap<>();
        int count = 0;
        for (Map<String, String> map : lngLats) {
            Long sUtc = DateUtils.getStrParse(map.get("gpsTime")).getTime() - analyseHours;
            Long eUtc = DateUtils.getStrParse(map.get("gpsTime")).getTime() + analyseHours;
            Future<List<String>> submit = ExecutorFactory.submit(new VehicleSimNoTask(countDownLatch, sUtc, eUtc, map.get("lon"), map.get("lat"), userId));
            futureMap.put(count++, submit);
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            logger.error("await thread execution error", e);
        }
        count = 0;
        Map<String, Integer> result = new HashMap<>();
        for (Map<String, String> map : lngLats) {
            List<String> areaVeh = new ArrayList<>();
            try {
                areaVeh = futureMap.get(count++).get();
            } catch (Exception e) {
                logger.error("get simNo error", e);
            }
            for (String vno : areaVeh) {
                result.put(vno, result.get(vno) == null ? 1 : result.get(vno) + 1);
            }
        }
        return result;
/***
     * 异步获取simNo
     */
    public class VehicleSimNoTask implements Callable<List<String>> {
        private CountDownLatch countDownLatch;
        private Long statrTime;
        private Long endTime;
        private String lon;
        private String lat;
        private String userId;

        public VehicleSimNoTask(CountDownLatch countDownLatch, Long statrTime, Long endTime, String lon, String lat, String userId) {
            this.countDownLatch = countDownLatch;
            this.statrTime = statrTime;
            this.endTime = endTime;
            this.lon = lon;
            this.lat = lat;
            this.userId = userId;
        }

        @Override
        public List<String> call() {
            try {
                return getAreaVehSimNo(statrTime, endTime, AREA_RADIUS, lon, lat, userId, false);
            } catch (Exception e) {
                logger.error("query simNo task error", e);
            } finally {
                countDownLatch.countDown();
            }
            return new ArrayList<>();
        }
    }

    
    
    
    
    
5:    
     SXSSFWorkbook workbook = new SXSSFWorkbook(100);
        workbook.setCompressTempFiles(true);
        SXSSFSheet sheet = workbook.createSheet("车辆跟随");
        try {
            int rowNum = 0;
            SXSSFRow firstRow = sheet.createRow(rowNum++);
            String[] heads = {"车牌号码", "轨迹匹配率", "起始停车时间", "起始停车位置"};
            for (int i = 0; i < heads.length; i++) {
                firstRow.createCell(i).setCellValue(heads[i]);
            }
            List<Map<String, String>> list = vehAnalyseService.findList(key, vno, startTime, endTime);
            Map<Integer, Future<String>> futureMap = new HashMap<>();
            for (Map<String, String> map : list) {
                int rownum = rowNum++;
                futureMap.put(rownum, ExecutorFactory.submit(new LocationTask(map.get("lon"), map.get("lat"))));
            }
            rowNum = 1;
            for (Map<String, String> map : list) {
                int rownum = rowNum++;
                SXSSFRow row = sheet.createRow(rownum);
                row.createCell(0).setCellValue(map.get("vno"));
                row.createCell(1).setCellValue(map.get("ratio"));
                row.createCell(2).setCellValue(map.get("startTime"));
                String val;
                try {
                    val = futureMap.get(rownum).get();
                } catch (Exception e) {
                    val = "暂无位置";
                    logger.error("异步获取位置信息出错", e);
                }
                row.createCell(3).setCellValue(val);
            }
        } catch (Exception e) {
            logger.error("导出异常", e);
        } finally {
            Cookie cookie = new Cookie("vehAnalyseExport", request.getParameter("downToken"));
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return ExportUtils.wbToEntity(ExportUtils.toUtf8String("车辆跟随.xlsx", request), workbook);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值