scheduleAtFixedRate 如果上一个任务的执行时间大于等待时间,任务结束后,下一个任务马上执行 scheduleWithFixedDealy 如果上个任务的执行时间大于等待时间,任务结束后也会等待相应的时间才执行下一个任务...总结 也就是说,不管是scheduleAtFixedRate还是scheduleWithFixedDealy都会等待上一个任务运行结束再进行下一个任务。
我们熟悉的 scheduleAtFixedRate、scheduleWithFixedDelay 方法,还有 cron 表达式,他们的主要区别在于计算下一次调度时间的逻辑不同,这样导致调度的效果有很大的区别...我们先来看看类图: 由类图可知,ScheduledThreadPoolExecutor 继承至线程池 ThreadPoolExecutor,并且它提供了 schedule、scheduleAtFixedRate...& scheduleWithFixedDelay scheduleAtFixedRate 方法的逻辑很简单,只是构造了一个 ScheduledFutureTask 任务,然后丢到延迟队列中,具体的代码如下所示...> scheduleAtFixedRate(Runnable command, long initialDelay...前面也说了,scheduleAtFixedRate、scheduleWithFixedDelay 这两个 api 方法传递的 period 值是有正负之分的,因此计算下一次调度时间也是有差异的,具体代码如下
任务执行需要延迟的时间 11 * @ unit:时间单位 12 * 13 * 一次性执行任务,执行完成结束 14 * 15 * ScheduledExecutorService: 16 * scheduleAtFixedRate...Executors.newScheduledThreadPool(2); 52 es.schedule(mer, 10000, TimeUnit.SECONDS.MILLISECONDS); 53 es.scheduleAtFixedRate
scheduleAtFixedRate: Sun Apr 28 14:36:10 CST 2019 End : scheduleAtFixedRate: Sun Apr 28 14:36:...18 CST 2019 Start: scheduleAtFixedRate: Sun Apr 28 14:36:20 CST 2019 End : scheduleAtFixedRate:...Sun Apr 28 14:36:28 CST 2019 Start: scheduleAtFixedRate: Sun Apr 28 14:36:30 CST 2019 End : scheduleAtFixedRate...scheduleAtFixedRate: Sun Apr 28 14:30:25 CST 2019 End : scheduleAtFixedRate: Sun Apr 28 14:30:...37 CST 2019 Start: scheduleAtFixedRate: Sun Apr 28 14:30:37 CST 2019 End : scheduleAtFixedRate:
ScheduledExecutorService有时会被用来实现本地的定期任务执行,常规使用方式如下所示: scheduledExecutorService.scheduleAtFixedRate
实验一 scheduleAtFixedRate(service,1000); 输出结果为: scheduleAtFixedRate 开始执行时间:15:03:15 scheduleAtFixedRate...开始执行时间:15:03:20 scheduleAtFixedRate 执行花费时间=1m scheduleAtFixedRate 执行完成时间:15:03:21 ==================...实验二 scheduleAtFixedRate(service,6000); 输出结果为: scheduleAtFixedRate 开始执行时间:15:06:12 scheduleAtFixedRate...开始执行时间:15:06:18 scheduleAtFixedRate 执行花费时间=6m scheduleAtFixedRate 执行完成时间:15:06:24 ==================...==================== scheduleAtFixedRate 开始执行时间:15:06:24 scheduleAtFixedRate 执行花费时间=6m scheduleAtFixedRate
使用 scheduleAtFixedRate 方法执行定时任务,执行多次定时任务。 使用 scheduleWithFixedDelay 方法执行定时任务,执行多次定时任务。...2.scheduleAtFixedRate scheduleAtFixedRate 方法可以执行多次定时任务,此方法需要 4 个参数: 第 1 个参数:传递一个任务,Runnable 或 Callable...3.scheduleWithFixedDelay scheduleWithFixedDelay 方法的使用和 scheduleAtFixedRate 类似,但执行效果完全不同,这个很容易理解如果效果一样就不用创建两个方法了...scheduleWithFixedDelay 方法是在方法执行完成之后,再隔 N 秒执行下一个定时任务,和 scheduleAtFixedRate 的固定时间执行不同,scheduleWithFixedDelay...使用 scheduleAtFixedRate 方法执行定时任务,执行多次定时任务,它的执行时间间隔是固定的,不受定时任务执行时长影响(定时任务时间间隔 > 任务执行时间)。
> scheduleAtFixedRate( Runnable command, long initialDelay, long period,...> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { return...scheduledExecutorService.scheduleAtFixedRate(command, initialDelay, period, unit); } @Override...> scheduleAtFixedRate(@Nonnull Runnable command, long initialDelay, long period, @Nonnull TimeUnit unit...(-delay);ScheduledFutureTask的run方法会对period进行判断,小于等于0的,会再次调用internalSchedule方法,来实现以FixedDelay进行调度的效果 scheduleAtFixedRate
需求说明:定时更新正在生成的文件大小和状态【进行中、失败、完成】,如果文件生成完成,则退出【CoderBaby】 调度可以用Timer 【调用schedule()或者scheduleAtFixedRate...logger.warn("Catch exception : " + e.toString()); } } } 通过scheduleAtFixedRate...接口来调用(设置时间间隔和且第一次执行的延迟时间) scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(...特别说明: 关于schedule(时间基准:运行的实际时间)和scheduleAtFixedRate(时间基准:理论时间点)的区别: scheduleAtFixedRate调度一个task,在delay...schedule在计算下一次执行的时间的时候,是通过当前时间(在任务执行前得到) + 时间片,而scheduleAtFixedRate方法是通过当前需要执行的时间(也就是计算出现在应该执行的时间)+ 时间片
> t = ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread,0,2,...InterruptThread interruptThread1 = new InterruptThread(2); ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate...); } } 踩坑记录 楼主在使用如下代码时,突然想到当这个定时任务需要被停止时该如何停止线程运行 ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate...既然没有线程的那就扒一下scheduleAtFixedRate的底层源码看看是什么东西吧,果不其然我在源码中看到了scheduleAtFixedRate方法的具体实现,发现他的返回值是ScheduledFuture...> scheduleAtFixedRate(Runnable command, long initialDelay
// scheduleAtFixedRate(TimerTask task, long delay, long period) public static void timer3() {...Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void...1000, 2000); } // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行. // Timer.scheduleAtFixedRate...calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00 Timer timer = new Timer(); timer.scheduleAtFixedRate
此时,你是否疑惑schedule与scheduleAtFixedRate效果一样,为什么提供两个方法,它们有什么区别?...schedule与scheduleAtFixedRate区别 在了解schedule与scheduleAtFixedRate方法的区别之前,先看看它们的相同点: 任务执行未超时,下次执行时间 = 上次执行开始时间...scheduleAtFixedRate保持执行频率的稳定 scheduleAtFixedRate在反复执行一个task的计划时,每一次执行这个task的计划执行时间在最初就被定下来了,也就是scheduledExecutionTime...如果用一句话来描述任务执行超时之后schedule和scheduleAtFixedRate的区别就是:schedule的策略是错过了就错过了,后续按照新的节奏来走;scheduleAtFixedRate...scheduleAtFixedRate方法 scheduleAtFixedRate方法,按指定频率周期执行某个任务。定义及参数说明: public ScheduledFuture<?
执行周期(毫秒) */ public void addEveryMillisecond(Runnable task, long startSeconds) { taskScheduler.scheduleAtFixedRate...else { // 普通定时任务延时1秒执行 startDate = new Date(c.getTimeInMillis() + 1000); } taskScheduler.scheduleAtFixedRate...> scheduleAtFixedRate(Runnable task, long period) { ScheduledExecutorService executor = getScheduledExecutor...(); try { return executor.scheduleAtFixedRate(errorHandlingTask(task, true), 0, period, TimeUnit.MILLISECONDS...> scheduleAtFixedRate(Runnable command, long initialDelay
同时也重载了scheduleAtFixedRate方法,scheduleAtFixedRate方法与schedule相同,只不过他们的侧重点不同,区别后面分析。...对于上面三个实例,LZ只是简单的演示了一下,同时也没有讲解scheduleAtFixedRate方法的例子,其实该方法与schedule方法一样!...3、scheduleAtFixedRate(TimerTask task, Date firstTime, long period)、scheduleAtFixedRate(TimerTask task..., long delay, long period) 在前面也提过scheduleAtFixedRate与schedule方法的侧重点不同,schedule方法侧重保存间隔时间的稳定,而scheduleAtFixedRate...所以scheduleAtFixedRate更加侧重于保持执行频率的稳定。
schedule(TimerTask task, long delay, long period) schedule(TimerTask task, Date firstTime, long period) scheduleAtFixedRate...(TimerTask task, long delay, long period) scheduleAtFixedRate(TimerTask task, Date firstTime, long period...schedule 和 scheduleAtFixedRate 在延时的情况下没有区别。 但是在设定时间在之当前运行环境之前的话,那么调用 timer.cancel 犯法会呈现不同的状况。...schedule 会立刻停止执行,但是 scheduleAtFixedRate 会执行从设定时间到停止运行所时间段所有的次数。...那么schedule 方法只会执行5次,但是scheduleAtFixedRate 方法会执行 10 次。也就是所谓的追加执行。
TimeUnit.SECONDS ); } log.info("TaskProcessSchedule scheduleAtFixedRate...下面, 我们再来看看提交任务的方法scheduleAtFixedRate: public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay...除此之外,如果我们详细阅读了ScheduledExecutorService的scheduleAtFixedRate的 doc 文档,如下所示: /** * Creates and executes...> scheduleAtFixedRate(Runnable command, long initialDelay
,long period) 区别 相同点: 1、方法schedule 和方法 scheduleAtFixedRate 都会按顺序执行,所以不用考虑非线程安全的情况。...2、方法schedule 和方法 scheduleAtFixedRate 如果执行任务的时间没有被延迟,那么下一次任务的执行时间参考的是上一次的任务的"开始"时的时间来计算的。...3、方法schedule 和方法 scheduleAtFixedRate 如果执行任务的时间被延迟了,那么下一次任务的执行时间参考的是上一次任务"结束"时的时间来计算。 ? ?...不同点: 方法schedule 和方法 scheduleAtFixedRate 在使用上基本没什么差别,就是 scheduleAtFixedRate 具有追赶执行性,什么意思呢?...就是如果任务 在周期性运行过程中被打断了,scheduleAtFixedRate 会尝试把之前落下的任务补上运行。而schedule就不管了,接着运行接下来的任务就行了,可以参考这篇博客,写的很生动。
schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,scheduleAtFixedRate会把已经过去的时间也作为周期执行,而schedule...; } }, 0, 5*1000); } } scheduleAtFixedRate import java.text.ParseException; import java.text.SimpleDateFormat...; timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println(“scheduleAtFixedRate...scheduleAtFixedRate(TimerTask task, long delay, long period)方法设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
long initialDelay = 1; long period = 1; // 从现在开始1秒钟之后,每隔1秒钟执行一次job1 service.scheduleAtFixedRate...MyScheduledExecutor("job2"), initialDelay, period, TimeUnit.SECONDS); } ScheduledExecutorService 中两种最常用的调度方法 ScheduleAtFixedRate...ScheduleAtFixedRate 每次执行时间为上一次任务开始起向后推一个时间间隔,即每次执行时间为 :initialDelay, initialDelay+period, initialDelay...由此可见,ScheduleAtFixedRate 是基于固定时间间隔进行任务调度,ScheduleWithFixedDelay 取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度。
领取专属 10元无门槛券
手把手带您无忧上云