Interface TimeScheduler

  • All Known Implementing Classes:
    TimeScheduler3

    public interface TimeScheduler
    Timer-like interface which allows for execution of tasks. Taks can be executed
    • one time only
    • at recurring time intervals. Intervals can be fixed-delay or fixed-rate, see ScheduledExecutorService for details
    • dynamic; at the end of the task execution, a task is asked what the next execution time should be. To do this, method TimeScheduler.Task.nextInterval() needs to be implemented.
    Author:
    Bela Ban
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.lang.String dumpTimerTasks()
      Returns a list of tasks currently waiting for execution.
      default void execute​(java.lang.Runnable command)
      Executes command with zero required delay.
      void execute​(java.lang.Runnable command, boolean can_block)  
      int getCurrentThreads()
      Returns the current threads in the pool, or -1 if not applicable
      long getKeepAliveTime()
      Returns the keep alive time (in ms) of the thread pool, or -1 if not applicable
      int getMaxThreads()
      Returns the configured max threads, or -1 if not applicable
      int getMinThreads()
      Returns the configured core threads, or -1 if not applicable
      boolean getNonBlockingTaskHandling()  
      boolean isShutdown()
      Returns true if stop() has been called, false otherwise
      void removeCancelledTasks()  
      default java.util.concurrent.Future<?> schedule​(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)
      Creates and executes a one-shot action that becomes enabled after the given delay.
      java.util.concurrent.Future<?> schedule​(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)  
      default java.util.concurrent.Future<?> scheduleAtFixedRate​(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
      Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
      java.util.concurrent.Future<?> scheduleAtFixedRate​(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit, boolean can_block)  
      default java.util.concurrent.Future<?> scheduleWithDynamicInterval​(TimeScheduler.Task task)
      Schedule a task for execution at varying intervals.
      java.util.concurrent.Future<?> scheduleWithDynamicInterval​(TimeScheduler.Task task, boolean can_block)  
      default java.util.concurrent.Future<?> scheduleWithFixedDelay​(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)
      Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.
      java.util.concurrent.Future<?> scheduleWithFixedDelay​(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)  
      void setKeepAliveTime​(long time)
      Sets the keep alive time (in ms) of the thread pool.
      void setMaxThreads​(int size)
      Sets the max pool size.
      void setMinThreads​(int size)
      Sets the core pool size.
      void setNonBlockingTaskHandling​(boolean b)  
      void setThreadFactory​(ThreadFactory factory)  
      int size()
      Returns the number of tasks currently in the queue.
      void start()
      Starts the runner thread
      void stop()
      Stops the scheduler if running, cancelling all pending tasks
    • Method Detail

      • execute

        default void execute​(java.lang.Runnable command)
        Executes command with zero required delay. This has effect equivalent to schedule(command, 0, anyUnit).
        Parameters:
        command - the task to execute
        Throws:
        java.util.concurrent.RejectedExecutionException - at discretion of RejectedExecutionHandler, if task cannot be accepted for execution because the executor has been shut down.
        java.lang.NullPointerException - if command is null
      • execute

        void execute​(java.lang.Runnable command,
                     boolean can_block)
      • schedule

        default java.util.concurrent.Future<?> schedule​(java.lang.Runnable command,
                                                        long delay,
                                                        java.util.concurrent.TimeUnit unit)
        Creates and executes a one-shot action that becomes enabled after the given delay.
        Parameters:
        command - the task to execute
        delay - the time from now to delay execution
        unit - the time unit of the delay parameter
        Returns:
        a ScheduledFuture representing pending completion of the task and whose get() method will return null upon completion
        Throws:
        java.util.concurrent.RejectedExecutionException - if the task cannot be scheduled for execution
        java.lang.NullPointerException - if command is null
      • schedule

        java.util.concurrent.Future<?> schedule​(java.lang.Runnable command,
                                                long delay,
                                                java.util.concurrent.TimeUnit unit,
                                                boolean can_block)
      • scheduleWithFixedDelay

        default java.util.concurrent.Future<?> scheduleWithFixedDelay​(java.lang.Runnable command,
                                                                      long initialDelay,
                                                                      long delay,
                                                                      java.util.concurrent.TimeUnit unit)
        Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.
        Parameters:
        command - the task to execute
        initialDelay - the time to delay first execution
        delay - the delay between the termination of one execution and the commencement of the next
        unit - the time unit of the initialDelay and delay parameters
        Returns:
        a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
        Throws:
        java.util.concurrent.RejectedExecutionException - if the task cannot be scheduled for execution
        java.lang.NullPointerException - if command is null
        java.lang.IllegalArgumentException - if delay less than or equal to zero
      • scheduleWithFixedDelay

        java.util.concurrent.Future<?> scheduleWithFixedDelay​(java.lang.Runnable command,
                                                              long initialDelay,
                                                              long delay,
                                                              java.util.concurrent.TimeUnit unit,
                                                              boolean can_block)
      • scheduleAtFixedRate

        default java.util.concurrent.Future<?> scheduleAtFixedRate​(java.lang.Runnable command,
                                                                   long initialDelay,
                                                                   long period,
                                                                   java.util.concurrent.TimeUnit unit)
        Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
        Parameters:
        command - the task to execute
        initialDelay - the time to delay first execution
        period - the period between successive executions
        unit - the time unit of the initialDelay and period parameters
        Returns:
        a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
        Throws:
        java.util.concurrent.RejectedExecutionException - if the task cannot be scheduled for execution
        java.lang.NullPointerException - if command is null
        java.lang.IllegalArgumentException - if period less than or equal to zero
      • scheduleAtFixedRate

        java.util.concurrent.Future<?> scheduleAtFixedRate​(java.lang.Runnable command,
                                                           long initialDelay,
                                                           long period,
                                                           java.util.concurrent.TimeUnit unit,
                                                           boolean can_block)
      • scheduleWithDynamicInterval

        default java.util.concurrent.Future<?> scheduleWithDynamicInterval​(TimeScheduler.Task task)
        Schedule a task for execution at varying intervals. After execution, the task will get rescheduled after TimeScheduler.Task.nextInterval() milliseconds. This is delay-based and not rate-based. The task is never done until nextInterval() return a value <= 0 or the task is cancelled.
        Parameters:
        task - the task to execute
      • scheduleWithDynamicInterval

        java.util.concurrent.Future<?> scheduleWithDynamicInterval​(TimeScheduler.Task task,
                                                                   boolean can_block)
      • setThreadFactory

        void setThreadFactory​(ThreadFactory factory)
      • dumpTimerTasks

        java.lang.String dumpTimerTasks()
        Returns a list of tasks currently waiting for execution. If there are a lot of tasks, the returned string should probably only return the number of tasks rather than a full dump.
        Returns:
      • removeCancelledTasks

        void removeCancelledTasks()
      • getMinThreads

        int getMinThreads()
        Returns the configured core threads, or -1 if not applicable
        Returns:
      • setMinThreads

        void setMinThreads​(int size)
        Sets the core pool size. Can be ignored if not applicable
      • getMaxThreads

        int getMaxThreads()
        Returns the configured max threads, or -1 if not applicable
        Returns:
      • setMaxThreads

        void setMaxThreads​(int size)
        Sets the max pool size. Can be ignored if not applicable
      • getKeepAliveTime

        long getKeepAliveTime()
        Returns the keep alive time (in ms) of the thread pool, or -1 if not applicable
      • setKeepAliveTime

        void setKeepAliveTime​(long time)
        Sets the keep alive time (in ms) of the thread pool. Can be ignored if not applicable
      • getCurrentThreads

        int getCurrentThreads()
        Returns the current threads in the pool, or -1 if not applicable
        Returns:
      • getNonBlockingTaskHandling

        boolean getNonBlockingTaskHandling()
      • setNonBlockingTaskHandling

        void setNonBlockingTaskHandling​(boolean b)
      • size

        int size()
        Returns the number of tasks currently in the queue.
        Returns:
        The number of tasks currently in the queue.
      • start

        void start()
        Starts the runner thread
      • stop

        void stop()
        Stops the scheduler if running, cancelling all pending tasks
      • isShutdown

        boolean isShutdown()
        Returns true if stop() has been called, false otherwise