0

0

JDK Timer定时器的用法

PHP中文网

PHP中文网

发布时间:2017-06-28 10:34:11

|

1807人浏览过

|

来源于php中文网

原创

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是以绝对的时间间隔执行,如果某一次执行被延时,它的后一次执行的延时将会缩短。

思高数码广场
思高数码广场

修订记录:1.用户登陆增加了验证码,提高了安全性,登陆后不再显示登陆框。2.修改成了相对路径,不再一定需要放置在网站根目录下面。3.修改了类别搜索功能,并且可以暂时屏蔽,而不是删除,修改了先增加大类后出现无法修改中类名称的bug。4.增加了产品修改,可以保存修改。5.原来是借用别人的流量统计系统,不能统计某个页面的访问量,现采用的count.seecool.net的流量统计。6.帮助中心采用了fa

下载

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

32

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

23

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

16

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

5

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

6

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

268

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

195

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

170

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

85

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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