I am currently programming a multithreaded program with Java. At some points different threads log their actions and I'm using nanoTime
for this. Every thread has it's own log file and at the end I merge them and sort them based on their time (nanoTime
) to see what happened. The thing is that I have erroneous behaviour similar to this where x
is a volatile
variable:
// logged by thead1
x = true // done at time 0000, time computed & logged after x = true was done
// no events in between
// logged by thread2
read x // reads false while time before reading is 0001
So it seems to me that nanoTime
doesn't really time things correctly.
In nanoTime
documentation it's written:
The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.
Is it perhaps possible for threads created by the same process to be executed in different JVMs? This would explain the erroneous behaviour of nanoTime
but still it doesn't make too much sense. Any ideas?