1.添加配置文件
@Configuration
@EnableAsync
public class SpringThreadPoolExecutorConfig {
@Bean(name = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setKeepAliveSeconds(300);
//核心线程池数
pool.setCorePoolSize(50);
//最大线程
pool.setMaxPoolSize(100);
//队列容量
pool.setQueueCapacity(1000);
//队列满,线程被拒绝执行策略
pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
pool.setThreadNamePrefix("aaaa-common-support-service--");
return pool;
}
}
ps:拒绝策略 https://blog.csdn.net/jgteng/article/details/54411423
2.使用
@Slf4j
@Service("AAAAAService")
public class AAAAAServiceImpl implements AAAAAService {
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
public List<String> queryUserByGivenname(String txNo, String givenname)
throws ParamInvalidException {
threadPoolTaskExecutor.execute(()->{
log.info("-------txNO[{}],givename[{}]", txNo,givenname);
});
return null;
}
}