springboot配置schedule线程池实现异步执行

  • Post author:
  • Post category:其他

启动类Application中增加以下

	@Value("${task.pool.size}")
	int poolSize=30;

	@Bean
	public TaskScheduler taskScheduler() {
		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
		taskScheduler.setPoolSize(poolSize);
		taskScheduler.setThreadNamePrefix("springboot-task");
		return taskScheduler;
	}

@EnableAsync是必需的 

配置文件中增加: 

task.pool.size=50

测试如下

方法上的@Async是必需的,实现异步

加Bean是实现控制线程数

参考:

Springboot Schedule定时任务实现异步的三种方法_136.la


版权声明:本文为laok186原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。