有些小伙伴不会写多线程,但是处理10w级以上的数据时候,单线程是肯定的大大影响效率的,这里给大家提供一个工具类
private Map<String, Integer> getHeatData(List<EscortVo> stopLocList, int addCount) {
Map<String, Integer> heatDataMap = new HashMap<>();
try {
if (stopLocList != null && stopLocList.size() > 0) {
// 线程数
int threadNum = 5;
// 多余数量
boolean special = stopLocList.size() % threadNum == 0;
// 每条线程数据量
int threadSize = stopLocList.size() / threadNum;
// 热力图大小参数值
int heatParamCount = addCount;
// 创建一个线程池
ExecutorService exec = Executors.newFixedThreadPool(threadNum);
// 定义一个任务集合
List<Callable<Map<String, Integer>>> tasks = new ArrayList<>();
Callable<Map<String, Integer>> task = null;
// 确定每条线程的数据
List<EscortVo> cutList = null;
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) {
if (special) {
break;
}
cutList = stopLocList.subList(threadSize * i, stopLocList.size());
} else {
cutList = stopLocList.subList(threadSize * i, threadSize * (i + 1));
}
final List<EscortVo> listStr = cutList;
task = new Callable<Map<String, Integer>>() {
Map<String, Integer> m = new HashMap<>();
@Override
public Map<String, Integer> call() {
m = calculateHeatData(listStr, heatParamCount);
return m;
}
};
// 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系
tasks.add(task);
}
List<Future<Map<String, Integer>>> results = exec.invokeAll(tasks);
for (Future<Map<String, Integer>> future : results) {
Map<String, Integer> m = future.get();
for (String key : m.keySet()) {
if (heatDataMap.containsKey(key)) {
heatDataMap.put(key, heatDataMap.get(key) + m.get(key));
} else {
heatDataMap.put(key, m.get(key));
}
}
}
// 关闭线程池
exec.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return heatDataMap;
}
具体使用方法如下
// escortList 是你要操作的数据
stopHeatMap = getHeatData(escortList, addCount);
//上面代码中 这个方法写你的业务逻辑
@Override
public Map<String, Integer> call() {
m = calculateHeatData(listStr, heatParamCount);
return m;
}
线程数的设置需要根据实际场景,就写这么多。。。