nanotime

Java 8 Instant.now() with nanosecond resolution?

冷暖自知 提交于 2019-11-26 06:43:35
问题 Java 8\'s java.time.Instant stores in \"nanosecond resolution\", but using Instant.now() only provides millisecond resolution... Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.getNano()); Result... 2013-12-19T18:22:39.639Z 639000000 How can I get an Instant whose value is \'now\', but with nanosecond resolution? 回答1: While default Java8 clock does not provide nanoseconds resolution, you can combine it with Java ability to measure time differences with

Is System.nanoTime() completely useless?

青春壹個敷衍的年華 提交于 2019-11-26 00:23:50
问题 As documented in the blog post Beware of System.nanoTime() in Java , on x86 systems, Java\'s System.nanoTime() returns the time value using a CPU specific counter. Now consider the following case I use to measure time of a call: long time1= System.nanoTime(); foo(); long time2 = System.nanoTime(); long timeSpent = time2-time1; Now in a multi-core system, it could be that after measuring time1, the thread is scheduled to a different processor whose counter is less than that of the previous CPU