java - 黑白 Clock.systemUTC() 和 System.currentTimeMillis() 的区别

标签 java

Clock.systemUTC() 文档说此方法可能使用 System.currentTimeMillis() 或更高分辨率的时钟(如果可用)。那么System.currentTimeMillis()使用什么时钟呢?这两个值的粒度有区别吗?

最佳答案

Can there be a difference in the granularity of these two values?

Clock类有 2 个用于检索当前时间的方法:

因为 instant() 返回 Instant ,它可以以纳秒的精度表示时间,答案是显而易见的。

答案:是的。


What clock does System.currentTimeMillis() use then?

如果你查看Clock.systemUTC()的源代码,你会发现它使用了一个内部SystemClock类。在 millis() 方法的注释中,它说(引用 Java 15):

System.currentTimeMillis() and VM.getNanoTimeAdjustment(offset) use the same time source - System.currentTimeMillis() simply limits the resolution to milliseconds.
So we take the faster path and call System.currentTimeMillis() directly - in order to avoid the performance penalty of VM.getNanoTimeAdjustment(offset) which is less efficient.

答案: System.currentTimeMillis()Clock.instant() 使用相同的时间源。

关于java - 黑白 Clock.systemUTC() 和 System.currentTimeMillis() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65754746/

相关文章:

java - 从 GIT 导入项目时找不到项目

java - 从 Assets 文件夹加载图像

java - 发送电子邮件至 Facebook

java - 2 名玩家井字游戏

java - Java 会自动异步调用这些方法吗?

java - 如何使用Alfresco OperationContext过滤掉文档?

java - 简单的 HTML 无法从 servlet (java) + mysql 获取 JSON

java - Android 上 .flatMap() 内的奇怪调度程序行为

java - 在 Firefox 31 和 Selenium 2.42.2 上查找 <select> 元素非常慢

java - 使用 GUI 时暂停程序