数据很多,多线程分批处理
@Slf4j
public class ThreadPoolUtils {
public static <R> List<R> batchExecute(String taskName, List<?> originList, Function<List<?>, Callable<R>> supplier) {
// 多线程批处理
int batchSize = ContentFeedpostConfig.batchZPCreativeLimit;
List<? extends List<?>> partition = Lists.partition(originList, batchSize);
CompletionService<R> completionService = new ExecutorCompletionService<>(ThreadPoolUtils.getPreparePoolExecutor());
List<Future<R>> futureList = Lists.newArrayListWithCapacity(4);
for (List<?> list : partition) {
Callable<R> apply = supplier.apply(list);
futureList.add(completionService.submit(apply));
}
List<R> result = Lists.newArrayListWithCapacity(4);
for (int n = 0; n < futureList.size(); n++) {
try {
Future<R> future = completionService.poll(10, TimeUnit.MILLISECONDS);
if (future != null) {
R r = future.get(