expiringmap icon indicating copy to clipboard operation
expiringmap copied to clipboard

Incorrect usage of System.nanoTime()

Open ben-manes opened this issue 7 years ago • 0 comments

ExpiringMap compares nanosecond times as time <= now. Both sides are from direct reads of System.nanoTime(). Unfortunately this violates the contract of that method,

* The value returned represents nanoseconds since some fixed but
* arbitrary <i>origin</i> time (perhaps in the future, so values
* may be negative). 
* ...
* <p>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.
* ...
* one should use {@code t1 - t0 < 0}, not {@code t1 < t0},
* because of the possibility of numerical overflow.

This behavior is not intuitive and causes these simple oversights. However, failing to abide by that directive can lead to incorrect conclusions.

ben-manes avatar Jul 11 '17 05:07 ben-manes