uptimeMillis() 多久重置一次,是否会影响 Handler.postAtTime

发布于 2024-10-10 00:31:05 字数 451 浏览 10 评论 0原文

方法 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 技术交流群。

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

发布评论

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

评论(3

得不到的就毁灭 2024-10-17 00:31:05

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.

死开点丶别碍眼 2024-10-17 00:31:05

如果您碰巧在包装时调用了 uptimeMillis,那么它会影响您的 postAtTime 调用。

Java 中的有符号 long 的范围为:

-9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 (~9.2E18)

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,223,372,036,854,775,807 to 9,223,372,036,854,775,807 (~9.2E18)

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

This clock is guaranteed to be
monotonic . . .

Then soon after they say that uptimeMillis will be reset due to variable wrapping - the exact opposite of a monotonic clock!

满天都是小星星 2024-10-17 00:31:05

我将它用于一项服务,但从未看到它重置。我真的认为不会。
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 (since uptimeMillis() will not update). If that's an issue, then I would use some other method.

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