Class HeartbeatScheduler


  • @ThreadSafe
    public final class HeartbeatScheduler
    extends java.lang.Object
    This class can be used for controlling heartbeat execution of a thread. In particular, the await(String) method can be used to wait for a thread to start waiting to be scheduled and the schedule(String) method can be used to schedule a thread that is waiting to be scheduled. The contract of this class is that the schedule(String) method should only be called for threads that are in fact waiting to be scheduled. If this contract is not met, concurrent execution of a thread and the heartbeat scheduler logic to schedule the thread can lead to a deadlock. For an example of how to use the HeartbeatScheduler, see unit test of HeartbeatThread.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addTimer​(ScheduledTimer timer)  
      static void await​(java.lang.String name)
      Waits for the given thread to be ready to be scheduled.
      static void await​(java.lang.String name, long time, java.util.concurrent.TimeUnit unit)
      Waits until the given thread can be executed, throwing an unchecked exception of the given timeout expires.
      static void clearTimer​(java.lang.String name)
      Removes a timer name from the scheduler if it exists.
      static void execute​(java.lang.String name)
      Convenience method for executing a heartbeat and waiting for it to complete.
      static java.util.Set<java.lang.String> getThreadNames()  
      static void removeTimer​(ScheduledTimer timer)
      Removes a timer from the scheduler.
      static void schedule​(java.lang.String threadName)
      Schedules execution of a heartbeat for the given thread.
      • Methods inherited from class java.lang.Object

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

      • addTimer

        public static void addTimer​(ScheduledTimer timer)
        Parameters:
        timer - a timer to add to the scheduler
      • clearTimer

        public static void clearTimer​(java.lang.String name)
        Removes a timer name from the scheduler if it exists.
        Parameters:
        name - the name to clear
      • removeTimer

        public static void removeTimer​(ScheduledTimer timer)
        Removes a timer from the scheduler. This method will fail if the timer is not in the scheduler.
        Parameters:
        timer - the timer to remove
      • getThreadNames

        public static java.util.Set<java.lang.String> getThreadNames()
        Returns:
        the set of threads present in the scheduler
      • schedule

        public static void schedule​(java.lang.String threadName)
        Schedules execution of a heartbeat for the given thread.
        Parameters:
        threadName - a name of the thread for which heartbeat is to be executed
      • await

        public static void await​(java.lang.String name)
                          throws java.lang.InterruptedException
        Waits for the given thread to be ready to be scheduled.
        Parameters:
        name - a name of the thread to wait for
        Throws:
        java.lang.InterruptedException - if the waiting thread is interrupted
      • await

        public static void await​(java.lang.String name,
                                 long time,
                                 java.util.concurrent.TimeUnit unit)
                          throws java.lang.InterruptedException
        Waits until the given thread can be executed, throwing an unchecked exception of the given timeout expires.
        Parameters:
        name - a name of the thread to wait for
        time - the maximum time to wait
        unit - the time unit of the time argument
        Throws:
        java.lang.InterruptedException - if the waiting thread is interrupted
      • execute

        public static void execute​(java.lang.String name)
                            throws java.lang.InterruptedException
        Convenience method for executing a heartbeat and waiting for it to complete.
        Parameters:
        name - the name of the heartbeat to execute
        Throws:
        java.lang.InterruptedException - if the waiting thread is interrupted