定时任务 (默认一个核心线程)
1、@EnableScheduling
开启定时任务
2、@Scheduled
开启一个定时任务
3、自动配置类 TaskSchedulingAutoConfiguration
异步任务
1、@EnableAsync
开启异步任务功能
2、 @Async
给希望异步执行的方法
上标注
3、自动配置类TaskExecutionAutoConfiguration
属性绑定在TaskExecutionProperties
配置定时任务参数
spring.task.execution.pool.core-size=20
spring.task.execution.pool.max-size=50
- 例子
@Slf4j
@Component
@EnableAsync
@EnableScheduling
public class HelloSchedule {
/**
* 1、Spring中6位组成,不允许7位d的年
* 2、周的位置,1-7代表周一到周日
* 3、定时任务不应该阻塞。默认是阻塞的
* 1)、可以让业务运行以异步的方式,自己提交到线程池
* 2)、支持定时任务线程池;设置TaskSchedulingProperties;
* spring.task.scheduling.pool.size=5
* 3)、让定时任务异步执行
* 异步任务
*
* 解决:使用异步任务来完成定时任务不阻塞的功能
*/
@Async
@Scheduled(cron = "*/5 * * * * ?")
public void hello() throws InterruptedException {
log.info("hello......");
Thread.sleep(3000);
}
}