Java线程池使用案例

@Bean("myExecutor")
public ThreadPoolExecutor task() {
// corePoolSize 核心线程数
// maximumPoolSize 最大线程数
// keepAliveTime 空闲线程存活时间
// unit 存活时间单位
// workQueue 工作队列 (ArrayBlockingQueue 有界队列)
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(5));
return executor;
}
public void task(Runnable task) {
try {
myExecutor.execute(task);
} catch (Exception e) {
log.error(e.getMessage());
}
}

task(() -> OkUtil.postWithHead(ConstantUtil.DhAPi.addSmsLog, JSON.toJSONString(smsApiLog), ConstantUtil.DhAPi.key, ConstantUtil.DhAPi.val));

posted @ 2021-11-23 16:12  qing222  阅读(175)  评论(0编辑  收藏  举报