转自:http://aguang520.iteye.com/blog/830864
org.quartz.jobStore.misfireThreshold = 60000 #60秒 默认值
那么执行第一次作业是在10:01秒,这时会设定下一次的执行时间为10:02秒,要等一个作业执行完之后才有可用线程,大概要在10:11秒才能执行前面安排的应该在10:02执行的作业,这时就会用到misfireThreshold, 因为10:11与10:02之间的差值小于6000,所以执行该作业,并以10:02为基准设置下一次执行时间为10:03,这样造成每次实际执行时间与安排时间错位
如果 org.quartz.jobStore.misfireThreshold = 6000 #秒
同样,在10:11计划执行安排在10:02的作业,发现10:11与10:02之间的差值小于6000,那么直接跳过该作业,执行本应在当前时间执行的作业,这时候会以10:11为基准设定下次作业执行时间为10:12
misfireThreshold只有当job任务被阻塞时才有效,如果线程池里线程很多,该参数没有意义。所以大部分时候只对有状态的job才有意义。
public class HelloJob implements StatefulJob {
public void execute(JobExecutionContext context) {
DateFormat df = new SimpleDateFormat("hh:mm:ss");
System.err.println("[" + Thread.currentThread().getName() + "] Now: "
+ df.format(context.getFireTime()) + " Scheduled: "
+ df.format(context.getScheduledFireTime()) + " Previous: "
+ df.format(context.getPreviousFireTime()) + " Next: "
+ df.format(context.getNextFireTime()));
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
执行次数:
SimpleTrigger trigger2 = new SimpleTrigger();
trigger2.setName("trigger2");
trigger2.setGroup("group");
trigger2.setStartTime(new Date());
trigger2.setRepeatInterval(2000);
trigger2.setRepeatCount(4);
sched.scheduleJob(job2, trigger2);
执行结果:
60秒执行任务:
[DefaultQuartzScheduler_Worker-2] Now: 05:36:42 Scheduled: 05:36:42 Previous: 05:36:40 Next: 05:36:44
[DefaultQuartzScheduler_Worker-3] Now: 05:36:52 Scheduled: 05:36:44 Previous: 05:36:42 Next: 05:36:46
[DefaultQuartzScheduler_Worker-4] Now: 05:37:02 Scheduled: 05:36:46 Previous: 05:36:44 Next: 05:36:48
[DefaultQuartzScheduler_Worker-5] Now: 05:37:12 Scheduled: 05:36:48 Previous: 05:36:46 Next: 05:36:50
3秒执行任务:
[DefaultQuartzScheduler_Worker-2] Now: 05:41:36 Scheduled: 05:41:36 Previous: 05:41:34 Next: 05:41:38
[DefaultQuartzScheduler_Worker-3] Now: 05:41:46 Scheduled: 05:41:46 Previous: 05:41:36 Next: 05:41:48
[DefaultQuartzScheduler_Worker-4] Now: 05:41:56 Scheduled: 05:41:56 Previous: 05:41:46 Next: 05:41:58
[DefaultQuartzScheduler_Worker-5] Now: 05:42:06 Scheduled: 05:42:06 Previous: 05:41:56 Next: 05:42:08
所以说:misfireThreshold 参数对执行次数并无影响,只是对cpu对线程处理的机制(会影响效率)有所影响而已。