System.nanoTime Javadoc 中引用的任意点是什么? [英] What is the arbitrary point that is referenced in System.nanoTime Javadoc?

查看:31
本文介绍了System.nanoTime Javadoc 中引用的任意点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 System.nanoTime() 与时代无关(尽管与 System.currentTimeMillis() 相关).

I understand that System.nanoTime() has no relation to the epoch (which is related to System.currentTimeMillis() though).

从 Javadoc 中,它指出从 System.nanoTime() 返回的值是从某个任意时间点(甚至可能是未来)的偏移量.

From the Javadoc it states that the value returned from System.nanoTime() is an offset from some arbitrary point in time (may even be the future).

关于这个

  1. 这个时间点是否在同一平台上的 JVM 实例中保持不变?
  2. 这个时间点是否在跨平台的 JVM 实例中保持不变?

假设我运行我的应用程序并将 System.nanoTime() 的值保存到一个文件中.接下来,我重新执行我的应用程序并计算文件中的值与从 System.nanoTime() 返回的新值之间的差异.

If suppose I run my application and persist the value of System.nanoTime() into a file. Next I reexecute my application and compute the difference between the value in the file and the new value returned from System.nanoTime().

那么假设差异是执行之间的实际时间量是否安全?

Then is it safe to assume that the difference is the actual amount of time between the executions?

如果任意时间点保持不变,那就是.

It would be if the arbitrary point in time would remain the same.

但是我不确定这是不是行为

However I am not sure is that's the behavior

推荐答案

不,这是任意的.不要依赖它是任何给定的值,因为它没有被指定以这种方式工作.正如在Javadoc中所说:

No, it's arbitrary. Don't rely upon it being any given value, as it is not specified to work in that way. As it says in the Javadoc:

Java 虚拟机实例中所有调用此方法均使用同一个源;其他虚拟机实例可能使用不同的来源.

The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

System.nanoTime() 用于测量单个 JVM 上的已用时间,因此您只需关心两次调用之间其值的差异.因此,不需要知道偏移量.

System.nanoTime() is used to measure elapsed time on a single JVM, so you only care about the difference in its value between two invocations. Thus, knowing the offset isn't necessary.

这篇关于System.nanoTime Javadoc 中引用的任意点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