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中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

禅工作室 13.0.1
功能强大的PHP集成开发环境

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境