如何使用 System.currentTimeMillis() 管理剩余时间
例如,我使用此代码来检查用户是否可以执行某些操作。因此用户每 5 秒只能执行一项操作。
if((System.currentTimeMillis() - lastTime) > 5000)
{
// Message: Ok, you can do action now.
}else{
// Message: Have to wait 5 seconds to do action.
return;
}
lastTime = System.currentTimeMillis();
但众所周知,System.currentTimeMillis()
返回一个 long,并且该 long 可以不断增加,直到变为负值。
我的代码应该运行在需要超过 1 个月的服务器上正常运行时间。所以我担心在某些时候 System.currentTimeMillis() 会返回负值,而我的代码将始终告诉用户他需要等待 5 秒或相反。
我真的很难专注于这段代码并修复它,所以我问你们是否有关于如何修复这个问题并使我的代码 100% 安全的提示。
I use for example this code to check if the user can do some action. So the user can only do one action each 5 seconds.
if((System.currentTimeMillis() - lastTime) > 5000)
{
// Message: Ok, you can do action now.
}else{
// Message: Have to wait 5 seconds to do action.
return;
}
lastTime = System.currentTimeMillis();
But as we all know, System.currentTimeMillis()
returns a long, and that long can keep increasing until it turns negativ..
My code should run on a server that need to have more than 1 month of uptime. So I'm afraid at some point System.currentTimeMillis()
will return a negativ value and my code will always tell the user that he need to wait 5 seconds or the opposite.
I'm having real hard time to concentrate on this piece of code and fix it, so I'm asking you guys if you have a tip on how to fix this problem and make my code 100% safe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
别担心。
你知道这是谁的问题吗?
需要在 Sun Aug 17 03:12:55 GMT-04:00 更新它的人292278994。
Don't worry about it.
You know whos problem it is?
The guy who will need to update it on Sun Aug 17 03:12:55 GMT-04:00 292278994.
以毫秒为单位的 long 可以代表 292 277 266 年。我不确定这是您需要担心的事情。
A long in milliseconds can represent 292 277 266 years. I'm not sure this is the kind of thing you need to be worried about.
根据此线程,它将在292278994年溢出。我会说时间很充裕:)
According to this thread, it will overflow in year 292278994. I will say it is plenty of time:)
正如大家所说,不用担心,但为了将来的参考,也许您更喜欢使用 Joda-Time 提出这样的问题。
As everyone has said don't worry about it but for future reference maybe you'd prefer to use Joda-Time to ask this kind of question.
System.currentTimeMillis() 返回当前时间和 UTC 时间 1970 年 1 月 1 日午夜之间的时间(以毫秒为单位)。可以表示为 long 的最大最大值是 9,223,372,036,854,775,807,如果我的计算正确(long max / (1000 * 3600 * 24 * 365)),可能会超过 292471208年。如果你的程序能够存活那么久,让那些多年后出生的人像我们为千年虫所做的那样担心它。
System.currentTimeMillis() returns the time in milliseconds, between the current time and midnight, January 1, 1970 UTC. With the largest maximum value that can be represented as a long is 9,223,372,036,854,775,807, if my calculation is right (long max / (1000 * 3600 * 24 * 365)), that could go up to more than 292471208 years. If your program can survive that long, let someone who will be born that many years later worry about it like we did for Y2K.
尽管正如其他人所说,它溢出的时间是遥远的未来。这甚至都不是问题,因为你取的是两次的差值。例如,假设您采用年份 292,278,994 和年份 292,278,995(这似乎是负数),差异只有 1 年(正数),例如,如果您采取
System.nanoTime() 可能会发生这种情况,因为它不会没有定义的开始时间并且滴答声快了一百万倍。然而,只要考虑时差,只要相差小于 292 年,负数或正数都没有关系。
因此,在回答您的问题时,即使在 292,278,994 年之后,除非应用程序在调用 System.currentTimeMillis() 之间运行了超过 292,278,994 年,否则您不会遇到问题!
Even though the time it will overflow is far, far into the future as others have stated. It won't even be a problem then because you are taking the difference of two times. e.g. say you take the year 292,278,994 and the year 292,278,995 (which would appear to be negative), the difference is only 1 year (a positive number) e.g. if you take
This sort of this could happen with System.nanoTime() as it doesn't have a defined starting time and ticks one million time faster. However as long as you take the time difference, it doesn't matter if one is negative or positive provided they are less than 292 years apart.
So in answer to your question, even after the year 292,278,994 you won't have a problem until the application have been running for more than 292,278,994 years between calls to System.currentTimeMillis() !