1、定义多线程


import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class RefreshThreadPool {

    private static final int THREAD_SIZE = 0;
    private static final int MAX_THREAD_SIZE = 100;
    private static final int KEEP_ALIVE_TIME = 5; //单位分钟

    public static final long DEFAULT_TIMEOUT = 10000; //单位秒

    public static final ThreadPoolExecutor REFRESH_DATA_EXECUTOR = new ThreadPoolExecutor(
            THREAD_SIZE, MAX_THREAD_SIZE, KEEP_ALIVE_TIME, TimeUnit.MINUTES, new LinkedBlockingDeque<>(), new ThreadFactory() {
        private final AtomicInteger count = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.setName("refresh-data-" + count.incrementAndGet());
            return thread;
        }
    }, new ThreadPoolExecutor.CallerRunsPolicy());

}

 2、使用多线

   private static List<SettlementOrderDetailPO> obtainSettlementOrderDetailPOList(List<String> payIdList) {
      //定义一个线程安全的list

        List<SettlementOrderDetailPO> poList = Collections.synchronizedList(new ArrayList<SettlementOrderDetailPO>());

        List<CompletableFuture<Boolean>> futures = new ArrayList<>();
        for (String payid : payIdList) {
            CompletableFuture<Boolean> taskFuture = CompletableFuture.supplyAsync(() -> {
                try {
                    List<SettlementOrderDetailPO> itemPoList = querySettlementOrderDetailPo(payid);
                    if (CollectionUtils.isNotEmpty(itemPoList)) {
                        poList.addAll(itemPoList);
                    }
                    return true;
                } catch (Exception e) {
                    log.error("查询订单异常", e);
                    return false;
                }
            }, RefreshThreadPool.REFRESH_DATA_EXECUTOR);
            futures.add(taskFuture);
        }

        try {
            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
                    .get(RefreshThreadPool.DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            log.error("查询订单数据超时", e);
        } catch (Exception e) {
            log.error("查询订单数据异常", e);
        }
        return poList;
    }