junjie_326168998
心梦无痕
2018-01-17 07:13

多线程与线程问题。高分悬赏。

20
  • 线程
  • 多线程
  • 线程池

线程池调用部分数据更新不到。因为调用外部接口需要等待时间,所有采用线程加快速度:代码如下:方案一、二 都会出现部分数据没有调用到数据或数据没有更新到数据库
方案一:
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();
}
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答