3

This question is not intended as an attack upon System.nanoTime(). I realize it is a surprisingly tricky method to use correctly.

What are some ways to deal with System.nanoTime() returning the same value between calls? Example: Multiple threads call System.nanoTime() and get the same value.

I am surprised how often I see this happen in my code base when running tests on Windows. We use nanoTime to sort events that arrive across multiple threads. Perhaps this is only a Windows issue and the Linux monotonic clock is more granular.

References:

Community
  • 1
  • 1
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • @ElliottFrisch - "... We use nanoTime to sort events that arrive across multiple threads ..." Like to see you do that with a UUID. – Dawood ibn Kareem Dec 22 '14 at 04:05
  • 3
    why not use an atomic counter to sort the threads instead? – Claudio Corsi Dec 22 '14 at 04:06
  • What exactly is the question? Are you looking for an alternative to nanoTime? Or a way to make nanoTime work for you? It seems like perhaps your model of the world (that only one thing happens during one tick) just doesn't fit with nanoTime. In reality, if you have multiple threads you cannot (without synchronization) produce a strict time-order for those events. – Rob Dec 22 '14 at 04:07
  • How big/long is a "tick"? I am Intel i8 on Windows. Are there docs for this per OS / hardware? – kevinarpe Dec 22 '14 at 04:09
  • @ClaudioCorsi: That is an interesting idea. – kevinarpe Dec 22 '14 at 04:10
  • @kevinarpe - Claudio's idea is the correct solution. – Dawood ibn Kareem Dec 22 '14 at 04:10

1 Answers1

4

To explain why you're getting the same value, read the documentation a bit more closely:

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

Your computer may not have enough clock resolution, so there could be a good chunk of time where nanoTime will return the same number.

As for your question

What are some ways to deal with System.nanoTime() returning the same value between calls?

I would suggest using some sort of an atomic counter, as Claudio Corsi suggests.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • A new computer probably won't help. Does any operating system actually provide a nanosecond-accurate `nanoTime()`? – Dawood ibn Kareem Dec 22 '14 at 04:08
  • `get a new computer` -- nice. I have an 8-core Intel CPU; I doubt I need a new computer. I am guessing this is a limitation on Windows. I don't actually need nanosecond resolution -- but I am surprised how often the value is the same (a few times per day). – kevinarpe Dec 22 '14 at 04:09
  • 1
    Can I have your old one after you replace it? – Dawood ibn Kareem Dec 22 '14 at 04:09
  • @DavidWallace Most standard operating systems probably won't provide nanosecond resolution, but there are probably some custom ones out there that will. It was a bit tongue in cheek. I had missed the part of the original question where he states his need, so I edited that suggestion out. – Jeffrey Dec 22 '14 at 04:09
  • @kevinarpe The number of cores on your computer has little to do with clock resolution. It's either a hardware limitation or an OS limitation. – Jeffrey Dec 22 '14 at 04:11
  • 1
    @kevinarpe If you are willing to use JNI you could make calls to access the higher precision timers on Windows. http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408%28v=vs.85%29.aspx – Michael Petch Dec 22 '14 at 08:35