Java 定时任务-笔记
[toc]
一、线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class demo1 { public static void main(String[] args) throws InterruptedException { final long interval = 3000; Runnable runnable = new Runnable(){ @Override public void run() { System.out.println("hello world!"); } };
Thread.sleep(5000); while(true){ Thread thread1 = new Thread(runnable); Thread.sleep(1000); thread1.start(); } } }
|
二、Timer 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import java.util.Timer; import java.util.TimerTask;
public static void main(String[] args) { Timer timer = new Timer(); timer.schedule(new TimerTask() { int count = 0; @Override public void run() { ++count; System.out.println("hello world! "+count); } },5000,1000); timer.scheduleAtFixedRate(new TimerTask() { int count = 0;
@Override public void run() { ++count; System.out.println("hello world! "+count); } },5000,1000); }
|
三、Quartz 框架
Job实现类(需要无参构造,所以必须写成一个文件):
1 2 3 4 5 6 7 8 9
| public class MyJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("hello world! "); } }
|
利用时间间隔,任务调度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public static void main(String[] args) throws SchedulerException {
MyJob myJob = new MyJob();
StdSchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(myJob.getClass()) .withIdentity("job1", "group1") .build();
SimpleTrigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger", "triggerGroup") .startAt(new Date(System.currentTimeMillis()+5000)) .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(1) .repeatForever() ) .build();
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
|
利用cron
表达式,任务调度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.cyw.task.test;
import org.quartz.*; import org.quartz.impl.StdScheduler; import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
public class demo3 { public static void main(String[] args) throws SchedulerException {
MyJob myJob = new MyJob();
StdSchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(myJob.getClass()) .withIdentity("job1", "group1") .build();
CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity("trigger2", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("5/1 * * * * ? *") ) .build();
scheduler.scheduleJob(jobDetail,cronTrigger);
scheduler.start();
} }
|
四、SpringTask
在启动类上,@EnableScheduling
开启任务调度:
1 2 3 4 5 6 7 8 9
| @EnableScheduling @SpringBootApplication public class TaskApplication {
public static void main(String[] args) { SpringApplication.run(TaskApplication.class, args); }
}
|
编写任务类:
1 2 3 4 5 6 7 8 9 10
| import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
@Component public class MyTask { @Scheduled(cron = "40/5 * * * * ?") public void task(){ System.out.println("hello world!"); } }
|