List<AlarmEvent> list = (List<AlarmEvent>) pageResult.getContent();
List<AlarmEvent> result = new ArrayList<>();
int threadSize =15;
int dataSize = list.size();
int threadNum = dataSize / threadSize + 1;
// 定义标记,过滤threadNum为整数
boolean special = dataSize % threadSize == 0;
List<AlarmEvent> cutList = null;
//定义固定长度的线程池 防止线程过多
ExecutorService executorService = Executors.newFixedThreadPool(25);
List<Callable<List<AlarmEvent>>> tasks = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) {
if (special) {
break;
}
cutList = list.subList(threadSize * i, dataSize);
} else {
cutList = list.subList(threadSize * i, threadSize * (i + 1));
}
final List<AlarmEvent> listStr = cutList;
Callable<List<AlarmEvent>> data = new LocationHandleThread(listStr);
tasks.add(data);
}
List<Future<List<AlarmEvent>>> futures=executorService.invokeAll(tasks);
//处理线程返回结果
if(futures!=null&&futures.size()>0){
for (Future<List<AlarmEvent>> future:futures){
result.addAll(future.get());
}
}
executorService.shutdown(); //关闭线程池
util.exportExcel(result, "报警统计信息");
long end1 = System.currentTimeMillis();
log.info(">>>>>>>>>>>>>>>>>>>>>报警导出>>>>>>>>>>>>>>>>>>>>>耗时 " + (end1 - start1) + " ms");
public class LocationHandleThread implements Callable<List<AlarmEvent>> {
private List<AlarmEvent> data;
public LocationHandleThread(List<AlarmEvent> data) {
this.data = data;
}
@Override
public List<AlarmEvent> call() throws Exception {
List<AlarmEvent> result = new ArrayList<>();
synchronized(this) {
for (AlarmEvent temp : data) {
double[] gpsCorrectList = GpsCorrect.getGpsCorrectList(temp.getLng() / 1000000, temp.getLat() / 1000000);
String address = MapLocationAddrUtil.getLocationAddr(gpsCorrectList[0] + "," + gpsCorrectList[1]);
temp.setAddress(address);
log.info("=======报警解析地址=====" + address);
String stateStr = "";
switch (temp.getState()) {
case 1:
stateStr = "已确认";
break;
case 2:
stateStr = "误报";
break;
case 3:
stateStr = "未确认";
break;
default:
}
temp.setStateStr(stateStr);
//log.info("=======报警解析状态====="+stateStr);
result.add(temp);
}
}
return result;
}
public List<AlarmEvent> getData() {
return data;
}
public void setData(List<AlarmEvent> data) {
this.data = data;
}
}