自定义异步线程池
@EnableAsync
@Configuration
public class TaskPoolConfig {
@Bean("taskExecutor")
public Executor taskExecutor() {
int i = Runtime.getRuntime().availableProcessors();
System.out.println("系统最大线程数 : " + i);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(16);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(99999);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("asyncServiceExecutor -");
executor.setAwaitTerminationSeconds(60);
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}
使用一
@Resource(name = "taskExecutor")
private ExecutorService esThreadPoolExecutor
使用二
@Override
@Async("taskExecutor")
public Result sendMsg(String content) {
try {
Thread.sleep(1000);
} catch (Exception e) {
log.error("发送信息异常 -> ", e)
}
}