public void handle() throws Exception {
long start = System.currentTimeMillis();
log.info("多线程 : ********** start " + new Date());
List<String> list = new ArrayList<>(getAllDeviceNumber());
int threadSize = 300;
int dataSize = list.size();
int threadNum = dataSize / threadSize + 1;
boolean special = dataSize % threadSize == 0;
ExecutorService exec = Executors.newFixedThreadPool(threadNum);
List<Callable<Integer>> tasks = new ArrayList<>();
Callable<Integer> task;
List<String> cutList;
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));
}
List<String> finalCutList = cutList;
task = () -> {
getLastDataTime(finalCutList);
return 1;
};
tasks.add(task);
}
exec.invokeAll(tasks);
exec.shutdown();
log.info("线程任务执行结束");
log.info("线程数" + threadNum + "执行任务消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
}