Class ScheduledRunner

All Implemented Interfaces:
Runnable

public class ScheduledRunner extends GSThread

ScheduledRunner is a single daemon thread that wakes up at fixed intervals, to run a single Runnable task. Calling cancel() method will wake up and terminate this daemon thread.

Much like Timer but without the overhead of a managed queue of tasks.


 Usage:
 ScheduledRunner runner =
        new ScheduledRunner( new YourRunnable(), "Runnable Name", 0 /delay/, 1000 /period/);

 ...
 runner.reschedule(2000); //reschedule with a new period
 ...
 runner.cancel();       //cancel - no longer needed
 runner = null;         //help gc
 
  • Constructor Details

    • ScheduledRunner

      public ScheduledRunner(Runnable target, long delay, long period)
      Allocates a new ScheduledRunner object. This constructor has the same effect as ScheduledRunner(target, name) where name is the target's class name.
      Parameters:
      target - the object whose run method is called.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive Runnable target executions.
      See Also:
    • ScheduledRunner

      public ScheduledRunner(Runnable target, String name, long delay, long period)
      Allocates a new ScheduledRunner object. This constructor has the same effect as ScheduledRunner(target) where name is a chosen name for this Runnable target.
      Parameters:
      target - the object whose run method is called.
      name - the chosen name for this Runnable target.
      period - time in milliseconds between successive Runnable target executions.
      See Also:
  • Method Details

    • run

      public void run()
      Runs the Runnable target's run method after every sleep interval unless cancel method was invoked, or if this thread was Interrupted.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • reschedule

      public final void reschedule(long period)
      Reschedules this Runnable execution for a new specified duration immediately. Much like scheduling with a no delay for a renewal period. If the Runnable target is currently awake, it will fall asleep only after its execution. Otherwise, it will wakeup, run, and fall asleep for the new duration.
      Parameters:
      period - the new period for this Runnable target.
    • cancel

      public void cancel()
      Cancels all subsequent executions of the Runnable target. If a target is currently running, it will end gracefully.