Java定时器类,每天在固定时间触发

发布于 2024-09-18 09:00:51 字数 119 浏览 4 评论 0原文

我想编写一个简单的计时器类,该类在应用程序运行时触发并在每天的固定时间进行方法调用。

我不想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-09-25 09:00:51

Why not use java.util.Timer and java.util.TimerTask?

彡翼 2024-09-25 09:00:51

util.concurrent 的 ScheduledExecutorService 允许您可以轻松地安排任务以固定的延迟运行。 Schedule 方法的签名如下:

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 

您可以找到当前时间与您希望首次运行它的时间之间的差异。将差值设置为 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:

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文