常用线程池开启多线程任务跑批
线程池配置
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();
}