See the Stopwatch(Ticker) constructor. Supplying your own Ticker allows you to produce a Stopwatch that yields predictable results for testing purposes.
What are the advantages of Guava Stopwatch class?
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:
- An alternate time source can be substituted, for testing or performance reasons.
- 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?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow