uptimeMillis() 多久重置一次,是否会影响 Handler.postAtTime
方法 uptimeMillis 的描述如下:
返回自启动以来的毫秒数,而不是 计算深度睡眠的时间。 注意:该值可能会重置 偶尔(之前 否则环绕)。
这种情况发生的频率以及(更重要的是)它是否会影响应由 Handler.postAtTime?
The description for the method uptimeMillis says:
Returns milliseconds since boot, not
counting time spent in deep sleep.
Note: This value may get reset
occasionally (before it would
otherwise wrap around).
How often might this happen and (more importantly) will it affect runnables that should be executed by Handler.postAtTime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
uptimeMillis 调用以 systemTime() 为基础,在 Linux 系统上,该调用会变成clock_gettime(CLOCK_MONOTONIC, struct timespec *)。
struct timespec 在 time_t 中保存秒数,它看起来是一个 32 位值。如果它开始计数接近于零,那么当它结束时你将不可能活着。
如果您需要更具体的详细信息,您应该研究 Linux 内核中的clock_gettime(CLOCK_MONOTONIC) 的行为。
The uptimeMillis call grounds out in systemTime(), which on a Linux system turns into clock_gettime(CLOCK_MONOTONIC, struct timespec *).
The struct timespec holds seconds in a time_t, which appears to be a 32-bit value. If it starts counting near zero, you will not likely be alive when it wraps.
If you need more specific details, you should investigate the behavior of clock_gettime(CLOCK_MONOTONIC) in the Linux kernel.
如果您碰巧在包装时调用了 uptimeMillis,那么它会影响您的 postAtTime 调用。
Java 中的有符号 long 的范围为:
9.2E18 毫秒,即 292,277,266 年。如果你正在研究太空探测器,你可能需要考虑到这一点,否则你可能会认为它不会在你的一生中包裹起来。
对我来说最重要的是 Android 文档 uptimeMillis 索赔
然后不久他们说 uptimeMillis 将由于变量包装而重置 - 与单调时钟完全相反!
If you happened to call uptimeMillis right when it wrapped, then yes it would affect your postAtTime call.
A signed long in Java has the range:
9.2E18 milliseconds is 292,277,266 years. If you are working on a space probe, you probably want to take this into consideration, otherwise you can probably get away with assuming it won't wrap in your lifetime.
The kicker for me is that the Android documentation for uptimeMillis claims
Then soon after they say that uptimeMillis will be reset due to variable wrapping - the exact opposite of a monotonic clock!
我将它用于一项服务,但从未看到它重置。我真的认为不会。
postAtTime()
的问题是它不会在睡眠期间被调用(因为uptimeMillis()
不会更新)。如果这是一个问题,那么我会使用其他方法。I was using it for a service and did not ever see it resetting. I would really assume it won't.
The problem with
postAtTime()
is that it will not be called during sleep (sinceuptimeMillis()
will not update). If that's an issue, then I would use some other method.