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.
Depending on the system, it can take more than 100 cpu cycles to execute. Not thread safe.
nanoTime() is a great function, but one thing it's not: accurate to the nanosecond. The accuracy of your measurement varies widely depending on your operation system, on your hardware and on your Java version. As a rule of thumb, you can expect microsecond resolution (and a lot better on some systems).
nanoTime() method returns the current value of the most precise available system timer, in nanoseconds. The value returned represents nanoseconds since some fixed but arbitrary time (in the future, so values may be negative) and provides nanosecond precision, but not necessarily nanosecond accuracy.
nanoTime() , the value, when the input data-set is large enough, turns negative.
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
If you are only interested in multi-milli-second delays I would use currentTimeMillis()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With