Will System.currentTimeMillis always return a value >= previous calls?

The short answer is no, System.currentTimeMillis() is not monotonic. It is based on system time, and hence can be subject to variation either way (forward or backward) in the case of clock adjustments (e.g. via NTP).

System.nanoTime() is monotonic, if and only if the underlying platform supports CLOCK_MONOTONIC — see the comments on Java bug report 6458294 for a good writeup on some circumstances where this is/isn’t true.

(And, as an additional anecdote, I have personally observed (several times) System.currentTimeMillis() run ‘backwards’, in the absence of clock adjustments, across threads — that is, a call to that method in one thread returned a lower value than a call in another thread, even though it occurred chronologically after it in ‘real-time’)

If you need a monotonic source, System.nanoTime() on a platform supporting monotonicity is your best option.

Leave a Comment