Class Watch

java.lang.Object
fc.util.Watch
Direct Known Subclasses:
NanoWatch

public class Watch extends Object
Allows measuring the start and stop time and (hence the elapsed time) of some event. ("event" meaning something of interest, for example, a method call). Can be started/stopped repeatedly. (cumulative time across all such start/stops are available via the cumulativeTime method).

All times are in milliseconds.

Thread Safety: This class is not threadsafe and it's method do not acquire any locks to reduce any time skew due to lock acquisition. Multiple threads should use separate Watch objects or alternatively, higher level synchronization.

Note: This class used to be called "Timer" but changed to Watch to avoid an annoying name conflict with java.util.Timer

Version:
1.0, 10/19/2001
See Also:
  • Constructor Details

  • Method Details

    • start

      public Watch start()
      Start measuring time. Call this method just before calling the method/code to be instrumented. This method does not reset the Watch, so the reset method should be called before calling this method again. Returns this so we can say:
      Watch w = new Watch().start()
    • stop

      public void stop()
      Stop measuring the time
    • time

      public long time()
      Returns the time elapsed since the Watch was started. If the watch was started and stopped, returns the time (in milliseconds) between the start/stop interval.
      Throws:
      RuntimeException - if the watch was never started before calling this method.
    • getTime

      public long getTime()
      This method is an alias for time() method.
    • timeInSeconds

      public long timeInSeconds()
      Useful for showing the elapsed time in seconds. Intervals between, (0 - 499 milliseconds are rounded down and 500 - 999 milliseconds are rounded up).
    • getTimeInSeconds

      public long getTimeInSeconds()
      This method is an alias for timeInSeconds() method.
    • seconds

      public long seconds()
      This method is an alias for timeInSeconds() method.
    • elapsed

      public long elapsed()
      This method is an alias for timeInSeconds() method.
    • timeSeconds

      public long timeSeconds()
      This method is an alias for timeInSeconds() method.
    • timeMillis

      public double timeMillis()
      This method is an alias for getTimeInMillis() method.
    • getTimeInMillis

      public double getTimeInMillis()
      Useful in NanoWatch and other subclasses. Not really useful here, returns the same value as getTime()/time() functions instead.

      This method is defined here so all watches have a common interface and any instance can be bound to a variable of type Watch.

    • cumulativeTime

      public long cumulativeTime()
      Returns the total time recorded by this Watch (across several starts/stops)
    • reset

      public void reset()
      Reset all values to zero. This method should be called before this object is used again. Does not restart the timer, call start again when start the timer.
    • restart

      public void restart()
      Reset all values to zero and restarts the timer.
    • isRunning

      public boolean isRunning()
      Is the Watch currently running ?
    • toString

      public String toString()
      Describes the current state of this watch. The exact details of said description are unspecified and subject to change.
      Overrides:
      toString in class Object
    • main

      public static void main(String[] args)