提示:以下是本篇文章正文内容,下面案例可供参考
使用步骤
1.config文件
package com.cooker.lottery_system.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ExecutorConfig {
@Value("${async.executor.thread.core_pool_size}")
private int corePoolSize;
@Value("${async.executor.thread.max_pool_size}")
private int maxPoolSize;
@Value("${async.executor.thread.queue_capacity}")
private int queueCapacity;
@Value("${async.executor.thread.name.prefix}")
private String namePrefix;
@Bean(name = "asyncServiceExecutor")
public ThreadPoolTaskExecutor asyncServiceExecutor(){
ThreadPoolTaskExecutor threadPoolTaskExecutor = new
ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
threadPoolTaskExecutor.setKeepAliveSeconds(3);
threadPoolTaskExecutor.setThreadNamePrefix(namePrefix);
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执⾏任务,⽽是由调⽤者所在的线程来执⾏
threadPoolTaskExecutor.setRejectedExecutionHandler(new
ThreadPoolExecutor.AbortPolicy());
//加载
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}
2、application.properties
## 线程池 ##
async.executor.thread.core_pool_size=10
async.executor.thread.max_pool_size=20
async.executor.thread.queue_capacity=20
async.executor.thread.name.prefix=async-service-
3、对象注入
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
4、并行执行
// 短信通知
threadPoolTaskExecutor.execute(()->sendMessage(winningRecordDOList));
// 邮件通知
threadPoolTaskExecutor.execute(()->sendMail(winningRecordDOList));