https://mp.weixin.qq.com/s/DD7AngUgYtVui0qqZzKHRw
1、使用线程创建 job 定时任务
/** * TODO 使用线程创建 job 定时任务 * @author 王松 */ public class JobThread { public static class Demo01 { static long count = 0; public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(1000); count++; System.out.println(count); } catch (Exception e) { // TODO: handle exception } } } }; Thread thread = new Thread(runnable); thread.start(); } } }
2、使用 TimerTask 创建job定时任务
/** * TODO 使用 TimerTask 创建job定时任务 * @author 王松 */ public class JobTimerTask { static long count = 0; public static void main(String[] args) { TimerTask timerTask = new TimerTask() { @Override public void run() { count++; System.out.println(count); } }; //创建timer对象设置间隔时间 Timer timer = new Timer(); // 间隔天数 long delay = 0; // 间隔毫秒数 long period = 1000; timer.scheduleAtFixedRate(timerTask, delay, period); } }
3、使用线程池创建 job定时任务
/** * TODO 使用线程池创建 job定时任务 * @author 王松 */ public class JobScheduledExecutorService { public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { // task to run goes here System.out.println("Hello !!"); } }; ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间 service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS); } }
4.Quartz 框架
1.引入maven依赖
<dependencies> <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency> </dependencies>
2.任务调度类
public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("quartz MyJob date:" + System.currentTimeMillis()); } }
3.启动类
public class JobQuartz { public static void main(String[] args) throws SchedulerException { //1.创建Scheduler的工厂 SchedulerFactory sf = new StdSchedulerFactory(); //2.从工厂中获取调度器实例 Scheduler scheduler = sf.getScheduler(); //3.创建JobDetail, JobDetail jb = JobBuilder.newJob(MyJob.class) //job的描述 .withDescription("this is a ram job") //job 的name和group .withIdentity("ramJob", "ramGroup") .build(); //任务运行的时间,SimpleSchedle类型触发器有效,3秒后启动任务 long time= System.currentTimeMillis() + 3*1000L; Date statTime = new Date(time); //4.创建Trigger //使用SimpleScheduleBuilder或者CronScheduleBuilder Trigger t = TriggerBuilder.newTrigger() .withDescription("") .withIdentity("ramTrigger", "ramTriggerGroup") //.withSchedule(SimpleScheduleBuilder.simpleSchedule()) //默认当前时间启动 .startAt(statTime) //两秒执行一次,Quartz表达式,支持各种牛逼表达式 .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) .build(); //5.注册任务和定时器 scheduler.scheduleJob(jb, t); //6.启动 调度器 scheduler.start(); }
5. springboot 的 @Scheduled 注解
@Component @Configuration //1.主要用于标记配置类,兼备Component的效果。 @EnableScheduling // 2.开启定时任务 public class SaticScheduleTask { @Scheduled(cron = "0/5 * * * * ?") //3.添加定时任务 //@Scheduled(fixedRate=5000) //或直接指定时间间隔,例如:5秒 private void configureTasks() { System.err.println("执行静态定时任务时间: " + LocalDateTime.now()); } }