Class DefaultPriorityWrapper

    • Constructor Detail

      • DefaultPriorityWrapper

        public DefaultPriorityWrapper​(PrioritySchedulerService scheduler,
                                      TaskPriority defaultPriority)
        Constructs a new priority wrapper with a new default priority to use.
        Parameters:
        scheduler - PriorityScheduler implementation to default to
        defaultPriority - default priority for tasks submitted without a priority
    • Method Detail

      • ensurePriority

        public static PrioritySchedulerService ensurePriority​(PrioritySchedulerService scheduler,
                                                              TaskPriority defaultPriority)
        Convenience function for wrapping the scheduler if the default priority is not what is desired. If it is already the desired priority it will simply return the provided reference directly.
        Parameters:
        scheduler - Scheduler to check priority against or wrap
        defaultPriority - The default priority the returned scheduler should have
        Returns:
        A scheduler with the set default task priority
      • execute

        public void execute​(java.lang.Runnable command)
        Specified by:
        execute in interface java.util.concurrent.Executor
      • 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 ListenableFuture<?> submit​(java.lang.Runnable task)
        Description copied from interface: SubmitterExecutor
        Submit a task to run as soon as possible. There is a slight increase in load when using SubmitterExecutor.submit(Runnable) over Executor.execute(Runnable). So this should only be used when the returned future is necessary.

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

        Specified by:
        submit in interface SubmitterExecutor
        Parameters:
        task - runnable to be executed
        Returns:
        a future to know when the task has completed
      • submit

        public <T> ListenableFuture<T> submit​(java.lang.Runnable task,
                                              T result)
        Description copied from interface: SubmitterExecutor
        Submit a task to run as soon as possible. The Future.get() method will return the provided result once the runnable has completed.
        Specified by:
        submit in interface SubmitterExecutor
        Type Parameters:
        T - type of result for future
        Parameters:
        task - runnable to be executed
        result - result to be returned from resulting future .get() when runnable completes
        Returns:
        a future to know when the task has completed
      • submit

        public ListenableFuture<?> submit​(java.lang.Runnable task,
                                          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 null once the runnable has completed.

        Specified by:
        submit in interface PrioritySchedulerService
        Parameters:
        task - runnable to be executed
        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.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)
        Description copied from interface: SubmitterExecutor
        Submit a Callable to run as soon as possible. This is needed when a result needs to be consumed from the callable.
        Specified by:
        submit in interface SubmitterExecutor
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - callable to be executed
        Returns:
        a future to know when the task has completed and get the result of the callable
      • 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)
        Description copied from interface: SubmitterScheduler
        Schedule a one time task with a given delay.
        Specified by:
        schedule in interface SubmitterScheduler
        Parameters:
        task - runnable to execute
        delayInMs - time in milliseconds to wait to execute task
      • 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)
        Description copied from interface: SubmitterScheduler
        Schedule a task with a given delay. The Future.get() method will return the provided result once the runnable has completed.
        Specified by:
        submitScheduled in interface SubmitterScheduler
        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
        Returns:
        a future to know when the task has completed
      • 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)
        Description copied from interface: SubmitterScheduler
        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 SubmitterScheduler
        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
        Returns:
        a future to know when the task has completed and get the result of the callable
      • 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
      • 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).

        Specified by:
        scheduleWithFixedDelay in interface PrioritySchedulerService
        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)
        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
      • 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).

        Specified by:
        scheduleAtFixedRate in interface PrioritySchedulerService
        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
      • 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
      • isShutdown

        public boolean isShutdown()
        Description copied from interface: SchedulerService
        Function to check if the thread pool is currently accepting and handling tasks.
        Specified by:
        isShutdown in interface SchedulerService
        Returns:
        true if thread pool is running
      • 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
      • getActiveTaskCount

        public int getActiveTaskCount()
        Description copied from interface: SchedulerService
        Call to check how many tasks are currently being executed in this scheduler.
        Specified by:
        getActiveTaskCount in interface SchedulerService
        Returns:
        current number of running tasks
      • 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