Class CyclicTask

java.lang.Object
org.opentcs.util.CyclicTask
All Implemented Interfaces:
java.lang.Runnable

public abstract class CyclicTask
extends java.lang.Object
implements java.lang.Runnable
A template for cyclic tasks. Subclasses only need to provide an implementation of runActualTask(), which will be called until the task is terminated by calling terminate(); after each call of runActualTask(), a configurable delay may be inserted.
  • Constructor Summary

    Constructors 
    Constructor Description
    CyclicTask​(long tSleep)
    Creates a new CyclicTask.
  • Method Summary

    Modifier and Type Method Description
    boolean isIgnoringInterrupts()
    Indicates whether this task is ignoring interrupts while it's sleeping.
    boolean isTerminated()
    Indicates whether this task has been terminated.
    void run()  
    protected abstract void runActualTask()
    Defines the actual work this task should do in every cycle.
    void setIgnoringInterrupts​(boolean ignoreInterrupts)
    Sets/unsets this task's flag for ignoring interrupts during sleep phases.
    void terminate()
    Terminates this task before its next execution cycle.
    void terminateAndWait()
    Terminates this task before its next execution cycle and waits for it to finish before returning.

    Methods inherited from class java.lang.Object

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

    • CyclicTask

      public CyclicTask​(long tSleep)
      Creates a new CyclicTask.
      Parameters:
      tSleep - The time to sleep between two executions of the actual task (in milliseconds).
  • Method Details

    • isTerminated

      public boolean isTerminated()
      Indicates whether this task has been terminated.
      Returns:
      true if, and only if, this task's terminated flag has been set.
    • terminate

      public void terminate()
      Terminates this task before its next execution cycle. This method merely flags the task for termination and returns immediately. If the actual task is currently being executed, its execution will not be interrupted, but it will not be run again after finishing.
    • terminateAndWait

      public void terminateAndWait()
      Terminates this task before its next execution cycle and waits for it to finish before returning. (This method waits for termination unless the calling thread is the thread that is executing this task. In that case, this method merely flags this task for termination and returns immediately.)
    • isIgnoringInterrupts

      public boolean isIgnoringInterrupts()
      Indicates whether this task is ignoring interrupts while it's sleeping.
      Returns:
      true if, and only if, this task is ignoring interrupts while it's sleeping.
    • setIgnoringInterrupts

      public void setIgnoringInterrupts​(boolean ignoreInterrupts)
      Sets/unsets this task's flag for ignoring interrupts during sleep phases.
      Parameters:
      ignoreInterrupts - If true, this task will ignore interrupts during sleep phases; if false, the run() method will throw an exception when interrupted.
    • run

      public void run()
      Specified by:
      run in interface java.lang.Runnable
    • runActualTask

      protected abstract void runActualTask()
      Defines the actual work this task should do in every cycle.