As documented here, 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. Thus we could get a value in time2 which is less than time1. Thus we would get a negative value in timeSpent.
Considering this case, isnt it that System.nanotime is pretty much useless for now?
Edit: I know that changing the system time doesnt affect nanotime. that is not the problem i describe above. The problem is that each cpu will keep a different counter since it was turned on. This counter can be lower on the 2nd cpu compared to the first cpu. Since the thread can be scheduled by the OS to the 2nd cpu after getting time1, the value of timeSpent may be incorrect and even negative.