Java 中的闹钟
我想制作一个程序,使弹出窗口在未来的某个时间出现,例如。今晚 5:00。基本上,该程序是约会、会议等的提醒/通知系统。
我的第一直觉是创建一种“时钟侦听器”,它会每分钟左右检查一次计算机的时间,看看 currentTime == AlarmTime 是否。但是,我不知道这是否占用了太多资源,或者让您的程序不断执行此类操作是否只是一种不好的做法。另外,为了使闹钟准确,我认为它需要每秒检查一次,而不是每分钟检查一次(因为如果它不检查秒并且将在 5:00:xx 响铃,它可能会在 5:00:xx 响铃) :00:59,这对于某些人来说可能已经太晚了)。每秒检查一次时钟是否过多?
我的第二个想法是当程序开始运行时,计算距离闹钟设置为响铃还有多长时间(例如,五个小时后)。然后,等待五个小时,然后拉响警报。但后来我想,虽然不太可能,但用户可能会更改计算机的时间,然后闹钟会在错误的时间响起。所以这实际上不起作用。
我见过一些使用线程的解决方案,但我还不熟悉它们,所以我宁愿不使用它们。
我倾向于第一个解决方案,但我想确保它是高效的并且不会减慢其他程序的速度。也许我想太多了,检查时钟是一个微不足道的操作,但我只是想确保我没有做错任何事情。
I want to make a program that will make a pop-up appear at a certain time in the future, eg. 5:00 tonight. Basically, the program is a reminder/notification system for appointments, meetings, etc.
My first instinct was to create a sort of "Clock Listener" that would check the computer's time every minute or so and see if currentTime == alarmTime. However, I don't know if this takes up too much resources or if it is just a bad practice to have your program constantly doing things like that. Also, for the alarm to be accurate, I think it would need to check every second, rather than every minute (since if it isn't checking the seconds and will go off at 5:00:xx, it could go off at 5:00:59, which may be too late for some people's liking). Is checking the clock every second too much?
My second thought was when the program starts running, calculate how long it is until the alarm is set to go off (say, in five hours). Then, wait five hours, and then sound the alarm. But then I thought, though unlikely, it would be possible for the user to change the computer's time, and then the alarm would go off at the wrong time. So this doesn't really work.
I've seen some solutions that use threads, but I'm not familiar with those yet, so I'd rather not use them.
I'm leaning towards the first solution, but I want to make sure it's efficient and won't slow down other programs. Perhaps I'm overthinking it and checking the clock is a trivial operation, but I just wanted to make sure I'm not doing anything wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
睡眠解决方案非常简单,但是使用
java .util.Timer
并不难,它为您提供了一种清晰的方法来扩展多个警报等的实用程序。假设您要使用 Swing 来显示通知,请注意您的TimerTask
需要执行它的一些工作在 Swing 事件线程上。SwingUtilities.invokeAndWait(...)
将帮助您。The sleep solution is very straightforward, but using
java.util.Timer
is not much harder, and gives you a clear way to extend your utility for multiple alarms, etc. Assuming you are going to use Swing to display the notification, note that yourTimerTask
will need to perform some of its work on the Swing event thread.SwingUtilities.invokeAndWait(...)
will help you with that.第一个解决方案是可以的。醒来,查看时间,然后继续睡觉应该不成问题。如果需要,您可以每秒检查一次,但如果您只需要 1 分钟的分辨率,也许每 30 秒检查一次就足够了。
第二种方法存在您已经概述的问题。如果您只是在剩余时间内睡觉,并且用户更改了时间(或通过其他方式更改了时间,例如与时间服务器同步),则闹钟会在错误的时间响起。如果您可以注册某种钩子,以便在系统时间更改时回调您的程序,则可以避免这种情况,但在 Java 中您无法轻松做到这一点。
The first solution is OK. Waking up, checking the time, and going back to sleep should not be a problem at all. You can check every second if you want, but if you only need 1-minute resolution perhaps it is enough to check e.g. every 30 seconds.
The second approach has the problem you have outlined already. If you just go to sleep for the time remaining, and the user changes the time (or the time is changed by some other means, e.g. synchronisation with a time server), the alarm would go off at the wrong time. You could avoid this if you could register some sort of hook so that your program is called back when the system time changes, but you cannot easily do this in Java.