为了账号安全,请及时绑定邮箱和手机立即绑定

java多线程数据生成重复

java多线程数据生成重复

Helenr 2019-01-16 08:54:46
这是我们业务中的一段代码,主要的逻辑就是将数据根据银行账号分组,每一组也就是每个银行账号起一个线程处理业务逻辑.现在出现的问题就是有线上小概率生成重复数据,本地测试尝试还原场景,但是一直不能复现问题. 相关代码 这是入库的实体 writeOffDailyReport.也即是这个实体生成的时候重复了. ExecutorService cachedThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2); List<Future> futures = Lists.newArrayList(); for (String value : Sets.newHashSet(multimap.keys())) { futures.add(cachedThreadPool.submit(() -> { Thread thread = Thread.currentThread(); logger.info("开启一条线程," + ": " + value + ",线程信息: 线程id" + thread.getId() + " 线程名" + thread.getName()); values.add(value); List<WriteOffDailyReport> subList = Lists.newArrayList(); BigDecimal initAmount = BigDecimal.ZERO; BigDecimal finalAmount = BigDecimal.ZERO; BigDecimal bankDifferenceBD = BigDecimal.ZERO; List<AnalysisBankBillVo> analysisBankBillVoList = this.getAnalysisBankBillthrowEx(MyUtils.formatNumber(value), exportDate); WriteOffDailyReport reportFromAnalysisBankBill = this.setValueFromAnalysisBankBill(analysisBankBillVoList); logger.info("开始遍历银行账号: " + value + " 下的所有机构核销数据"); for (WriteOffData writeOffDataExample : Lists.newArrayList(multimap.get(value))) { logger.info("开始执行 银行账号: " + value + "下 机构: " + writeOffDataExample.getOrgName()); WriteOffDailyReport writeOffDailyReport = new WriteOffDailyReport(); writeOffDailyReport.setStatus("00"); this.setValueFromWriteOffData(exportDate, writeOffDataExample, writeOffDailyReport); List<OrgConfig> orgConfigs = this.setValueFromOrgConig(writeOffDailyReport, orgConfigMultimap, writeOffDataExample); this.setValueFromYesterDayReport(exportDate, writeOffDailyReport); initAmount = new BigDecimal(writeOffDailyReport.getBankInitialBalance()); finalAmount = new BigDecimal(writeOffDailyReport.getBankFinalBalance()); bankDifferenceBD = this.computeBankDifference(bankDifferenceBD, writeOffDailyReport); this.computeBusinessIncome(writeOffDailyReport); this.computeBankIncomeOrPayout(writeOffDailyReport, orgConfigs); this.setValueFromReportAnalysisBankBill(reportFromAnalysisBankBill, writeOffDailyReport); subList.add(writeOffDailyReport); logger.info("银行账号: " + value + "下 机构: " + writeOffDataExample.getOrgName() + " 完毕!"); } logger.info("将银行账号: " + value + " 下差异汇总计算"); bankDifferenceBD = computeBankDifferenceExpand(bankDifferenceBD, reportFromAnalysisBankBill); logger.info("开始汇总插入/更新核销日报"); for (WriteOffDailyReport dailyReport : subList) { dailyReport.setBankDifference(MyUtils.initValue(initAmount.add(bankDifferenceBD).subtract(finalAmount).toPlainString())); this.sortListFromDailyReport(batchAdd, batchUpdate, dailyReport); } return true; })); } 现在是将多线程去掉在跑,效率有点低.对多线程处理还是不熟悉,望指教.
查看完整描述

1 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

 List<AnalysisBankBillVo> analysisBankBillVoList = this.getAnalysisBankBillthrowEx(MyUtils.formatNumber(value), exportDate);
 WriteOffDailyReport reportFromAnalysisBankBill = this.setValueFromAnalysisBankBill(analysisBankBillVoList);      

应该是这里生成的逻辑问题. 没看到里边也不太好说. future是不是必须的如果不是必须的,先把这里去掉,一步一步的排查,,这种使用模式肯定是没有问题的.

查看完整回答
反对 回复 2019-02-12
  • 1 回答
  • 0 关注
  • 914 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信