Class PrioritySchedulerTaskInterceptor

    • Constructor Detail

      • PrioritySchedulerTaskInterceptor

        public PrioritySchedulerTaskInterceptor​(PrioritySchedulerService parentScheduler,
                                                java.util.function.BiFunction<java.lang.Runnable,​java.lang.Boolean,​java.lang.Runnable> taskManipulator)
        Constructs a wrapper for PrioritySchedulerService pool so that tasks can be intercepted and modified, before being submitted to the pool.
        Parameters:
        parentScheduler - An instance of PrioritySchedulerService to wrap
        taskManipulator - A lambda to manipulate a Runnable that was submitted for execution
    • Method Detail

      • 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,
                                           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,
                                        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
      • 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
      • 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