一、定时任务实现的几种方式:
Timer:jdk util自带的Timer类,可以调度一个java.util.TimerTask任务。只能设定任务按照某个频度执行,但不能按设定时间运行。
 ScheduledExecutorService:jdk自带类,基于线程池的定时任务类,通过使用分配的线程池中的一个线程去执行调度任务,多线程并发执行。
 Spring Task:Spring3.0以后自带的task,类似轻量级的Quartz,使用最方便。
 Quartz:功能强大的的调度器,可以按指定时间执行,也可以设定频度执行,需要做配置。
 二、Timer使用方法
 例:
 public class TestTimer {
 public static void main(String[] args) {
 TimerTask timerTask = new TimerTask() {
 @Override
 public void run() {
 System.out.println(“hello world”);
 }
 };
 Timer timer = new Timer();
 //每5秒执行一次
 timer.schedule(timerTask,10,5000);
 }
 }
三、ScheduledExecutorService使用方法
 例:
 public class TestScheduledExecutorService {
 public static void main(String[] args) {
 ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
 // 每隔5s执行一次任务,立即执行
 service.scheduleAtFixedRate(()->System.out.println("hello world "), 0, 5, TimeUnit.SECONDS);
 }
 }
四、Spring Task注解方式使用方法
 1.导入依赖pom.xml:
 
 
 org.springframework.boot
 spring-boot-starter-web
 
 
 org.springframework.boot
 spring-boot-starter
 
 
 org.projectlombok
 lombok
 true
 
 
 org.springframework.boot
 spring-boot-starter-test
 test
 
 
2.创建异步方法
 @EnableAsync启用异步方法执行,跟在xml中配置task:* 效果一样,多线程同时执行任务。
 @Configuration
 @EnableAsync
 public class AsyncConfig implements AsyncConfigurer {
 private int corePoolSize = 10;
 private int maxPoolSize = 200;
 private int queueCapacity = 10;
 @Bean
 @Override
 public Executor taskExecutor() {
 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
 executor.setCorePoolSize(corePoolSize);
 executor.setMaxPoolSize(maxPoolSize);
 executor.setQueueCapacity(queueCapacity);
 executor.initialize();
 return executor;
 }
 @Override
 public AsyncUncaughtExceptionHandler taskUncaughtExceptionHandler() {
 return MyAsyncUncaughtExceptionHandler();
 }
 }
@Configuration:表明该类是一个配置类
 @EnableAsync:开启异步事件的支持
3.创建任务类:
 @Async
 @Component
 public class ScheduledService {
 private static Log log = LogFactory.getLog(ScheduledService .class);
 @Scheduled(cron = “0/5 * * * * *”)
 public void scheduled(){
 log.info(“hello1”);
 }
 @Scheduled(fixedRate = 5000)
 public void scheduled1() {
 log.info(“hello2”);
 }
 @Scheduled(fixedDelay = 5000)
 public void scheduled2() {
 log.info(“hello3”);
 }
 }
4.在主类上使用@EnableScheduling注解开启对定时任务的支持。
5.执行时间的配置方式
 使用@Scheduled注解来设置任务的执行时间,三种属性配置方式:
 fixedRate:定义一个按一定频率执行的定时任务
 fixedDelay:定义一个按一定频率执行的定时任务,不同的是改属性可以配合initialDelay, 定义该任务延迟执行时间。
 cron:通过表达式来配置任务执行时间,一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。在线cron表达式生成:http://qqe2.com/cron/index
五、Quartz使用方法
 1.添加依赖pom.xml
 
 org.springframework.boot
 spring-boot-starter-quartz
 
2.创建任务类TestQuartz,该类继承了QuartzJobBean,为执行具体任务
 public class TestQuartz extends QuartzJobBean {
 @Override
 protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
 System.out.println("hello world "));
 }
 }
3.创建配置类QuartzConfig
 @Configuration
 public class QuartzConfig {
 @Bean
 public JobDetail teatQuartzDetail(){
 return JobBuilder.newJob(TestQuartz.class).withIdentity(“testQuartz”).storeDurably().build();
 }
@Bean
public Trigger testQuartzTrigger(){
    SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
            .withIntervalInSeconds(5)  
            .repeatForever();
    return TriggerBuilder.newTrigger().forJob(teatQuartzDetail())
            .withIdentity("testQuartz")
            .withSchedule(scheduleBuilder)
            .build();
}
}








