Class SingleThreadScheduler

    • Constructor Detail

      • SingleThreadScheduler

        public SingleThreadScheduler()
        Constructs a new SingleThreadScheduler. No threads will start until the first task is provided. This defaults to using a daemon thread for the scheduler.
      • SingleThreadScheduler

        public SingleThreadScheduler​(TaskPriority defaultPriority,
                                     long maxWaitForLowPriorityInMs)
        Constructs a new SingleThreadScheduler. No threads will start until the first task is provided. This defaults to using a daemon thread for the scheduler.
        Parameters:
        defaultPriority - Default priority for tasks which are submitted without any specified priority
        maxWaitForLowPriorityInMs - time low priority tasks to wait if there are high priority tasks ready to run
      • SingleThreadScheduler

        public SingleThreadScheduler​(boolean daemonThread)
        Constructs a new SingleThreadScheduler. No threads will start until the first task is provided.
        Parameters:
        daemonThread - true if scheduler thread should be a daemon thread
      • SingleThreadScheduler

        public SingleThreadScheduler​(TaskPriority defaultPriority,
                                     long maxWaitForLowPriorityInMs,
                                     boolean daemonThread)
        Constructs a new SingleThreadScheduler. No threads will start until the first task is provided.
        Parameters:
        defaultPriority - Default priority for tasks which are submitted without any specified priority
        maxWaitForLowPriorityInMs - time low priority tasks to wait if there are high priority tasks ready to run
        daemonThread - true if scheduler thread should be a daemon thread
      • SingleThreadScheduler

        public SingleThreadScheduler​(java.util.concurrent.ThreadFactory threadFactory)
        Constructs a new SingleThreadScheduler. No threads will start until the first task is provided.
        Parameters:
        threadFactory - factory to make thread for scheduler
      • SingleThreadScheduler

        public SingleThreadScheduler​(TaskPriority defaultPriority,
                                     long maxWaitForLowPriorityInMs,
                                     java.util.concurrent.ThreadFactory threadFactory)
        Constructs a new SingleThreadScheduler. No threads will start until the first task is provided.
        Parameters:
        defaultPriority - Default priority for tasks which are submitted without any specified priority
        maxWaitForLowPriorityInMs - time low priority tasks to wait if there are high priority tasks ready to run
        threadFactory - factory to make thread for scheduler
    • Method Detail

      • shutdown

        public void shutdown()
        Stops any new tasks from being submitted to the pool. But allows all tasks which are submitted to execute, or scheduled (and have elapsed their delay time) to run. If recurring tasks are present they will also be unable to reschedule. This call will not block to wait for the shutdown of the scheduler to finish. If shutdown() or shutdownNow() has already been called, this will have no effect.

        If you wish to not want to run any queued tasks you should use shutdownNow().

      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Stops any new tasks from being submitted to the pool. If any tasks are waiting for execution they will be prevented from being run. If a task is currently running it will be allowed to finish (though this call will not block waiting for it to finish).
        Returns:
        returns a list of runnables which were waiting in the queue to be run at time of shutdown
      • awaitTermination

        public void awaitTermination()
                              throws java.lang.InterruptedException
        Block until the thread pool has shutdown and all threads have been stopped. If neither shutdown() or shutdownNow() is invoked, then this will block forever.
        Throws:
        java.lang.InterruptedException - Thrown if blocking thread is interrupted waiting for shutdown
      • awaitTermination

        public boolean awaitTermination​(long timeoutMillis)
                                 throws java.lang.InterruptedException
        Block until the thread pool has shutdown and all threads have been stopped. If neither shutdown() or shutdownNow() is invoked, then this will block until the timeout is reached.
        Parameters:
        timeoutMillis - time to block and wait for thread pool to shutdown
        Returns:
        true if the pool has shutdown, false if timeout was reached
        Throws:
        java.lang.InterruptedException - Thrown if blocking thread is interrupted waiting for shutdown
      • isShutdown

        public boolean isShutdown()
        Description copied from interface: SchedulerService
        Function to check if the thread pool is currently accepting and handling tasks.
        Returns:
        true if thread pool is running
      • isTerminated

        public boolean isTerminated()
        One step beyond isShutdown(), a true here indicates that not only has a shutdown on the pool been requested, but that the thread running tasks has completed.
        Returns:
        true if this scheduler is no longer processing any tasks
      • prestartExecutionThread

        public void prestartExecutionThread()
        Start thread for execution if not already started. This can avoid the minor delay of the thread starting later. This will not block till the thread is running, instead letting it start in the background (usually fine).
      • prestartExecutionThread

        public void prestartExecutionThread​(boolean blockTillStarted)
        Start thread for execution if not already started. This can avoid the minor delay of the thread starting later.
        Parameters:
        blockTillStarted - If true this will not return till the scheduler thread has started
      • scheduleWithFixedDelay

        public void scheduleWithFixedDelay​(java.lang.Runnable task,
                                           long initialDelay,
                                           long recurringDelay,
                                           TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Schedule a fixed delay recurring task to run. The recurring delay time will be from the point where execution has finished. So the execution frequency is the recurringDelay + runtime for the provided task.

        Unlike ScheduledExecutorService if the task throws an exception, subsequent executions are NOT suppressed or prevented. So if the task throws an exception on every run, the task will continue to be executed at the provided recurring delay (possibly throwing an exception on each execution).

        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        recurringDelay - delay in milliseconds for running task after last finish
        priority - priority for task to get available thread to run on
      • scheduleAtFixedRate

        public void scheduleAtFixedRate​(java.lang.Runnable task,
                                        long initialDelay,
                                        long period,
                                        TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Schedule a fixed rate recurring task to run. The recurring delay will be the same, regardless of how long task execution takes. A given runnable will not run concurrently (unless it is submitted to the scheduler multiple times). Instead of execution takes longer than the period, the next run will occur immediately (given thread availability in the pool).

        Unlike ScheduledExecutorService if the task throws an exception, subsequent executions are NOT suppressed or prevented. So if the task throws an exception on every run, the task will continue to be executed at the provided recurring delay (possibly throwing an exception on each execution).

        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        period - amount of time in milliseconds between the start of recurring executions
        priority - priority for task to get available thread to run on
      • getActiveTaskCount

        public int getActiveTaskCount()
        Description copied from interface: SchedulerService
        Call to check how many tasks are currently being executed in this scheduler.
        Returns:
        current number of running tasks