线程池调用部分数据更新不到。因为调用外部接口需要等待时间,所有采用线程加快速度:代码如下:方案一、二 都会出现部分数据没有调用到数据或数据没有更新到数据库
方案一:
List logisticsTracesList=logisticsTraceService.updateLogisticsTraceOffline(params);// 只查询线下的物流信息
if(CollectionUtils.isNotEmpty(logisticsTracesList)){
Map> longListMap=getLogisticsTrace(logisticsTracesList);//一次分一千为一批
List list = new ArrayList();
for (Long aLong : longListMap.keySet()) {
list=longListMap.get(aLong);
}
Var var=new Var();
var.setSysVar(SystemConstants.LOGISTICS_TASK);//查询调用配置项
var=varService.getByExample(var);
Integer dealSize=100;
if(var!=null){
dealSize = Integer.parseInt(var.getSysVal());
}
int index = 0;
Integer count=(int)Math.ceil(list.size()/dealSize.doubleValue());
ExecutorService ex = Executors.newFixedThreadPool(count);
List>> futures = new ArrayList>>();
//分配
for(int i=0;i
int start = index;
if(start>=list.size()) break;
int end = start + dealSize;
end = end>=list.size() ? list.size() : end;
LogisticsTraceTask logisticsTraceTask=new LogisticsTraceTask(list.subList(start,end),start,end);
logisticsTraceTask.setExpressConfigService(expressConfigService);
logisticsTraceTask.setLogisticsTraceService(logisticsTraceService);
futures.add(ex.submit(logisticsTraceTask));
}
try {
//处理
for(Future> future : futures){
System.out.println("=============="+future);
}
} catch (Exception e) {
e.printStackTrace();
}
}
方案二:
List logisticsTracesList=logisticsTraceService.updateLogisticsTraceOffline(params);// 只查询线下的物流信息
if(CollectionUtils.isNotEmpty(logisticsTracesList)){
Map> longListMap=getLogisticsTrace(logisticsTracesList);//一次分一千为一批
List list = new ArrayList();
for (Long aLong : longListMap.keySet()) {
list=longListMap.get(aLong);
}
Var var=new Var();
var.setSysVar(SystemConstants.LOGISTICS_TASK);//查询调用配置项
var=varService.getByExample(var);
Integer dealSize=500;//每个线程处理500条数据、可动态传入
if(var!=null){
dealSize = Integer.parseInt(var.getSysVal());
}
int index = 0;
Integer count=(int)Math.ceil(list.size()/dealSize.doubleValue());
if(count<=0){
count=1;
}
//分配
for(int i=0;i
int start = index;
if(start>=list.size()) break;
int end = start + dealSize;
end = end>=list.size() ? list.size() : end;
LogisticsTraceTask logisticsTraceTask=new LogisticsTraceTask(list.subList(start,end),start,end);
logisticsTraceTask.setExpressConfigService(expressConfigService);
logisticsTraceTask.setLogisticsTraceService(logisticsTraceService);
logisticsTraceTask.start();
}
}
多线程与线程问题。高分悬赏。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
悬赏问题
- ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
- ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
- ¥50 树莓派安卓APK系统签名
- ¥15 maple软件,用solve求反函数出现rootof,怎么办?
- ¥65 汇编语言除法溢出问题
- ¥15 Visual Studio问题
- ¥20 求一个html代码,有偿
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果