Class Timer


  • public class Timer
    extends java.lang.Object
    Timer for code that gets run.

    Sample usage:

    
       Timer myTimer = ...
       myTimer.time(this::doSomething);
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Timer.Factory  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long getElapsedTime()
      Gets the time elapsed between a call to started() and a call to stop(), in microseconds.
      void reset()
      Resets this timer.
      void started()
      Starts the timer.
      void stop()
      Stops the timer.
      void time​(java.lang.Runnable target)
      Times the given runnable target.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • started

        public void started()
        Starts the timer. Call stop() to stop timing.
        Throws:
        java.lang.IllegalStateException - if this a call to this method is preceded by another call to started()
      • stop

        public void stop()
        Stops the timer. This will post a TimerEvent containing the elapsed time and analysis to the event bus.
        Throws:
        java.lang.IllegalStateException - if this a call to this method is not preceded by a call to started().
      • reset

        public void reset()
        Resets this timer.
      • time

        public void time​(java.lang.Runnable target)
        Times the given runnable target. This is safe, even if the target throws an exception.

        Sample usage:

        
           Timer myTimer = ...
           myTimer.time(this::doSomething);
         
        Parameters:
        target - the code to time
        Throws:
        java.lang.IllegalStateException - if this timer is already timing something
      • getElapsedTime

        public long getElapsedTime()
        Gets the time elapsed between a call to started() and a call to stop(), in microseconds.