SpringBoot定时任务
代码做定时任务:1、开个线程,线程里面休眠去做
2、使用一些定时任务的框架去做
1、创建TimerTest类
package com.cppdy.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class TimerTest { @Scheduled(fixedRate=2000) public void showTime() { System.out.println("当前时间戳:"+System.currentTimeMillis()); } }
2、在Application类中开启定时任务(@EnableScheduling)
package com.cppdy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }