Class Timer


  • public final class Timer
    extends Object
    This class represents a timer like a stop watch. It can be started and stopped several times. It measures the sum, the average, the minimum, the maximum and the number of those intervals. This class is similar to Stopwatch but has more features.

    This class is not thread-safe and may be used only from within a single thread.

    • Constructor Detail

      • Timer

        public Timer()
        Create a fresh timer in the not-running state.
    • Method Detail

      • start

        public void start()
        Start the timer. May be called only if the timer is currently not running.
      • stop

        public void stop()
        Stop the timer. May be called only if the timer is currently running.
      • stopIfRunning

        public void stopIfRunning()
      • isRunning

        public boolean isRunning()
        Check if the timer is running. Contrary to all other methods of this class, this method is thread-safe. This means it can be safely run from another thread.
      • getSumTime

        public TimeSpan getSumTime()
        Return the sum of all intervals. If timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getMaxTime

        public TimeSpan getMaxTime()
        Return the maximal time of all intervals. If timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • getMinTime

        public TimeSpan getMinTime()
        Return the minimal time of all intervals. If the timer is running, the current interval is not considered. If the timer was never started, this method returns 0.
      • getNumberOfIntervals

        public int getNumberOfIntervals()
        Return the number of intervals. If timer is running, the current interval is also counted. If the timer was never started, this method returns 0.
      • getLengthOfLastInterval

        public TimeSpan getLengthOfLastInterval()
        Return the length of the last measured interval. If the timer is running, this is the time from the start of the current interval up to now. If the timer was never started, this method returns 0.
      • getAvgTime

        public TimeSpan getAvgTime()
        Return the average of all intervals. If timer is running, the current interval is also counted (up to the current time). If the timer was never started, this method returns 0.
      • toString

        public String toString()
        Return a String with a default representation of the the sum of the times of all intervals. For printing other times, or with a specific unit, use the appropriate getter and call TimeSpan.formatAs(java.util.concurrent.TimeUnit). The format and the content of the String returned by this method is not guaranteed to be the same in future versions of this code.
        Overrides:
        toString in class Object
      • prettyFormat

        public String prettyFormat()
        Syntax sugar method: pretty-format the timer output into a string in seconds.