Class ZThread

java.lang.Object
org.spiderwiz.zutils.ZThread
All Implemented Interfaces:
Runnable

public abstract class ZThread
extends Object
implements Runnable
A base class for implementations of actions running in a looping thread with set suspension time between iterations.

Extensions of this class shall at least implement two abstract methods:

doLoop() - the action to execute in one loop iteration.

getLoopInterval() - determines the time to wait until the next loop iteration.

Implementations should also call execute() to spawn the thread and start the loop.

  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected ZThread()
    Constructs a looping thread object.
  • Method Summary

    Modifier and Type Method Description
    void activate()
    Activates the thread action explicitly.
    void cleanup()
    Terminates the loop and shuts down the thread.
    protected abstract void doLoop()
    Implement this method with the code to execute at each loop iteration.
    void execute()
    Spawns the thread and starts the thread loop.
    protected int getExecutionHour()
    Returns the time of the day, as a number of milliseconds since midnight, that the thread action runs at.
    protected abstract long getLoopInterval()
    Returns a value that determines the time interval to wait between loop iterations.
    void kill()
    Aborts the thread action if it runs and resets the object to its pre-execute state.
    void run()
    Internal implementation of Runnable.run().

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ZThread

      protected ZThread()
      Constructs a looping thread object.
  • Method Details

    • doLoop

      protected abstract void doLoop()
      Implement this method with the code to execute at each loop iteration.
    • getLoopInterval

      protected abstract long getLoopInterval()
      Returns a value that determines the time interval to wait between loop iterations.

      Implement this method to specify the time interval to wait between iterations, as follows:

      • If the method returns a positive number then this is the number of milliseconds of the loop rate.
      • If the method returns a negative number then the thread action runs once then waits for an explicit activation by activate() for the next iteration.
      • If the method returns zero then the thread action runs daily at a specific time of the day. In this case you must override getExecutionHour().
      Returns:
      a positive number of milliseconds of the iteration rate, a negative value for explicitly activated iterations or zero for execution at a specific time of the day.
      See Also:
      getExecutionHour()
    • getExecutionHour

      protected int getExecutionHour()
      Returns the time of the day, as a number of milliseconds since midnight, that the thread action runs at.

      Override this function if you return zero in getLoopInterval() to set the time of the day that the thread action runs at daily. The time of the day is specified as the number of seconds since midnight.

      By default the method returns a negative value, which means that the thread action shall be explicitly activated by activate().

      Returns:
      the time of the day as the number of milliseconds after midnight that the thread action runs at.
      See Also:
      getLoopInterval()
    • execute

      public void execute()
      Spawns the thread and starts the thread loop.
    • activate

      public void activate()
      Activates the thread action explicitly.

      Calling this method is the only way to run the thread action if getLoopInterval() returns a negative value. In other cases, the method activates the action immediately regardless of the set loop rate.

    • kill

      public final void kill()
      Aborts the thread action if it runs and resets the object to its pre-execute state.
    • cleanup

      public void cleanup()
      Terminates the loop and shuts down the thread.
    • run

      public final void run()
      Internal implementation of Runnable.run().
      Specified by:
      run in interface Runnable