System.currentTimeMillis vs System.nanoTime

后端 未结 10 1350
后悔当初
后悔当初 2020-11-21 22:56

Accuracy Vs. Precision

What I would like to know is whether I should use System.currentTimeMillis() or System.nanoTime()<

10条回答
  •  深忆病人
    2020-11-21 23:10

    System.currentTimeMillis() is not safe for elapsed time because this method is sensitive to the system realtime clock changes of the system. You should use System.nanoTime. Please refer to Java System help:

    About nanoTime method:

    .. 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()..

    If you use System.currentTimeMillis() your elapsed time can be negative (Back <-- to the future)

提交回复
热议问题