System.nanoTime vs System.currentTimeMillis

1、System.currentTimeMillis定义

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

翻译一下就是:

返回当前时间,以毫秒为单位。注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,可能更大。例如,许多操作系统以数十毫秒为单位度量时间。
有关“计算机时间”和协调世界时(UTC)之间可能出现的细微差异的讨论,请参阅Date类的描述。
返回:
当前时间与1970年1月1日午夜之间的差值,以毫秒为单位。

补充说明

1)、从源码中可以看到,这个方式是一个native方法,该值由底层提供。
2)、该方法可以用来计算当前日期,当前星期几等,与Date的换算非常方便,JDK提供了相关的接口来换算。
3)、通过该方法获取的值的依据是当前系统的日期和时间,可以在系统设置中进行设置和修改。

2、System.nanoTime 的定义

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 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:
  
 long startTime = System.nanoTime();
 // ... the code being measured ...
 long estimatedTime = System.nanoTime() - startTime;
To compare two nanoTime values
  
 long t0 = System.nanoTime();
 ...
 long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.
Returns:
the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds
Since:1.5

翻译一下就是:

返回正在运行的Java虚拟机的高分辨率时间源的当前值,以纳秒为单位。
此方法只能用于测量运行时间,与任何其他系统或时钟时间概念无关。返回的值表示纳秒,因为某个固定但任意的起始时间(可能在将来,因此值可能为负数)。Java虚拟机实例中该方法的所有调用都使用相同的起源;其他虚拟机实例可能使用不同的起源。
此方法提供纳秒级的精度,但不一定提供纳秒级的分辨率(即值变化的频率)——除了分辨率至少与currentTimeMillis()一样好之外,不能保证分辨率。
由于数值溢出,持续时间超过大约292年(2的63次方纳秒)的连续调用之间的差异将不能正确计算运行时间。
只有当计算在同一个Java虚拟机实例中获得的两个这样的值之间的差时,此方法返回的值才有意义。
例如,要度量某些代码执行所需的时间:
  
long startTime = System.nanoTime();
/ /……正在测量的代码…
long estimatedTime = System.nanoTime() - startTime;
比较两个nanoTime值
  
long t0 = System.nanoTime();
…
long t1 = System.nanoTime();
应使用t1 - t0 <0,不是t1 <T0,因为存在数值溢出的可能性。
返回:
运行中的Java虚拟机的高分辨率时间源的当前值,以纳秒为单位
开始版本:1.5

补充说明

1)、从源码中可以看到,这个方式是一个native方法,该值由底层提供。
2)、该方法只是用来计算运行时间差的。
3)、该方法所基于的时间是随机的,但在同一个JVM中,不同的地方使用的原点时间是一样的。

对于使用t1-t0<0的判断,可以用如下的代码进行验证

public static void main(String[] args) throws Exception {
        int init=(int)Math.pow(2,31);
        int t0= init-20;
        int t1= -init-3;
        int t2 = init - 3;
        if (t1 - t0 > 0 || t2 - t0 > 0) {
            System.out.println("1---------"+(t1-t0));
        }else{
            System.out.println("2---------"+(t1-t0));
        }
    }

原理是 -7-7=1111 + 0111 = 0110 =6

3、当调整服务器时间的时候,currentTimeMillis是会受到影响的,有可能会导致后面获取的时间比前面的时间小 。

但是nanoTime 不是基于系统时间,同一个JVM中 原点时间 是一致的,所以调整服务器时间,后面时间还是比前面的时间大,但是需要采用B-A>0的方式来判断。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容