方法名称 schedule() 和 scheduleAtFixedRate() 的区别

两种情况看区别

首次计划执行的时间早于当前时间

比如说:当前时间是 11:06, 但是首次计划执行的时间应该为: 11:00

任务执行所需的时间超出任务的执行周期间隔

比如说:我们执行的任务的时间为 3秒,但是任务执行的周期间隔为 2秒。

详细分析

首次计划的时间早于当前时间

schedule 方法

fixed-delay; 如果第一次执行的时间被 delay 了,随后的执行时间按照上一次实际执行完成的时间点进行计算。

代码实例

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class DifferenceTest {

public static void main(String[] args) {

// 获取当前时间按照指定的格式输出

final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar calendar = Calendar.getInstance();

System.out.println("current time is:"+ sf.format(calendar.getTime()));

// 设置成6秒之前的时间

calendar.add(Calendar.SECOND, -6);

System.out.println("current time minus six second is :"+ sf.format(calendar.getTime()));

System.out.println("Task is being executed!");

// 使用 timer 来执行

Timer timer = new Timer();

timer.schedule(new TimerTask() {

@Override

public void run() {

// TODO Auto-generated method stub

System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));

}

}, calendar.getTime(), 2000);

}

}

控制台输出

current time is:2018-07-05 13:09:57

current time minus six second is :2018-07-05 13:09:51

scheduled exec time is :2018-07-05 13:09:57

scheduled exec time is :2018-07-05 13:09:59

scheduled exec time is :2018-07-05 13:10:01

scheduled exec time is :2018-07-05 13:10:03

scheduled exec time is :2018-07-05 13:10:05

scheduled exec time is :2018-07-05 13:10:07

scheduled exec time is :2018-07-05 13:10:09

schedule 方法总结

虽然我们是将事件提前了6秒,但是使用 schedule 还是从当前时间开始执行。然后每隔两秒执行一次。

scheduleAtFixedRate 方法

fixed-rate; 如果第一次执行时间被 delay了,随后的执行时间按照上一次开始的点计算,并且为了赶上进度会多次执行任务,因为 TimerTask中的执行需要考虑同步。

代码实例

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class DifferenceTest {

public static void main(String[] args) {

// 获取当前时间按照指定的格式输出

final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar calendar = Calendar.getInstance();

System.out.println("current time is:"+ sf.format(calendar.getTime()));

// 设置成6秒之前的时间

calendar.add(Calendar.SECOND, -6);

System.out.println("current time minus six second is :"+ sf.format(calendar.getTime()));

// 使用 timer 来执行

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

@Override

public void run() {

// TODO Auto-generated method stub

System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));

}

}, calendar.getTime(), 2000);

}

}

控制台输出

current time is:2018-07-06 14:32:34

current time minus six second is :2018-07-06 14:32:28

scheduled exec time is :2018-07-06 14:32:28

scheduled exec time is :2018-07-06 14:32:30

scheduled exec time is :2018-07-06 14:32:32

scheduled exec time is :2018-07-06 14:32:34

scheduled exec time is :2018-07-06 14:32:36

scheduled exec time is :2018-07-06 14:32:38

scheduleAtFixedRate 方法总结

我们可以看到实际的效果是:在启动执行的时候,会立马执行3次,就是为了追赶已经过去的6秒。然后再按照设定的间隔,每两秒钟执行一次。

任务执行所需的时间超出任务的执行周期间隔

schedule 方法

下次执行时间相当于上一次实际执行完成的时间点,因为执行的时间会不断延后。

代码实例

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class DifferenceTest {

public static void main(String[] args) {

// 获取当前时间按照指定的格式输出

final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar calendar = Calendar.getInstance();

System.out.println("current time is:"+ sf.format(calendar.getTime()));

// 使用 timer 来执行

Timer timer = new Timer();

timer.schedule(new TimerTask() {

@Override

public void run() {

// 模拟当前执行的过程需要 3秒

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

// 打印最近一次执行的时间

System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));

}

}, calendar.getTime(), 2000);

}

}

控制台输出

current time is:2018-07-06 14:43:51

scheduled exec time is :2018-07-06 14:43:51

scheduled exec time is :2018-07-06 14:43:54

scheduled exec time is :2018-07-06 14:43:57

scheduled exec time is :2018-07-06 14:44:00

scheduled exec time is :2018-07-06 14:44:03

说明

我们可以空控制台中输出的结果中看到:我们当前的时间为 14:43:51,然后第一次计划执行的时间也为 14:43:51。但是以后每次执行的时间都是相隔 3秒钟,并不是我们上面设置 timerTask 的时间间隔 2秒。所以说使用 schedule 方法,在这种情况下会不断的延后。

scheduleAtFixedRate 方法

下一次执行时间相对于上一次开始的时间点,因此执行时间一般不会延后,因此存在并发性

代码实例

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class DifferenceTest {

public static void main(String[] args) {

// 获取当前时间按照指定的格式输出

final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar calendar = Calendar.getInstance();

System.out.println("current time is:"+ sf.format(calendar.getTime()));

// 使用 timer 来执行

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

@Override

public void run() {

// 模拟当前执行的过程需要 3秒

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

// 打印最近一次执行的时间

System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));

}

}, calendar.getTime(), 2000);

}

}

