6

I have a thread which pushes data into a queue and another thread which reads data from queue and processes it. I would like to check how long the data sits in the queue before getting processed.

I added a time parameter (calculated using System.nanoTime()) in the data before being pushed by the first thread. Once the second thread processes it, it will calculate System.nanoTime() and find the difference from the previous time set in the data.

Would this work properly? I am asking this because I am seeing negative difference in the logs.

UPDATE

I would like to clarify that, the start time is put by a process in a different machine and the difference is calculated in a different machine.

cppcoder
  • 22,227
  • 6
  • 56
  • 81

1 Answers1

5

I have used System.nanoTime() between threads and processes. On a single machine it is both global and monotonically increasing (with the exception of multi-socket Windows XP)

If you see a negative difference, most likely it is a bug in your code.

You can see nanoTime() between machines, but you have to adjust for the difference and drift between clocks. (And you can get large very negative results if you don't do this correction)

the start time is put by a process in a different machine and the difference is calculated in a different machine.

Between machines you need to either

  • use System.currentTimjeMillis() which is usually accurate to a milli-second with NTP.
  • use System.nanoTime() for a round trip, i.e. send a message from A ro B and another back to A. The half round trip time can be estimated from this.
  • use System.nanoTime() not for the elapse time, but to detect jitter. This assumes that most of the time the delay is acceptable (say 10 - 100 micro-seconds) but sometimes it is much higher than normal. I use this class to help detect jitter. RunningMinimum

If you are only interested in multi-milli-second delays I would use currentTimeMillis()

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I think this is exactly what I am facing. Could you please explain how to adjust the difference across machines? – cppcoder Oct 05 '16 at 18:15
  • I don't think there's any point in trying to make `nanoTime` work across machines and even between processes it only works by accident (isn't required to work). – Marko Topolnik Oct 05 '16 at 18:18
  • Is there any other mechanism to find the elapsed time across machines? – cppcoder Oct 05 '16 at 18:20
  • @cppcoder you can't find the elapsed time unless you use currentTimeMillis or you time the round trip. i.e. you send nanoTime to a machine which sends it back. What I use it for is to detect jitter/delayed messages and for that nanoTime gets around 10 micro-seconds accuracy which is enough for me. i.e. you don't know what the delay is but you not much the delay is higher than normal. The class is RunningMinimum https://github.com/OpenHFT/Chronicle-Core/blob/master/src/main/java/net/openhft/chronicle/core/time/RunningMinimum.java – Peter Lawrey Oct 05 '16 at 18:24