Skip to content

Instantly share code, notes, and snippets.

@qlong8807
Created March 6, 2019 09:15
Show Gist options
  • Save qlong8807/4984d5732345546d3514952df2a8ab6d to your computer and use it in GitHub Desktop.
Save qlong8807/4984d5732345546d3514952df2a8ab6d to your computer and use it in GitHub Desktop.
springboot Scheduled 多任务 task
//1. 开启功能@EnableScheduling
//2. 配置线程池
//spring默认(不配置线程池)为单线程,一个任务执行完成才会执行下一个任务。
@Configuration
public class TaskConfigure{
@Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler=new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);//线程池
return taskScheduler;
}
}
//3. spring配置定时任务有三种方式
//a. 用cron表达式配置,在类上添加@Component,方法上添加@Scheduled(cron="*/6 * * * * ? ")
//b. 固定频率,@Scheduled(initialDelay = 2000,fixedRate = 3000)//不会等待上一次完成再执行
//c. 固定延迟,@Scheduled(initialDelay = 2000,fixedDelay = 3000)//会等待上一次完成再执行
//异步运行,类上添加@EnableAsync,方法上添加@Async。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment