// 创建固定线程池 ExecutorService executorService = Executors.newFixedThreadPool(5); try { // 将list 切割成小list List<List<SyncPersonInfo>> lists = Lists.partition(list1, 5000); for (List<SyncPersonInfo> list : lists) { // 线程池执行小list executorService.submit(() -> { for (SyncPersonInfo queryPersonInfo : list) { // 处理单条逻辑 deal(queryPersonInfo); } }); } try { // 关闭线程池,一定要在等待主线程关闭之前执行 executorService.shutdown(); // 超时关闭线程池 if (!executorService.awaitTermination(30, TimeUnit.MINUTES)) { executorService.shutdown(); } } catch (Exception e) { executorService.shutdown(); } log.info("lists:{}", lists.size());
java 大list 切割,并多线程处理
于 2023-12-28 14:29:25 首次发布