classWorker extends TimerTask{

publicvoidrun(){

    System.out.println("我在工作啦!");

  }}

Timer类用schedule方法或者scheduleAtFixedRate方法启动定时执行,schedule重载了四个版本,scheduleAtFixedRate重载了两个。每个方法的实现都不同,下面是每个方法的说明:

schedulepublic void schedule(TimerTask task,

long delay)

Schedules the specified task for execution after the specified delay.

Parameters:task - task to be scheduled.

delay - delay in milliseconds before task is to be executed.Throws:delay is negative, or delay + System.currentTimeMillis() is negative.说明:该方法会在设定的延时后执行一次任务。

schedulepublic void schedule(TimerTask task,

Date time)

Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution.

Parameters:task - task to be scheduled.

time - time at which task is to be executed.Throws:time.getTime() is negative.说明:该方法会在指定的时间点执行一次任务。

schedulepublic void schedule(TimerTask task,

long delay,

long period)

Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.

Parameters:task - task to be scheduled.

delay - delay in milliseconds before task is to be executed.

period - time in milliseconds between successive task executions.Throws:delay is negative, or delay + System.currentTimeMillis() is negative.说明:该方法会在指定的延时后执行任务,并且在设定的周期定时执行任务。

schedulepublic void schedule(TimerTask task,

Date firstTime,

long period)

Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.

Parameters:task - task to be scheduled.

firstTime - First time at which task is to be executed.

period - time in milliseconds between successive task executions.Throws:time.getTime() is negative.说明:该方法会在指定的时间点执行任务,然后从该时间点开始,在设定的周期定时执行任务。特别的,如果设定的时间点在当前时间之前,任务会被马上执行,然后开始按照设定的周期定时执行任务。

scheduleAtFixedRatepublic void scheduleAtFixedRate(TimerTask task,

long delay,

long period)

Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.

Parameters:task - task to be scheduled.

delay - delay in milliseconds before task is to be executed.

period - time in milliseconds between successive task executions.Throws:delay is negative, or delay + System.currentTimeMillis() is negative.说明:该方法和schedule的相同参数的版本类似,不同的是,如果该任务因为某些原因(例如垃圾收集)而延迟执行,那么接下来的任务会尽可能的快速执行,以赶上特定的时间点。

scheduleAtFixedRatepublic void scheduleAtFixedRate(TimerTask task,

Date firstTime,

long period)

Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.

Parameters:task - task to be scheduled.

firstTime - First time at which task is to be executed.

period - time in milliseconds between successive task executions.Throws:time.getTime() is negative.说明:和上一个方法类似。

下面是我的一个测试片断:

publicstaticvoidmain(String[] args) throws Exception{

    Timer timer=newTimer(false);

    timer.schedule(newWorker(),newDate(System.currentTimeMillis()+1000));

  }

posted on 2005-06-09 10:29 小米 阅读(33077) 评论(7)  编辑  收藏 所属分类: Java

