java多线程中ArrayList应当替换为CopyOnWriteArrayList
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java多线程中ArrayList应当替换为CopyOnWriteArrayList,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2634字,纯文字阅读大概需要4分钟。
内容图文
![java多线程中ArrayList应当替换为CopyOnWriteArrayList](/upload/InfoBanner/zyjiaocheng/657/6ef2f6cb0af9404ca11694d5aed3813f.jpg)
@ApiOperation(value = "获取子公司sub,省分prov,总部hq, 全部 total 指标值中的 最高分,最低分,平均分") @RequestMapping(value = "/getMaxAndMinAndAverageKpiValue", method = RequestMethod.POST) private Map<String, Object> getMaxAndMinAndAverageKpiValue(@RequestBody KpiStatisticsReq req) { if (req.getKpiIds() == null) { return ResponseUtil.fail("请输入 kpi指标的kpiId数组"); } try { List<HashMap<String, Object>> list = new CopyOnWriteArrayList(); if (req.getBranch() == null) { req.setBranch("total"); } List<String> ldapOrgCodesList1 = soaOrgWithPathBranchServiceImpl.queryLdapOrgCodesByBranch(req.getBranch()); List<String> ldapOrgCodesList2= new ArrayList<>(); ldapOrgCodesList2.addAll(ldapOrgCodesList1); if (req.getLdapOrgCodes()!=null){ ldapOrgCodesList2.addAll(Arrays.asList(req.getLdapOrgCodes())); } final List<String> ldapOrgCodesList=ldapOrgCodesList2.stream().distinct().collect(Collectors.toList()); ForkJoinPool forkJoinPool = new ForkJoinPool(); for (Integer kpiId : req.getKpiIds()) { forkJoinPool.execute(() -> { Kpi kpi = kpiServiceImpl.queryInfoByNatrualKey(kpiId); // 获取 子公司编码 ldap code HashMap<String, Object> datas = new HashMap<>(); datas.put("kpi", kpi); // List<KpiValue> appKpiListAll = new ArrayList<>(); List<Double> doubles = new ArrayList<>(); KpiValue appKpiParam = new KpiValue(); for (String scope : ldapOrgCodesList) { appKpiParam.setScope(scope); appKpiParam.setKpiId(kpiId); // List<KpiValue> appKpiList = kpiValueService.searchInfos(appKpiParam); KpiValue kpiValue = kpiValueService.searchOne(appKpiParam); if (kpiValue != null && kpiValue.getValue() != null && !kpiValue.getValue().trim().equals("")) { // doubles.add(Double.parseDouble(kpiValue.getValue())); doubles.add(new BigDecimal(kpiValue.getValue()).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()); } } if (doubles.size() > 0) { DoubleSummaryStatistics statistics = doubles.stream().mapToDouble(x -> x).summaryStatistics(); datas.put("max", BigDecimal.valueOf(statistics.getMax()).setScale(4, BigDecimal.ROUND_HALF_UP)); datas.put("min", BigDecimal.valueOf(statistics.getMin()).setScale(4, BigDecimal.ROUND_HALF_UP)); datas.put("average", BigDecimal.valueOf(statistics.getAverage()).setScale(4, BigDecimal.ROUND_HALF_UP)); datas.put("sum", BigDecimal.valueOf(statistics.getSum()).setScale(4, BigDecimal.ROUND_HALF_UP)); datas.put("count", statistics.getCount()); } list.add(datas); }); } while (forkJoinPool.getActiveThreadCount() != 0) { Thread.sleep(100); } forkJoinPool.shutdown(); return ResponseUtil.ok(list); } catch (Exception e) { e.printStackTrace(); LoggerUtils.error(getClass(), "查询失败!" + e.getMessage()); return ResponseUtil.fail("查询失败!" + e.getMessage()); } }
内容总结
以上是互联网集市为您收集整理的java多线程中ArrayList应当替换为CopyOnWriteArrayList全部内容,希望文章能够帮你解决java多线程中ArrayList应当替换为CopyOnWriteArrayList所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。