Class TimeTracker


  • public class TimeTracker
    extends java.lang.Object
    Simple class to track elapsed time. Initialised with any time units, returns remaining time (in nanoseconds) on request.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static TimeTracker ZERO
      Public tracker that is permanently timed out.
    • Constructor Summary

      Constructors 
      Constructor Description
      TimeTracker()
      Initialise tracker with maximum duration -- effectively an infinite time.
      TimeTracker​(long timeout, java.util.concurrent.TimeUnit unit)
      Initialise tracker with duration supplied.
      TimeTracker​(TimeTracker timeTracker)
      Initialise tracker with duration of old tracker (regardless that the old one has timed out).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long remainingMillis()
      Return the remaining time to go in milliseconds, or zero if there is no more.
      long remainingNanos()
      Return the remaining time to go in nanoseconds, or zero if there is no more.
      boolean timedOut()  
      void timedWait​(java.lang.Object lock)
      A TimeUnit.timedWait(java.lang.Object, long) utility which uses the TimeTracker state.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ZERO

        public static final TimeTracker ZERO
        Public tracker that is permanently timed out.
    • Constructor Detail

      • TimeTracker

        public TimeTracker​(long timeout,
                           java.util.concurrent.TimeUnit unit)
        Initialise tracker with duration supplied.
        Parameters:
        timeout - - duration of tracker
        unit - - units that timeout is in, e.g. TimeUnit.MILLISECONDS.
      • TimeTracker

        public TimeTracker​(TimeTracker timeTracker)
        Initialise tracker with duration of old tracker (regardless that the old one has timed out).
        Parameters:
        timeTracker - - a TimeTracker instance
      • TimeTracker

        public TimeTracker()
        Initialise tracker with maximum duration -- effectively an infinite time.
    • Method Detail

      • remainingNanos

        public long remainingNanos()
        Return the remaining time to go in nanoseconds, or zero if there is no more.
        Returns:
        remaining time (in nanoseconds) - 0 means time has run out
      • remainingMillis

        public long remainingMillis()
        Return the remaining time to go in milliseconds, or zero if there is no more.
        Returns:
        remaining time (in milliseconds) - 0 means time has run out
      • timedWait

        public void timedWait​(java.lang.Object lock)
                       throws java.lang.InterruptedException
        A TimeUnit.timedWait(java.lang.Object, long) utility which uses the TimeTracker state.

        Used in synchronized(lock){} blocks that want to timeout based upon a time tracker object.

        Parameters:
        lock - - object to lock on
        Throws:
        java.lang.InterruptedException - if the thread is interrupted while waiting.
      • timedOut

        public boolean timedOut()
        Returns:
        true if time has run out, false otherwise
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object