Java定时器类,每天在固定时间触发
我想编写一个简单的计时器类,该类在应用程序运行时触发并在每天的固定时间进行方法调用。
我不想使用 Quartz,因为我认为它对于这个简单的问题来说太过分了,我可以尝试哪些不同的方法?
提前致谢
I want to write a simple timer class that fires , and makes a method call at a fixed time of every day, while the application is running.
I dont want to use Quartz as I think its a overkill for this simple problem, what are the different approaches I can try for ?
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用 java.util.Timer 和 java.util.TimerTask?
Why not use java.util.Timer and java.util.TimerTask?
util.concurrent 的 ScheduledExecutorService 允许您可以轻松地安排任务以固定的延迟运行。 Schedule 方法的签名如下:
您可以找到当前时间与您希望首次运行它的时间之间的差异。将差值设置为 initialDelay 的值。将周期设置为1,将时间单位单位设置为TimeUnit.DAYS。这将导致它每天在那个时间运行。
The util.concurrent's ScheduledExecutorService allows you to pretty easily schedule tasks to run on a fixed delay. The signature of the schedule method is as follows:
You could find the difference between the current time and the time in which you wish to have it first run. Set the difference as the value for initialDelay. Set the period to 1 and the TimeUnit unit to TimeUnit.DAYS. That will cause it to run every day at that time.