最好的方法是在两者之间区分 timer灵感时间设定过去时间T,scheduleAtFixedRate将从T现在所有的任务中运行,schedule而该任务将只运行从现在开始计时。

public class HelloMain {

    private static int count = 0;

    public static void main(String[] args) {

        Date date = new Date(System.currentTimeMillis() - 3000);

        Timer timer = new Timer();

        System.out.println("timer schedule before");
timer.scheduleAtFixedRate(new TimerTask() { @Override
public void run() {
count++;
System.out.println(count + " timer task run " + Calendar.getInstance().getTime()); }
}, date, 1000);
System.out.println("timer schedule after"); try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("timer cancel before");
timer.cancel();
System.out.println("timer cancel after");
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

schedule vs scheduleAtFixedRate的更多相关文章

  1. schedule() 和 scheduleAtFixedRate() 区别

    1.  schedule() ,2个参数方法:在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行. ...

  2. schedule和scheduleAtFixedRate区别

    需求: 由于系统长期运作,各设备之间产生很多信息,一段时间后需要清除数据 考虑方案: 用schedule还是scheduleAtFixedRate,在此比较分析了下这两个的区别 schedule和sc ...

  3. schedule() 和 scheduleAtFixedRate() 的区别--转载

    1.  schedule() ,2个参数方法:在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行. ...

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

    timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有来得及完成下次任务又交到手上. 我们来举个例子: 暑假到了老 ...

  5. Timer类的schedule和scheduleAtFixedRate 简单应用

    Timer类可以用作定时任务,主要的方法有schedule和scheduleAtFixedRate. schedule(TimerTask task, Date time) 安排在指定的时间执行指定的 ...

  6. schedule与scheduleAtFixedRate比较

    schedule与scheduleAtFixedRate: 不延时: schedule(TimerTask, Date runDate, long period)方法任务不延时----Date类型 i ...

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

    在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...

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

    在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...

  9. Java并发之ScheduledExecutorService(schedule、scheduleAtFixedRate、scheduleWithFixedDelay)

    package com.thread.test.thread; import java.util.Timer; import java.util.TimerTask; import java.util ...

随机推荐

  1. PHP操作Mysql中间BLOB场

    1.MySQL在BLOB字段类型 BLOB场的类型用于存储二进制数据. MySQL在.BLOB它是一种类型的一系列.含有:TinyBlob.Blob.MediumBlob.LongBlob.大小上不同 ...

  2. httpclient超时总结(转)

    Httpclient超时 背景: 网站这边多次因为httpclient调用超时时间没设置好导致关掉,影响非常不好,而且问题重复出现,查看网络,没有比较明确介绍httpclient所有超时相关的设置(大 ...

  3. SPOJ PGCD(莫比乌斯反演)

    传送门:Primes in GCD Table 题意:给定两个数和,其中,,求为质数的有多少对?其中和的范围是. 分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0). ...

  4. www.centos.org

    https://www.centos.org/forums/viewtopic.php?t=5770 In order to conserve the limited bandwidth availa ...

  5. hdu1292(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1292 分析:    i代表人数,j代表组数,有dp[i][j]=dp[i-1][j-1]+dp[i-1 ...

  6. poj2479(dp)

    题目链接:http://poj.org/problem?id=2479 题意:求所给数列中元素值和最大的两段子数列之和. 分析:从左往右扫一遍,b[i]表示前i个数的最大子数列之和. 从右往左扫一遍, ...

  7. 在IIS上发布一个WebService,再发布一个网站调用这个WebService(实例)

    首先描述一下先决条件:IIS可用,VS2005可用. 好,现在开始: 首先写一个WebService并把它发布到IIS上: 在IIS上的默认网站下新建一个“虚拟目录”,取名为“webservice1” ...

  8. 新版SDK自己主动加入PlaceholderFragment的思考

    自从Android SDK更新到22.6.3,发现新建Activity的时候,会自己主动生成一个Fragment.这个Fragment是activity的静态内部类.同一时候生成了一个xml叫frag ...

  9. cocos2dx-lua牧场小游戏(一)

    环境: cocos2dx-3.0rc2,   xcode5.0 一.lua项目建立參考 http://blog.csdn.net/daydayup_chf/article/details/249641 ...

  10. Lua 与C 交换 第一篇

    编译 windows上编译lua源代码 cl /MD /O2 /W3 /c /DLUA_BUILD_AS_DLL *.c del *.o ren lua.obj lua.o ren luac.obj ...