java timer定时执行一次_用java.util.Timer定时执行任务相关推荐

  1. scheduled线程池ScheduledExecutorService只执行一次_有个定时任务突然不执行了

    scheduled线程池ScheduledExecutorService只执行一次_有个定时任务突然不执行了 原因 If any execution of the task encounters an ...

  2. java项目中多个定时器_在java项目中如何使用Timer定时器

    在java项目中如何使用Timer定时器 发布时间:2020-11-16 16:36:16 来源:亿速云 阅读:97 作者:Leah 在java项目中如何使用Timer定时器?很多新手对此不是很清楚, ...

  3. python 定时执行 爬虫 模块_浅析python实现scrapy定时执行爬虫

    项目需要程序能够放在超算中心定时运行,于是针对scrapy写了一个定时爬虫的程序main.py ,直接放在scrapy的存储代码的目录中就能设定时间定时多次执行. 最简单的方法:直接使用Timer类 ...

  4. java执行python脚本_通过Java调用Python脚本

    在进行开发的过程中,偶尔会遇到需要使用Java调用Python脚本的时候,毕竟Python在诸如爬虫,以及科学计算等方面具有天然的优势.最近在工作中遇到需要在Java程序中调用已经写好的Python程 ...

  5. java gc回收堆还是栈_浅析JAVA的垃圾回收机制(GC)

    1.什么是垃圾回收? 垃圾回收(Garbage Collection)是Java虚拟机(JVM)垃圾回收器提供的一种用于在空闲时间不定时回收无任何对象引用的对象占据的内存空间的一种机制. 注意:垃圾回 ...

  6. java 必须try catch的异常_【java基础之异常】死了都要try,不淋漓尽致地catch我不痛快!...

    @ 1.异常 1.1 异常概念 异常 :简单说就是不正常运行,最终导致JVM的非正常停止. 在Java等面向对象的编程语言中,异常本身是一个类,产生异常就是创建异常对象并抛出了一个异常对象.Java处 ...

  7. java模拟摇摆小球程序代码_用java实现跳动的小球示例代码

    实现效果为一个小球接触左右侧时,会反向的运动. import javafx.application.Application; import javafx.event.ActionEvent; impo ...

  8. java基础入门课后习题答案_《Java基础入门》课后习题及答案

    <Java基础入门>课后习题及答案Java基础入门,课后习题,答案 博学谷--让IT教学更简单,让IT学习更有效 第6章JavaAPI 一.填空题 1.在Java中定义了两个类来封装对字符 ...

  9. java语言中声明布尔型_【Java初探02】——Java语言基础

    本篇博文就Java语言的一些基本元素进行一些记录和阐述,主要讲解一下Java语言的一些基本构成元素和Java的主类结构. Java语言基础的大致组成 java主类结构 基本的数据类型 变量与常量 运算 ...

最新文章

  1. 清华「计图」现在支持国产芯片了!动态图推理比PyTorch快了270倍
  2. Python基础1 历史 变量
  3. linux ap程序,ubuntu(linux)无线网卡开启/关闭wifi(AP)
  4. 运放搭建主动滤波电路
  5. eclipse 代码上传github 笔记
  6. [vue] watch和计算属性有什么区别?
  7. JavaScript Iframe富文本编辑器中的光标定位
  8. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 23丨向CEO汇报工作的人【难度中等】​
  9. Linux Shell 编程实战技巧
  10. abb机器人伺服电机报闸是什么_ABB机器人电池更换时回零程序Reference
  11. [jQuery1.9]Cannot read property ‘msie’ of undefined错误的解决方法
  12. 如何给软件开发项目估价?
  13. 1086 Tree Traversals Again (25分)
  14. 聚名网:华为申请“燃力红”商标,广告语注册商标需要符合哪些条件呢?
  15. 计算机创新创业1000字,大学生创新创业论文1000字
  16. bp神经网络和cnn神经网络,bp神经网络和神经网络
  17. 硬仗酒全线升级,新概念新玩法新风尚
  18. 基于《环境影响评价技术导则生态影响》(HJ 19—2022)下的生态环境影响评价技术方法及图件制作与案例
  19. 垂直对齐:vertical-align属性(转)
  20. 微信小程序后端获取用户信息

热门文章

  1. 团队作业7——第二次项目冲刺(Beta版本)-第三篇
  2. 李书福造手机,图个吉利?
  3. POJ 2983 浅谈差分约束系统处理严格等价性问题
  4. css动态显示或隐藏
  5. 区块链公司谈区块链未来走向
  6. android 环信消息撤回
  7. 可对前端打包器所构建的网站进行一键扫描的Packer Fuzzer
  8. 2021 KDD投稿时间
  9. 西安考研计算机分低的学校,西安计算机考研学校排名(读研究生毕业能干嘛)
  10. 剪辑过程中的思考与总结(持续更新ing)