List<String> studentList = new LinkedList<>();
studentList.add("A");
studentList.add("B");
studentList.add("C");
studentList.add("D");
studentList.add("E");
studentList.add("F");
studentList.add("G");
studentList.add("H");
studentList.add("I");
studentList.add("J");
studentList.add("K");
studentList.add("L");
studentList.add("M");
studentList.add("N");
studentList.add("O");
int nThreads = 3;
int size = studentList.size();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List<Future<Integer>> futures = new ArrayList<Future<Integer>>(nThreads);
for (int i = 0; i < nThreads; i++) {
final List<String> groupList = studentList.subList(size / nThreads * i, size / nThreads * (i + 1));
Callable<Integer> task1 = () -> {
log.info("Print:"+JSONObject.toJSONString(groupList));
//do your work
return 1;
};
futures.add(executorService.submit(task1));
}
executorService.shutdown();
if (!futures.isEmpty() && futures != null) {
log.info("I AM COPY OVER");
}
多线程批量拆分处理List集合任务
最新推荐文章于 2024-04-22 15:18:53 发布