Class AbstractPriorityScheduler

    • Method Detail

      • setMaxWaitForLowPriority

        public void setMaxWaitForLowPriority​(long maxWaitForLowPriorityInMs)
        Changes the max wait time for low priority tasks. This is the amount of time that a low priority task will wait if there are ready to execute high priority tasks. After a low priority task has waited this amount of time, it will be executed fairly with high priority tasks (meaning it will only execute the high priority task if it has been waiting longer than the low priority task).
        Parameters:
        maxWaitForLowPriorityInMs - new wait time in milliseconds for low priority tasks during thread contention
      • getMaxWaitForLowPriority

        public long getMaxWaitForLowPriority()
        Description copied from interface: PrioritySchedulerService
        Getter for the amount of time a low priority task will wait during thread contention before it is eligible for execution.
        Specified by:
        getMaxWaitForLowPriority in interface PrioritySchedulerService
        Returns:
        currently set max wait for low priority task
      • execute

        public void execute​(java.lang.Runnable task,
                            TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Executes the task as soon as possible for the given priority.
        Specified by:
        execute in interface PrioritySchedulerService
        Parameters:
        task - runnable to execute
        priority - priority for task to get available thread to run on
      • submit

        public <T> ListenableFuture<T> submit​(java.lang.Runnable task,
                                              T result,
                                              TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Submit a task to run as soon as possible for the given priority. There is a slight increase in load when using submit over execute. So this should only be used when the future is necessary.

        The Future.get() method will return the provided result once the runnable has completed.

        Specified by:
        submit in interface PrioritySchedulerService
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - runnable to be executed
        result - result to be returned from resulting future .get() when runnable completes
        priority - priority for task to get available thread to run on
        Returns:
        a future to know when the task has completed
      • submit

        public <T> ListenableFuture<T> submit​(java.util.concurrent.Callable<T> task,
                                              TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Submit a Callable to run as soon as possible for the given priority. This is needed when a result needs to be consumed from the callable.
        Specified by:
        submit in interface PrioritySchedulerService
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - callable to be executed
        priority - priority for task to get available thread to run on
        Returns:
        a future to know when the task has completed and get the result of the callable
      • schedule

        public void schedule​(java.lang.Runnable task,
                             long delayInMs,
                             TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Schedule a task with a given delay and a specified priority.
        Specified by:
        schedule in interface PrioritySchedulerService
        Parameters:
        task - runnable to execute
        delayInMs - time in milliseconds to wait to execute task
        priority - priority for task to get available thread to run on
      • submitScheduled

        public <T> ListenableFuture<T> submitScheduled​(java.lang.Runnable task,
                                                       T result,
                                                       long delayInMs,
                                                       TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Schedule a task with a given delay and a specified priority.

        The Future.get() method will return the provided result once the runnable has completed.

        Specified by:
        submitScheduled in interface PrioritySchedulerService
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - runnable to execute
        result - result to be returned from resulting future .get() when runnable completes
        delayInMs - time in milliseconds to wait to execute task
        priority - priority for task to get available thread to run on
        Returns:
        a future to know when the task has completed
      • submitScheduled

        public <T> ListenableFuture<T> submitScheduled​(java.util.concurrent.Callable<T> task,
                                                       long delayInMs,
                                                       TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Schedule a Callable with a given delay. This is needed when a result needs to be consumed from the callable.
        Specified by:
        submitScheduled in interface PrioritySchedulerService
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - callable to be executed
        delayInMs - time in milliseconds to wait to execute task
        priority - priority for task to get available thread to run on
        Returns:
        a future to know when the task has completed and get the result of the callable
      • scheduleWithFixedDelay

        public void scheduleWithFixedDelay​(java.lang.Runnable task,
                                           long initialDelay,
                                           long recurringDelay)
        Description copied from interface: SubmitterScheduler
        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).

        Specified by:
        scheduleWithFixedDelay in interface SubmitterScheduler
        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        recurringDelay - delay in milliseconds for running task after last finish
      • scheduleAtFixedRate

        public void scheduleAtFixedRate​(java.lang.Runnable task,
                                        long initialDelay,
                                        long period)
        Description copied from interface: SubmitterScheduler
        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).

        Specified by:
        scheduleAtFixedRate in interface SubmitterScheduler
        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
      • remove

        public boolean remove​(java.lang.Runnable task)
        Description copied from interface: SchedulerService
        Removes the runnable task from the execution queue. It is possible for the runnable to still run until this call has returned.

        Note that this call has high guarantees on the ability to remove the task (as in a complete guarantee). But while this is being invoked, it will reduce the throughput of execution, so should NOT be used extremely frequently.

        For non-recurring tasks using a future and calling Future.cancel(boolean) can be a better solution.

        Specified by:
        remove in interface SchedulerService
        Parameters:
        task - The original runnable provided to the executor
        Returns:
        true if the runnable was found and removed
      • remove

        public boolean remove​(java.util.concurrent.Callable<?> task)
        Description copied from interface: SchedulerService
        Removes the callable task from the execution queue. It is possible for the callable to still run until this call has returned.

        Note that this call has high guarantees on the ability to remove the task (as in a complete guarantee). But while this is being invoked, it will reduce the throughput of execution, so should NOT be used extremely frequently.

        For non-recurring tasks using a future and calling Future.cancel(boolean) can be a better solution.

        Specified by:
        remove in interface SchedulerService
        Parameters:
        task - The original callable provided to the executor
        Returns:
        true if the callable was found and removed
      • getQueuedTaskCount

        public int getQueuedTaskCount()
        Description copied from interface: SchedulerService
        Returns how many tasks are either waiting to be executed, or are scheduled to be executed at a future point. This can indicate pool back pressure, but it can also just indicate generally scheduled tasks. It's computationally cheaper than SchedulerService.getWaitingForExecutionTaskCount().
        Specified by:
        getQueuedTaskCount in interface SchedulerService
        Returns:
        quantity of tasks waiting execution or scheduled to be executed later
      • getWaitingForExecutionTaskCount

        public int getWaitingForExecutionTaskCount()
        Description copied from interface: SchedulerService
        Returns how many tasks are either waiting to be executed. A value here can indicate the pool is being starved for threads.
        Specified by:
        getWaitingForExecutionTaskCount in interface SchedulerService
        Returns:
        quantity of tasks waiting execution
      • getWaitingForExecutionTaskCount

        public int getWaitingForExecutionTaskCount​(TaskPriority priority)
        Description copied from interface: PrioritySchedulerService
        Returns a count of how many tasks are either waiting to be executed for a specific priority. A value here can indicate the pool is being starved for threads.
        Specified by:
        getWaitingForExecutionTaskCount in interface PrioritySchedulerService
        Parameters:
        priority - priority for tasks to be counted
        Returns:
        quantity of tasks waiting execution