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是以绝对的时间间隔执行,如果某一次执行被延时,它的后一次执行的延时将会缩短。
0
0
相关文章
Java并发编程中如何实现定时任务_ScheduledExecutor使用
Java初学者项目实战:开发任务调度与提醒系统
在Java中实现简单任务调度程序_Java线程定时项目说明
如何在20秒后自动关闭Chrome浏览器(含异常处理与可靠实现方案)
在Java中如何使用Timer执行定时任务_Java定时器API解析
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。
178
2026.01.28
本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。
35
2026.01.28
AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。
79
2026.01.28
本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。
2
2026.01.28
本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。
8
2026.01.28
本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。
24
2026.01.27
在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。
122
2026.01.26
在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。
72
2026.01.26
热门下载
相关下载
精品课程
最新文章


