Question

In the Google Guava library, there is a Stopwatch class, and its documentation states this -

An object that measures elapsed time in nanoseconds. It is useful to measure elapsed time using this class instead of direct calls to System.nanoTime() for a few reasons:

  1. An alternate time source can be substituted, for testing or performance reasons.
  2. As documented by nanoTime, the value returned has no absolute meaning, and can only be interpreted as relative to another timestamp returned by nanoTime at a different time. Stopwatch is a more effective abstraction because it exposes only these relative values, not the absolute ones.

Question - What is this 'alternate time source' they refer to? And does JRE/JVM provide this option too?

Was it helpful?

Solution

See the Stopwatch(Ticker) constructor. Supplying your own Ticker allows you to produce a Stopwatch that yields predictable results for testing purposes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top