在JDK中,System类中有个nanoTime()的方法,可以获取JVM的机器时间.

/**

* Returns the current value of the running Java Virtual Machine's

* high-resolution time source, in nanoseconds.

*

*

This method can only be used to measure elapsed time and is

* not related to any other notion of system or wall-clock time.

* The value returned represents nanoseconds since some fixed but

* arbitrary origin time (perhaps in the future, so values

* may be negative). 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.

*

*

This method provides nanosecond precision, but not necessarily

* nanosecond resolution (that is, how frequently the value changes)

* - no guarantees are made except that the resolution is at least as

* good as that of {@link #currentTimeMillis()}.

*

*

Differences in successive calls that span greater than

* approximately 292 years (263 nanoseconds) will not

* correctly compute elapsed time due to numerical overflow.

*

*

The values returned by this method become meaningful only when

* the difference between two such values, obtained within the same

* instance of a Java virtual machine, is computed.

*

*

For example, to measure how long some code takes to execute:

*

 {@code

* long startTime = System.nanoTime();

* // ... the code being measured ...

* long estimatedTime = System.nanoTime() - startTime;}

*

*

To compare two nanoTime values

*

 {@code

* long t0 = System.nanoTime();

* ...

* long t1 = System.nanoTime();}

*

* one should use {@code t1 - t0 < 0}, not {@code t1 < t0},

* because of the possibility of numerical overflow.

*

* @return the current value of the running Java Virtual Machine's

* high-resolution time source, in nanoseconds

* @since 1.5

*/

public static native long nanoTime();

其中有一段描述是:

*

To compare two nanoTime values

*

 {@code

* long t0 = System.nanoTime();

* ...

* long t1 = System.nanoTime();}

*

* one should use {@code t1 - t0 < 0}, not {@code t1 < t0},

* because of the possibility of numerical overflow.

JDK表明比较两个nanoTime的时候,应该用t1 - t2 > 0的方式来比较,而不能用 t1 > t2的方式来比较,因为nanoTime在获取时有数值溢出的可能。

下一段是源于stackoverflow的解释:

The Nano time is not a 'real' time, it is just a counter that increments starting from some

unspecified number when some unspecified event occurs (maybe the computer is booted

up).

It will overflow, and become negative at some point. If your t0 is just before it overflows (i.e. very large positive), and your t1 is just after (very large negative number), then t1 < t0 (i.e. your conditions are wrong because t1 happened after t0).....

It will overflow, and become negative at some point. If your t0 is just before it overflows (i.e. very large positive), and your t1 is just after (very large negative number), then t1 < t0 (i.e. your conditions are wrong because t1 happened after t0).....

But, if you say t1 - t0 < 0, well, the magic is that a for the same overflow (undeflow) reasons (very large negative subtract a very large positive will underflow), the result will be the number of nanoseconds that t1 was after t0..... and will be right.

In this case, two wrongs really do make a right!

大致意思是:

Nano时间不是'真实'时间,它只是一个计数器,当某些未指定的事件发生时(可能是计算机启动),计数器从一些未指定的数字开始递增。

它会溢出,在某些时候变为负数。 如果你的t0恰好在它溢出之前(即非常大的正数),并且你的t1刚好在(非常大的负数)之后,则t1

但是,如果你说t1 - t0 < 0,那么神奇的是,对于相同的溢出(undeflow)原因(非常大的负数减去一个非常大的正数),结果将是t1的纳秒数在t0之后......并且是对的。

在这种情况下,两个错误确实是正确的!

