最好的方法是在两者之间区分 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. Twitter僵尸帐号厂商雇佣中国员工专填验证码_Web2.0 - Microblogging 微博_cnBeta.COM

    Twitter僵尸帐号厂商雇佣中国员工专填验证码_Web2.0 - Microblogging 微博_cnBeta.COM Twitter僵尸帐号厂商雇佣中国员工专填验证码

  2. ubuntu 经常使用软件及环境

    安装完系统后,更新系统 sudo apt-get update # 更新源索引 sudo apt-get dist-upgrade # 更新系统 sudo apt-get autoremove 1安装 ...

  3. xcode 改动整个项目名

    目标为:将项目名XCD4改成xcd5. 先上结果图:==> 1.在左側的导航区域点击两次项目名,两次间隔时间略微长些,项目名会变成可编辑状态. 将名称改动为xcd5后按enter键弹出一个对话框 ...

  4. jQuery Easy UI Droppable(放置)组件

    Droppable(放置)组件也是一个基本组件,使用方法较简单,语法都在样例里面凝视了: 演示样例: <!DOCTYPE html> <html> <head> & ...

  5. android 项目中使用对话框统一封装

    近期在做拼车项目中使用到了一些对话框,而且在非常多地方都使用到了,既然非常多地方使用到,那么肯定要封装一下,

  6. xcode中找不到XXX.dylib

    xcode中找不到 XXX.dylib 了,比如libz.tbd 如果要用到 libz.dylib,可以用下面的办法,来自 Stack Overflow. Go to Build Phases > ...

  7. MySQL 模拟Oracle邻接模型树形处理

    数据库对层次结构的处理模型有好多种,能够依据自己的需求来设计模型.当然最简单的也是最easy设计的模型就是所谓的邻接模型.在这方面,其它数据库比方Oracle 提供了现成的分析方法 connect b ...

  8. String的Split方法的用法与要注意事项

    转自:http://shukuiyan.iteye.com/blog/1058672 之前在http://shukuiyan.iteye.com/blog/507915文中已经叙述过这个问题,但是最近 ...

  9. Install Linux Kernel - AT91SAM9260EK

    两.AT91SAM9260EK 2.1下载 介绍页: http://www.at91.com/linux4sam/bin/view/Linux4SAM/LegacyLinuxKernel 下载页: a ...

  10. css实现背景渐变色效果

    webkit内核的浏览器,例如(chrome,safari等) background:-webkit-gradient(linear,0 0,0 100%,from(#000000),to(#ffff ...