使用线程池开启多线程任务处理

常用线程池开启多线程任务跑批

线程池配置

public class ThreadPoolExecuteUtils {

public static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();

private static final Logger logger = LoggerFactory.getLogger(ThreadPoolExecuteUtils.class);

private static final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10000);

private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
        CPU_COUNT * 10,
        CPU_COUNT * 10,
        60L,
        TimeUnit.SECONDS,
        queue,
        (r, executor) -> {
            try {
                executor.getQueue().put(r);
            } catch (Exception e) {
                logger.error("线程池入队异常", e);
            }
        });


public static ThreadPoolExecutor newExecutorInstance() {
    return executor;
}
}

多线程任务开启执行具体逻辑

private void run(List<T> list) throws InterruptedException {
        Queue<List<T>> queue = new ConcurrentLinkedQueue<>(Lists.partition(list, 20));
        int size = ThreadPoolExecuteUtils.newExecutorInstance().getCorePoolSize() + 1;
        size = Math.min(size, queue.size());
        CountDownLatch downLatch = new CountDownLatch(size);
        for (int i = 0; i < size; i++) {
            ThreadPoolExecuteUtils.newExecutorInstance().execute(() -> {
                List<T> entityList = null;
                try {
                    while (!queue.isEmpty()) {
                        entityList = queue.poll();
                        //执行具体逻辑
                        runData(entityList);
                    }
                } catch (Exception e) {
                    log.error("跑批出错:{}", e);
                } finally {
                    //线程计数
                    downLatch.countDown();
                }
            });
        }
        downLatch.await();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值