控制台输出

current time is:2018-07-07 10:15:51

scheduled exec time is :2018-07-07 10:15:51

scheduled exec time is :2018-07-07 10:15:53

scheduled exec time is :2018-07-07 10:15:55

scheduled exec time is :2018-07-07 10:15:57

scheduled exec time is :2018-07-07 10:15:59

说明

当执行的频率为2秒钟,但是执行的时间为3秒的时。我们从控制台上的输出可以看到,执行的频率还是为2秒,因此就会存在并发性。

getwayworker timer_Java定时器之Timer学习二相关推荐

  1. linux trace学习(二)——trace使用

    linux trace学习(二)--trace使用 备注:   1. Kernel版本:4.19.123   2. 使用工具:Source Insight 4.0   3. 参考博客: Linux f ...

  2. [19/04/12-星期五] 多线程_任务定时调度(Timer、Timetask和QUARTZ)

    一.Timer和Timetask 通过Timer和Timetask,我们可以实现定时启动某个线程. java.util.Timer 在这种实现方式中,Timer类作用是类似闹钟的功能,也就是定时或者每 ...

  3. C#多线程学习(二) 如何操纵一个线程

    C#多线程学习(二) 如何操纵一个线程 原文链接:http://kb.cnblogs.com/page/42529/ [1] C#多线程学习(二) 如何操纵一个线程 [2] C#多线程学习(二) 如何 ...

  4. spring security 学习二

    spring security 学习二 doc:https://docs.spring.io/spring-security/site/docs/ 基于表单的认证(个性化认证流程): 一.自定义登录页 ...

  5. STL源码剖析学习二:空间配置器(allocator)

    STL源码剖析学习二:空间配置器(allocator) 标准接口: vlaue_type pointer const_pointer reference const_reference size_ty ...

  6. mysql用创建的用户登陆并修改表格_MySQL 基础学习二:创建一个用户表,并增删改查...

    MySQL 基础学习二:创建一个用户表,并 增删改查 提示:MySQL 命令建议都用大写,因为小写运行时,还是翻译成大写的. 第一步,创建一个用户表 1,打开控制台,进入数据库 C:\Users\Ad ...

  7. OpenCV学习(二十四 ):角点检测(Corner Detection):cornerHarris(),goodFeatureToTrack()

    OpenCV学习(二十四 ):角点检测(Corner Detection):cornerHarris(),goodFeatureToTrack() 参考博客: Harris角点检测原理详解 Harri ...

  8. OpenCV学习(二十二) :反向投影:calcBackProject(),mixChannels()

    OpenCV学习(二十二) :反向投影:calcHist(),minMaxLoc(),compareHist() 参考博客: 反向投影backproject的直观理解 opencv 反向投影 颜色直方 ...

  9. OpenCV学习(二十一) :计算图像连通分量:connectedComponents(),connectedComponentsWithStats()

    OpenCV学习(二十一) :计算图像连通分量:connectedComponents(),connectedComponentsWithStats() 1.connectedComponents() ...

最新文章

  1. 模型裁剪--Rethinking the Value of Network Pruning
  2. 微信小程序云开发 | 云函数安装依赖
  3. VTK:Qt之RenderWindowUISingleInheritance
  4. 缓存穿透、缓存雪崩、redis并发
  5. python开发之路---第二章之--函数之匿名函数
  6. awk编程基本使用示例
  7. 关于卷积神经网络可视化的一点心得
  8. docker 启动镜像_Docker学以致用,开源项目和云服务,是最好的学习资源
  9. Java web切面编程
  10. n1进入recovery模式_OPPO N1如何进入recovery模式
  11. 浅析API网关——Ocelot[网关]+Consul[服务发现负载均衡]+Polly[服务熔断]+Ids4[服务认证]
  12. 最美应用-从Android研发工程师的角度之[厨房故事]
  13. java基于ssm的自助旅游管理系统
  14. 存储函数与存储过程(有这一篇就够了)
  15. 一例所有文件都打不开的数据恢复过程
  16. 2.4G无线收发模块的应用
  17. html手机和电脑端网页效果相同,关于手机网页和电脑网页相同内容会不会重复收录的问题...
  18. 对塑料瓶无知真是害死人 从塑料瓶底数字看其毒性[转]
  19. 【论文总结】飞行器任务规划技术综述沈成林教授/国防科大
  20. 苹果微信王者荣耀哪个服务器人多,王者荣耀:QQ区和微信区哪个玩家多?看完会明白...

热门文章

  1. Redis在Window服务下的安装
  2. android最新设计规范,Android应用未来的设计规范
  3. 页表长度和页表大小_在请求调页系统中,若逻辑地址中的页号超过页表控制寄存器中的页表长度,则会引起( ) 。_学小易找答案...
  4. 制作 mysql的rpm文件_自制mysql.rpm安装包
  5. Foxmail新建自动标签功能在哪 如何给Foxmail收件人邮件设置自动标签
  6. 搜狗浏览器中如何删除自带工具 搜狗浏览器删除自带工具的方法步骤
  7. uefi启动如何进入
  8. myeclipse不是eclipse,servlet 报错 HttpServlet cannot be resolved to a type
  9. Python 二叉树实现
  10. php用a什么软件来下载,AMQB官方PHP库