java的定时器,
分享于 点击 12012 次 点评:120
java的定时器,
1.写一个计算器的工具类,配置省略
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /* * 定时任务处理,必须在启动类加上注解@EnableScheduling,开启任务调度 * **/ @Component public class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //下面的任务每隔5000毫秒,即每隔5秒执行一次。 @Scheduled(fixedRate = 5000) public void reportCurrentTime() { log.info("使用fixedRate执行定时任务,当前时间 {}", dateFormat.format(new Date())); } //下面的任务每隔3秒开始 @Scheduled(cron = "0/3 * * * * ?") public void reportCurrentTimeByCron() { log.info("使用cron表达式执行定时任务,当前时间 {}", dateFormat.format(new Date())); } }
2.我必须在启动类里面加注解
@EnableScheduling 开始任务调度
相关文章
- 暂无相关文章
用户点评