定时调度
package TCPUDPThread;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
/**
* 高级主题:定时调度,more
*
* 任务定时调度 通过Timer和Timetask,我们可以实现定时启动某个线程. java.util.Timer:类似闹钟的功能,本身实现的就是一个线程.
* java.util.TimerTask:一个抽象类,该类实现了Runnable接口,所以该类具备多线程的能力.
*
*
* 高级主题:quartz,任务调度框架
*
* Scheduler - 调度器,控制所有的调度
* Trigger - 触发条件,采用DSL模式
* JobDetail - 需要处理的JOB
* Job - 执行逻辑
*
* DSL:Domain-specific language 领域特定语言,针对一个特定的领域,具有受限表达性的一种计算机程序语言,即领域专用语言,声明式编程:
* 1.Method Chaining 方法链 Fluent Style流畅风格,builder模式构建器
* 2.Nested Functions 嵌套函数
* 3.Lambda Expressions/Closures
* 4.Functional Sequence
* @author pmc
*
*/
public class synchronizedTest12 {
public static void main(String[] args) {
Timer timer = new Timer();
// timer.schedule(new Mytask(), 1000);// 执行一次
// timer.schedule(new Mytask(), 1000, 200);//执行多次
Calendar cal=new GregorianCalendar(2099,12,31,21,53,54);
timer.schedule(new Mytask(), cal.getTime(), 200);//指定时间开始
}
}
// 任务类
class Mytask extends TimerTask {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("放空大脑");
}
}
}
下载调度器地址http://www.quartz-scheduler.org/downloads/
导入lib源码
测试调度器
可以使用DSL模式,类似与append().append().append...
package TCPUDPThread;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("------start");
System.out.println("------Hello World!"+new Date());
System.out.println("------end");
}
}
package TCPUDPThread;
import static org.quartz.DateBuilder.evenSecondDateAfterNow;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
public class SimpleExample {
public void run() throws Exception {
// 1.创建Scheduler的工厂
SchedulerFactory sf = new StdSchedulerFactory();
// 2.从工厂中获取调度器
Scheduler sched = sf.getScheduler();
// 3.创建JobDetail
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
// 时间
Date runTime = evenSecondDateAfterNow();
// 4.触发器
//Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
Trigger trigger = newTrigger().withIdentity("trigger3", "group1").startAt(runTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(5).withRepeatCount(3)).build();
// 5.注册任务和触发条件
sched.scheduleJob(job, trigger);
// 6.启动
sched.start();
try {
// wait 65 seconds to show job
Thread.sleep(16L * 1000L);
// executing...
} catch (Exception e) {
//
}
// shut down the scheduler
sched.shutdown(true);
}
public static void main(String[] args) throws Exception {
SimpleExample example = new SimpleExample();
example.run();
}
}
withRepeatCount() 0执行1次依此类推