Class Scheduler

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class Scheduler
    extends java.lang.Object
    implements java.io.Closeable
    A simple scheduler for executing and scheduling tasks in the background. This implementation delegates all background execution to an instance of a ScheduledExecutorService with core pool size 1. The behaviour of this underlying scheduler service determines the semantics of the methods in this class. Namely: Execution of background tasks never overlaps and is FIFO for tasks scheduled for the same time. In addition all tasks scheduled through methods of this class are automatically wrapped into SafeRunnable instances. The background thread executing submitted tasks is a deamon thread.
    • Constructor Summary

      Constructors 
      Constructor Description
      Scheduler​(@Nullable java.lang.String name)
      Create a new instance with the given name.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close this scheduler.
      void execute​(@NotNull java.lang.String name, @NotNull java.lang.Runnable task)
      Immediately execute task.
      void scheduleAtFixedRate​(@NotNull java.lang.String name, long period, @NotNull java.util.concurrent.TimeUnit unit, @NotNull java.lang.Runnable task)
      Run task regularly at a given interval.
      void scheduleOnce​(@NotNull java.lang.String name, long delay, @NotNull java.util.concurrent.TimeUnit unit, @NotNull java.lang.Runnable task)
      Run task once after some delay.
      void scheduleWithFixedDelay​(@NotNull java.lang.String name, long delay, @NotNull java.util.concurrent.TimeUnit unit, @NotNull java.lang.Runnable task)
      Run task regularly after a fixed delay.
      • Methods inherited from class java.lang.Object

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

      • Scheduler

        public Scheduler​(@Nullable
                         @Nullable java.lang.String name)
        Create a new instance with the given name. The name is used to derive the default name of the background thread from..
        Parameters:
        name -
    • Method Detail

      • execute

        public void execute​(@NotNull
                            @NotNull java.lang.String name,
                            @NotNull
                            @NotNull java.lang.Runnable task)
        Immediately execute task. The background thread's name is set to name during execution of task.
        Parameters:
        name -
        task -
        See Also:
        Executor.execute(Runnable)
      • scheduleOnce

        public void scheduleOnce​(@NotNull
                                 @NotNull java.lang.String name,
                                 long delay,
                                 @NotNull
                                 @NotNull java.util.concurrent.TimeUnit unit,
                                 @NotNull
                                 @NotNull java.lang.Runnable task)
        Run task once after some delay. The background thread's name is set to name during execution of task.
        Parameters:
        name -
        delay -
        unit -
        task -
        See Also:
        ScheduledExecutorService.schedule(Runnable, long, TimeUnit)
      • scheduleAtFixedRate

        public void scheduleAtFixedRate​(@NotNull
                                        @NotNull java.lang.String name,
                                        long period,
                                        @NotNull
                                        @NotNull java.util.concurrent.TimeUnit unit,
                                        @NotNull
                                        @NotNull java.lang.Runnable task)
        Run task regularly at a given interval. The background thread's name is set to name during execution of task.
        Parameters:
        name -
        period -
        unit -
        task -
        See Also:
        ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
      • scheduleWithFixedDelay

        public void scheduleWithFixedDelay​(@NotNull
                                           @NotNull java.lang.String name,
                                           long delay,
                                           @NotNull
                                           @NotNull java.util.concurrent.TimeUnit unit,
                                           @NotNull
                                           @NotNull java.lang.Runnable task)
        Run task regularly after a fixed delay. The background thread's name is set to name during execution of task.
        Parameters:
        name -
        delay -
        unit -
        task -
        See Also:
        ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
      • close

        public void close()
        Close this scheduler.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        See Also:
        ExecutorService.shutdown()