Timer TimerTask schedule scheduleAtFixedRate

jdk 自带的 timer 框架是有缺陷的, 其功能简单,而且有时候它的api 不好理解。

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TestTimer {

    private static final int delay = 1000;

    /**
     * @param args
     */
    public static void main(String[] args) {
        Timer t = new Timer();
        int seconds = 2;
        TimerTask task = new SimpleTask();
        t.schedule(task , delay, seconds * 1000);
    }

}

class SimpleTask extends TimerTask{

    @Override
    public void run() {

        System.out.println("SimpleTask.run() 11 " + new Date());

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //System.out.println("SimpleTask.run() 22 " + new Date());
    }

}

结果为

SimpleTask.run() 11 Mon May 30 16:43:58 CST 2016
SimpleTask.run() 11 Mon May 30 16:44:02 CST 2016
SimpleTask.run() 11 Mon May 30 16:44:06 CST 2016

显示是每隔4秒, 而不是我想象的 period +  sleep time  即 2+4 = 6s, why ??

原来是这样的:

参考: http://stackoverflow.com/questions/15128937/java-util-timer-fixed-delay-not-working

Nope, that‘s how it is intended to work. The period is the period between start times, not the period between an end time and the next start time.

Basically you‘re telling it in plain english "start at 1 second and execute every two seconds after that" so 1, 3, 5, 7, etc is the logical interpretation.

shareimprove this answer
answered Feb 28 ‘13 at 6:34

Affe
31k25064

Ok, I re-read the Javadoc again. That means ‘schedule‘ uses previous task‘s start-time as reference (2nd task will reference 1st task, 3rd task will reference 2nd task). While ‘scheduleAtFixedRate‘ always use the first task‘s start-time as reference (all task‘s start-time are based on the 1st task‘s start-time). Is that how it works? – Dave Xenos Feb 28 ‘13 at 6:52

Yeah, the way I think of it is if ‘schedule‘ misses one, it forgets about it, if ‘scheduleAtFixedRate‘ misses one, it puts in a make-up. – Affe Mar 1 ‘13 at 2:17

时间: 2024-08-29 17:36:35

Timer TimerTask schedule scheduleAtFixedRate的相关文章

简单理解java中timer的schedule和scheduleAtFixedRate方法的区别

timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有来得及完成下次任务又交到手上. 我们来举个例子: 暑假到了老师给schedule和scheduleAtFixedRate两个同学布置作业. 老师要求学生暑假每天写2页,30天后完成作业. 这两个学生每天按时完成作业,直到第10天,出了意外,两个学生出去旅游花了5天时间,这5天时间里两个人都没有做作业.任务被拖延了. 这时候两个学生采取的策略就不同了: sch

Java中timer的schedule()和schedualAtFixedRate()函数的区别

本文主要讨论java.util.Timer的schedule(timerTask,delay,period)和scheduleAtFixedRate(timerTask,delay,period)的区别. 这两个函数不管是哪一个,TImer都是单线程的,任务始终在这个单线程里面执行. 下面讨论四种情况: [(任务3s,间隔2s)+(任务2s,间隔3s)]×[schedule+scheduleAtFixedRate] schedule,任务3s,间隔2s new Timer().schedule(

Handler+Timer/TimerTask实现ViewPager的自动循环播放

ViewPager是android.support.v4中提供的空间,和IOS中的UIScrollView有类似的效果,ViewPager正常工作需要一个PagerAdapter. PagerAdapter.java如下: package com.mxd.studyandroid; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import

Timer的schedule和scheduleAtFixedRate方法的区别解析

在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)schedule方法:"fixed-delay":如果第一次执行时间被delay了,随后的执行时间按 照 上一次 实际执行完成的时间点 进行计算 (2)scheduleAtFixedRate方法:"fixed-rate":如果第一次执行时间被delay了,随后的执行时间按照 上一

Timer的schedule和scheduleAtFixedRate方法的区别解析(转)

在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)schedule方法:“fixed-delay”:如果第一次执行时间被delay了,随后的执行时间按 照 上一次 实际执行完成的时间点 进行计算(2)scheduleAtFixedRate方法:“fixed-rate”:如果第一次执行时间被delay了,随后的执行时间按照 上一次开始的 时间点 进行计算,并且

android timer的schedule和scheduleAtFixedRate运用

在java中,Timer类主要用于定时性.周期性任务的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 java代码如下: [java] view plaincopy SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date startDate = dateFormatter.parse("2

Java Timer, TimerTask, Timer.Schedule

schedule的意思(时间表.进度表) timer.schedule(new TimerTask(){ void run()},0, 60*60*1000);timer.schedule(new MyTask(event.getServletContext()), 0, 60*60*1000);第一个参数"new MyTask(event.getServletContext())":是 TimerTask 类,在包:import java.util.TimerTask .使用者要继承

Timer TimerTask CountDown 计时器 API

Timer 计时器 一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行. 与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务.计时器任务应该迅速完成.如果完成某个计时器任务的时间太长,那么它会"独占"计时器的任务执行线程.因此,这就可能延迟后续任务的执行,而这些任务就可能"堆在一起",并且在上述不友好的任务最终完成时才能够被快速连续地执行. 对 Timer 对象最后的引用完成后,并且 所有未处理的任务都已

java timer timertask mark

其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了run方法的一个类,而具体的TimerTask需要由你自己来实现,例如这样: 1 2 3 4 5 6 Timer timer = new Timer(); timer.schedule(new TimerTask() {         public void run() {             System.out.println("abc");         } }, 200000 , 1000); 这里