nanotime java_为什么NanoTime不能直接比较大小相关推荐

  1. nanotime java_Java System nanoTime()方法

    Java System nanoTime()方法 java.lang.System.nanoTime() 方法返回最精确的可用系统计时器的当前值,以毫微秒为单位.返回的值表示从一些固定的,任意时间纳秒 ...

  2. 怎么控制table的大小java_如何查询Table占用空间的大小

    Oracle和DB2都支持分区表,但是我们得知道什么样的表适合使用分区表技术,Oracle的官方建议是超过2G的Table就要使用分区表.下面来总结一下,各DB中如何获得这些大小信息:Oracle 1 ...

  3. 计算 java_两种计算Java对象大小的方法(转)

    原文:http://blog.csdn.net/iter_zc/article/details/41822719 另一篇类似文章:http://www.cnblogs.com/magialmoon/p ...

  4. instrumentation java_使用Instrumentation计算java对象大小

    对象大小的计算 正如笔者看的这篇文章所描述的一样,当我们试图获取一个JVM中的对象实际占用的空间大小时.通常使用的方法是徒手计算,或者通过gc计算前后内存差来估计对象大小.而今天要介绍的方法--利用I ...

  5. 【Java基础】时间比较 currentTimeMillis 与 nanoTime

    时间比较 currentTimeMillis 与 nanoTime nanoTime() 返回的是 正在运行的 Java 虚拟机的高分辨率时间源的当前值,以纳秒为单位. currentTimeMill ...

  6. golang源码分析-启动过程概述

    golang源码分析-启动过程概述 golang语言作为根据CSP模型实现的一种强类型的语言,本文主要就是通过简单的实例来分析一下golang语言的启动流程,为深入了解与学习做铺垫. golang代码 ...

  7. Java队列集合的性能测试

    同时开10个线程存入和取出100万的数据,结论如下: DoubleBufferedQueue < ConcurrentLinkedQueue < ArrayBlockingQueue &l ...

  8. 深入理解GO语言:GC原理及源码分析

    Go 中的runtime 类似 Java的虚拟机,它负责管理包括内存分配.垃圾回收.栈处理.goroutine.channel.切片(slice).map 和反射(reflection)等.Go 的可 ...

  9. Java中STW (GC卡顿)

    在垃圾回收时,都会产生应用程序的停顿,停顿产生时,整个应用程序会被卡死,没有任何响应. java中Stop-The-World机制简称STW,是在执行垃圾收集算法时,Java应用程序的其他所有线程都被 ...

最新文章

  1. 揭秘深度学习成功的数学原因:从全局最优性到学习表征不变性
  2. 使用XML声明自定义Android UI元素
  3. 第三次学JAVA再学不好就吃翔(part110)--使用指定的码表读写字符
  4. 为什么“支付宝里没钱了”和“微信里没钱了”给人两种不同的感觉
  5. java api帮助文档_JAVA的Swagger界面丑、功能弱怎么破?
  6. nginx工作笔记004---配置https_ssl证书_视频服务器接口等
  7. hdu 2604 Queuing AC自动机构造递推式-矩阵-结果
  8. 解决QT编程出现 C2001错误
  9. karto探秘之open_karto 第五章 --- 栅格地图的生成
  10. java小游戏制作(maxfo)
  11. 【uart篇】synopsys uart vip配置使用
  12. 【JavaScript应用篇】--实现文字的特效
  13. 2021年中式烹调师(初级)模拟考试系统及中式烹调师(初级)实操考试视频
  14. React二级路由的实现
  15. android使用google gcm接收push消息需要注意的地方
  16. NTCUTTER BD-1002013年新包装通知
  17. 【细胞分割】原子力显微镜图像分析【含GUI Matlab源码 1371期】
  18. python位置参数ppt_argparse模块的位置参数和可选(必选)参数
  19. 调用支付jsapi缺少参数 total_fee
  20. 分享三套美团面试经历+简单答案

热门文章

  1. Glassfish JAVA容器中间件使用(咋个办呢 zgbn)
  2. horizon云桌面管理功能说明
  3. 结构图的分类--产品功能结构图、产品信息结构图、产品结构图
  4. Docker教程(Docker实战)
  5. Matlab实现图书管理系统
  6. 如何批量查询谷歌PR权重是多少?谷歌PR权重怎么批量查询
  7. 阿里员工离职换来一条血泪教训:以为当小白兔很安全,是你对职场最大的误解
  8. 1139:整理药名(C C++)
  9. pb9 日历控件(源码)
  10. JavaSE-Lambada