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赚钱平台入口大全
2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。
268
2026.01.31
无需付费的漫画app大全
想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。
170
2026.01.31
漫画免费在线观看地址大全
想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。
85
2026.01.31
热门下载
相关下载
精品课程
相关推荐
/
热门推荐
/
最新课程
最新文章







