Class TimeScheduler3

  • All Implemented Interfaces:
    java.lang.Runnable, TimeScheduler

    public class TimeScheduler3
    extends java.lang.Object
    implements TimeScheduler, java.lang.Runnable
    Implementation of TimeScheduler. Uses a DelayQueue to order tasks according to execution times
    Since:
    3.3
    Author:
    Bela Ban
    • Field Detail

      • pool

        protected java.util.concurrent.Executor pool
        Thread pool used to execute the tasks
      • queue

        protected final java.util.concurrent.BlockingQueue<TimeScheduler3.Task> queue
        DelayQueue with tasks being sorted according to execution times (next execution first)
      • runner

        protected volatile java.lang.Thread runner
        Thread which removes tasks ready to be executed from the queue and submits them to the pool for execution
      • log

        protected static final Log log
      • timer_thread_factory

        protected ThreadFactory timer_thread_factory
      • non_blocking_task_handling

        protected boolean non_blocking_task_handling
      • shut_down_pool

        protected boolean shut_down_pool
    • Constructor Detail

      • TimeScheduler3

        public TimeScheduler3()
        Create a scheduler that executes tasks in dynamically adjustable intervals
      • TimeScheduler3

        public TimeScheduler3​(ThreadFactory factory,
                              int min_threads,
                              int max_threads,
                              long keep_alive_time,
                              int max_queue_size,
                              java.lang.String rejection_policy)
      • TimeScheduler3

        public TimeScheduler3​(ThreadFactory factory,
                              int min_threads,
                              int max_threads,
                              long keep_alive_time,
                              java.util.concurrent.BlockingQueue<java.lang.Runnable> queue,
                              java.lang.String rejection_policy,
                              boolean thread_pool_enabled)
      • TimeScheduler3

        public TimeScheduler3​(java.util.concurrent.Executor thread_pool,
                              ThreadFactory factory,
                              boolean start)
    • Method Detail

      • setThreadPool

        public void setThreadPool​(java.util.concurrent.Executor new_pool)
      • getMinThreads

        public int getMinThreads()
        Description copied from interface: TimeScheduler
        Returns the configured core threads, or -1 if not applicable
        Specified by:
        getMinThreads in interface TimeScheduler
        Returns:
      • setMinThreads

        public void setMinThreads​(int size)
        Description copied from interface: TimeScheduler
        Sets the core pool size. Can be ignored if not applicable
        Specified by:
        setMinThreads in interface TimeScheduler
      • getMaxThreads

        public int getMaxThreads()
        Description copied from interface: TimeScheduler
        Returns the configured max threads, or -1 if not applicable
        Specified by:
        getMaxThreads in interface TimeScheduler
        Returns:
      • setMaxThreads

        public void setMaxThreads​(int size)
        Description copied from interface: TimeScheduler
        Sets the max pool size. Can be ignored if not applicable
        Specified by:
        setMaxThreads in interface TimeScheduler
      • getKeepAliveTime

        public long getKeepAliveTime()
        Description copied from interface: TimeScheduler
        Returns the keep alive time (in ms) of the thread pool, or -1 if not applicable
        Specified by:
        getKeepAliveTime in interface TimeScheduler
      • setKeepAliveTime

        public void setKeepAliveTime​(long time)
        Description copied from interface: TimeScheduler
        Sets the keep alive time (in ms) of the thread pool. Can be ignored if not applicable
        Specified by:
        setKeepAliveTime in interface TimeScheduler
      • getCurrentThreads

        public int getCurrentThreads()
        Description copied from interface: TimeScheduler
        Returns the current threads in the pool, or -1 if not applicable
        Specified by:
        getCurrentThreads in interface TimeScheduler
        Returns:
      • getQueueSize

        public int getQueueSize()
      • size

        public int size()
        Description copied from interface: TimeScheduler
        Returns the number of tasks currently in the queue.
        Specified by:
        size in interface TimeScheduler
        Returns:
        The number of tasks currently in the queue.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isShutdown

        public boolean isShutdown()
        Description copied from interface: TimeScheduler
        Returns true if stop() has been called, false otherwise
        Specified by:
        isShutdown in interface TimeScheduler
      • dumpTimerTasks

        public java.lang.String dumpTimerTasks()
        Description copied from interface: TimeScheduler
        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.
        Specified by:
        dumpTimerTasks in interface TimeScheduler
        Returns:
      • execute

        public void execute​(java.lang.Runnable task,
                            boolean can_block)
        Specified by:
        execute in interface TimeScheduler
      • schedule

        public java.util.concurrent.Future<?> schedule​(java.lang.Runnable work,
                                                       long initial_delay,
                                                       java.util.concurrent.TimeUnit unit,
                                                       boolean can_block)
        Specified by:
        schedule in interface TimeScheduler
      • scheduleWithFixedDelay

        public java.util.concurrent.Future<?> scheduleWithFixedDelay​(java.lang.Runnable work,
                                                                     long initial_delay,
                                                                     long delay,
                                                                     java.util.concurrent.TimeUnit unit,
                                                                     boolean can_block)
        Specified by:
        scheduleWithFixedDelay in interface TimeScheduler
      • scheduleAtFixedRate

        public java.util.concurrent.Future<?> scheduleAtFixedRate​(java.lang.Runnable work,
                                                                  long initial_delay,
                                                                  long delay,
                                                                  java.util.concurrent.TimeUnit unit,
                                                                  boolean can_block)
        Specified by:
        scheduleAtFixedRate in interface TimeScheduler
      • stop

        public void stop()
        Stops the timer, cancelling all tasks
        Specified by:
        stop in interface TimeScheduler
      • run

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

        protected java.util.concurrent.Future<?> scheduleRecurring​(java.lang.Runnable work,
                                                                   TimeScheduler3.TaskType type,
                                                                   long initial_delay,
                                                                   long delay,
                                                                   java.util.concurrent.TimeUnit unit,
                                                                   boolean can_block)
      • doSchedule

        protected java.util.concurrent.Future<?> doSchedule​(TimeScheduler3.Task task,
                                                            long initial_delay)
      • condSet

        protected void condSet​(java.util.function.Consumer<java.util.concurrent.ThreadPoolExecutor> setter)
      • condGet

        protected <T> T condGet​(java.util.function.Function<java.util.concurrent.ThreadPoolExecutor,​T> getter,
                                T default_value)
      • isRunning

        protected boolean isRunning()