SpringBoot 配置使用ThreadPoolTaskExecutor 线程池

 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;
	}

}

猜你喜欢

转载自blog.csdn.net/LOVE_sel/article/details/96452493