首页 > Java > java教程 > 正文

JDK Timer定时器的用法

PHP中文网
发布: 2017-06-28 10:34:11
原创
1765人浏览过

implementing and scheduling a task to be executed by a timer
1) implement a custom subclass of timertask. the run method contains the code that performs the task.
    class remindtask extends timertask {
        public void run() {
            system.out.println("time up!");
            system.exit(0);
        }
    }
2) create a thread by instantiating the timer class
    timer timer = new timer();
3) instantiate the timer task object(new remindtask())
    remindtask task = new remindtask();
4) schedule the timer task for execution.
  (1) execute the task after special milliseconds delay.
    timer.schedule(task,5*1000);
  (2) specify the time when the task suould execute.
    //execute the task at 11:01 p.m
    calendar calendar = calendar.getinstance();
    calendar.set(calendar.hour_of_day,23);
    calendar.set(calendar.minute,1);
    calendar.set(calendar.second,0);
    date time = calendar.gettime();

    timer.schedule(task,time);

stopping timer threads
  by defaulst, aprogram keeps running as long as its timer threads are running. there is four ways to terminate a timer thread
  1) invoke cancel on the timer.(timer.cancel())
  2) make the timer's thread a daemon(后台), by creating the timer like this: new timer(true). if the only threads left in the program are daemon threads, the program exits.
  3) after all the timer scheduleds tasks have finished executing,remove all references to the timer object. the timer thread will terminate.
  4) invoke the system.exit method, which makes the entire program and all its threads exit.

performing a task repeatedly
there is four timer method to perform a task repeatedly
  * 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.
    执行重复的任务,第一次在延时时间后执行,往后的以特定的时间间隔执行
    timer.schedule(new remindtask(),3*1000,1*1000)
    remindtask任务将会在3秒后执行,以后将会以1秒的间隔重复执行

  * schedule(timertask task, date time, long period)
    执行重复的任务,第一次在特定的时间执行,往后的以特定的时间间隔执行

  * 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.
    以固定的延时执行重复的任务,首次执行在特定的延时之后,以后的执行发生在特定的时间间隔之后
    temer.scheduleatfixedrate(new remindtask(),3*1000,1*1000)
  * scheduleatfixedrate(timertask task, date firsttime, long period)
    执行重复的任务,第一次在特定的时间执行,往后的以特定的时间间隔执行

  schedule和scheduleatfixedrate的区别在于,schedule以固定的相对时间间隔执行,如果某一次执行被延时了,往后的执行的执行时间也会相对延时;而scheduleatfixedrate是以绝对的时间间隔执行,如果某一次执行被延时,它的后一次执行的延时将会缩短。

以上就是JDK Timer定时器的用法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号