Class ScheduledThreadPoolExecutorPlus

  • All Implemented Interfaces:
    java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent.ScheduledExecutorService, ExecutorPlus, ResizableThreadPool, ScheduledExecutorPlus

    public class ScheduledThreadPoolExecutorPlus
    extends java.util.concurrent.ScheduledThreadPoolExecutor
    implements ScheduledExecutorPlus
    Like ExecutorPlus, ScheduledThreadPoolExecutorPlus always logs exceptions from the tasks it is given, even if Future.get is never called elsewhere. Catches exceptions during Task execution so that they don't suppress subsequent invocations of the task. Finally, there is a special rejected execution handler for tasks rejected during the shutdown hook. - For fire and forget tasks (like ref tidy) we can safely ignore the exceptions. - For any callers that care to know their task was rejected we cancel passed task.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor

        java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(java.lang.Runnable run)  
      void execute​(WithResources withResources, java.lang.Runnable run)
      Invoke task.
      int getActiveTaskCount()
      Returns the approximate number of threads that are actively executing tasks.
      int getCorePoolSize()
      Returns core pool size of thread pool, the minimum number of workers (where that makes sense for a thread pool, SEPExecutor does not have a minimum size).
      int getMaximumPoolSize()
      Returns maximum pool size of thread pool.
      int getMaxTasksQueued()  
      int getPendingTaskCount()
      Returns the approximate total of tasks waiting to be executed.
      NamedThreadFactory getThreadFactory()  
      boolean inExecutor()  
      protected <T> RunnableFuture<T> newTaskFor​(java.lang.Runnable runnable, T value)  
      protected <T> RunnableFuture<T> newTaskFor​(java.util.concurrent.Callable<T> callable)  
      java.util.concurrent.ScheduledFuture<?> schedule​(java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)  
      <V> java.util.concurrent.ScheduledFuture<V> schedule​(java.util.concurrent.Callable<V> task, long delay, java.util.concurrent.TimeUnit unit)  
      java.util.concurrent.ScheduledFuture<?> scheduleAt​(java.lang.Runnable run, long deadline)
      Schedule a timeout action.
      java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(java.lang.Runnable task, long initialDelay, long period, java.util.concurrent.TimeUnit unit)  
      java.util.concurrent.ScheduledFuture<?> scheduleSelfRecurring​(java.lang.Runnable run, long delay, java.util.concurrent.TimeUnit units)
      Schedule an action that is recurring but self-administered.
      java.util.concurrent.ScheduledFuture<?> scheduleTimeoutAt​(java.lang.Runnable run, long deadline)
      Schedule a timeout action.
      java.util.concurrent.ScheduledFuture<?> scheduleTimeoutWithDelay​(java.lang.Runnable run, long delay, java.util.concurrent.TimeUnit units)
      Schedule a timeout action.
      java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay​(java.lang.Runnable task, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)  
      void setCorePoolSize​(int number)
      Allows user to resize minimum size of the thread pool.
      void setMaximumPoolSize​(int number)
      Allows user to resize maximum size of the thread pool.
      java.util.List<java.lang.Runnable> shutdownNow()  
      Future<?> submit​(java.lang.Runnable run)
      Overrides ExecutorService.submit(Runnable) to return a Cassandra Future
      <T> Future<T> submit​(java.lang.Runnable run, T result)
      Overrides ExecutorService.submit(Runnable, Object) to return a Cassandra Future
      <T> Future<T> submit​(java.util.concurrent.Callable<T> call)
      Overrides ExecutorService.submit(Callable) to return a Cassandra Future
      Future<?> submit​(WithResources withResources, java.lang.Runnable run)
      Invoke task, returning a future yielding null if successful, or the abnormal termination of task otherwise.
      <T> Future<T> submit​(WithResources withResources, java.lang.Runnable run, T result)
      Invoke task, returning a future yielding result if successful, or the abnormal termination of task otherwise.
      <T> Future<T> submit​(WithResources withResources, java.util.concurrent.Callable<T> call)
      Invoke task, returning a future representing this computation.
      protected void terminated()  
      • Methods inherited from class java.util.concurrent.ScheduledThreadPoolExecutor

        decorateTask, decorateTask, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy, shutdown
      • Methods inherited from class java.util.concurrent.ThreadPoolExecutor

        afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getKeepAliveTime, getLargestPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setKeepAliveTime, setRejectedExecutionHandler, setThreadFactory, toString
      • Methods inherited from class java.util.concurrent.AbstractExecutorService

        invokeAll, invokeAll, invokeAny, invokeAny
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.concurrent.ExecutorService

        awaitTermination, isShutdown, isTerminated, shutdown
    • Field Detail

      • rejectedExecutionHandler

        public static final java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler
    • Method Detail

      • schedule

        public java.util.concurrent.ScheduledFuture<?> schedule​(java.lang.Runnable task,
                                                                long delay,
                                                                java.util.concurrent.TimeUnit unit)
        Specified by:
        schedule in interface java.util.concurrent.ScheduledExecutorService
        Overrides:
        schedule in class java.util.concurrent.ScheduledThreadPoolExecutor
      • schedule

        public <V> java.util.concurrent.ScheduledFuture<V> schedule​(java.util.concurrent.Callable<V> task,
                                                                    long delay,
                                                                    java.util.concurrent.TimeUnit unit)
        Specified by:
        schedule in interface java.util.concurrent.ScheduledExecutorService
        Overrides:
        schedule in class java.util.concurrent.ScheduledThreadPoolExecutor
      • scheduleAtFixedRate

        public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(java.lang.Runnable task,
                                                                           long initialDelay,
                                                                           long period,
                                                                           java.util.concurrent.TimeUnit unit)
        Specified by:
        scheduleAtFixedRate in interface java.util.concurrent.ScheduledExecutorService
        Overrides:
        scheduleAtFixedRate in class java.util.concurrent.ScheduledThreadPoolExecutor
      • scheduleWithFixedDelay

        public java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay​(java.lang.Runnable task,
                                                                              long initialDelay,
                                                                              long delay,
                                                                              java.util.concurrent.TimeUnit unit)
        Specified by:
        scheduleWithFixedDelay in interface java.util.concurrent.ScheduledExecutorService
        Overrides:
        scheduleWithFixedDelay in class java.util.concurrent.ScheduledThreadPoolExecutor
      • scheduleSelfRecurring

        public java.util.concurrent.ScheduledFuture<?> scheduleSelfRecurring​(java.lang.Runnable run,
                                                                             long delay,
                                                                             java.util.concurrent.TimeUnit units)
        Description copied from interface: ScheduledExecutorPlus
        Schedule an action that is recurring but self-administered.
        Specified by:
        scheduleSelfRecurring in interface ScheduledExecutorPlus
      • scheduleAt

        public java.util.concurrent.ScheduledFuture<?> scheduleAt​(java.lang.Runnable run,
                                                                  long deadline)
        Description copied from interface: ScheduledExecutorPlus
        Schedule a timeout action. This method is primarily used by the Simulator to modify its scheduling behaviour with respect to this operation.
        Specified by:
        scheduleAt in interface ScheduledExecutorPlus
      • scheduleTimeoutAt

        public java.util.concurrent.ScheduledFuture<?> scheduleTimeoutAt​(java.lang.Runnable run,
                                                                         long deadline)
        Description copied from interface: ScheduledExecutorPlus
        Schedule a timeout action. This method is primarily used by the Simulator to modify its scheduling behaviour with respect to this operation.
        Specified by:
        scheduleTimeoutAt in interface ScheduledExecutorPlus
      • scheduleTimeoutWithDelay

        public java.util.concurrent.ScheduledFuture<?> scheduleTimeoutWithDelay​(java.lang.Runnable run,
                                                                                long delay,
                                                                                java.util.concurrent.TimeUnit units)
        Description copied from interface: ScheduledExecutorPlus
        Schedule a timeout action. This method is primarily used by the Simulator to modify its scheduling behaviour with respect to this operation.
        Specified by:
        scheduleTimeoutWithDelay in interface ScheduledExecutorPlus
      • execute

        public void execute​(java.lang.Runnable run)
        Specified by:
        execute in interface java.util.concurrent.Executor
        Overrides:
        execute in class java.util.concurrent.ScheduledThreadPoolExecutor
      • execute

        public void execute​(WithResources withResources,
                            java.lang.Runnable run)
        Description copied from interface: ExecutorPlus
        Invoke task. The invoking thread will first instantiate the resources provided before invoking task, so that thread state may be modified and cleaned up. The invoking thread will execute something semantically equivlent to: try (Closeable close = withResources.get()) { task.run(); }
        Specified by:
        execute in interface ExecutorPlus
        Parameters:
        withResources - the resources to create and hold while executing task
        run - the task to execute
      • submit

        public Future<?> submit​(java.lang.Runnable run)
        Description copied from interface: ExecutorPlus
        Overrides ExecutorService.submit(Runnable) to return a Cassandra Future
        Specified by:
        submit in interface ExecutorPlus
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
        Overrides:
        submit in class java.util.concurrent.ScheduledThreadPoolExecutor
      • submit

        public <T> Future<T> submit​(java.lang.Runnable run,
                                    T result)
        Description copied from interface: ExecutorPlus
        Overrides ExecutorService.submit(Runnable, Object) to return a Cassandra Future
        Specified by:
        submit in interface ExecutorPlus
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
        Overrides:
        submit in class java.util.concurrent.ScheduledThreadPoolExecutor
      • submit

        public <T> Future<T> submit​(java.util.concurrent.Callable<T> call)
        Description copied from interface: ExecutorPlus
        Overrides ExecutorService.submit(Callable) to return a Cassandra Future
        Specified by:
        submit in interface ExecutorPlus
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
        Overrides:
        submit in class java.util.concurrent.ScheduledThreadPoolExecutor
      • submit

        public <T> Future<T> submit​(WithResources withResources,
                                    java.lang.Runnable run,
                                    T result)
        Description copied from interface: ExecutorPlus
        Invoke task, returning a future yielding result if successful, or the abnormal termination of task otherwise. The invoking thread will first instantiate the resources provided before invoking task, so that thread state may be modified and cleaned up. The invoking thread will execute something semantically equivlent to: try (Closeable close = withResources.get()) { task.run(); return result; }
        Specified by:
        submit in interface ExecutorPlus
        Parameters:
        withResources - the resources to create and hold while executing task
        run - the task to execute
        result - the result if successful
      • submit

        public Future<?> submit​(WithResources withResources,
                                java.lang.Runnable run)
        Description copied from interface: ExecutorPlus
        Invoke task, returning a future yielding null if successful, or the abnormal termination of task otherwise. The invoking thread will first instantiate the resources provided before invoking task, so that thread state may be modified and cleaned up try (Closeable close = withResources.get()) { task.run(); return null; }
        Specified by:
        submit in interface ExecutorPlus
        Parameters:
        withResources - the resources to create and hold while executing task
        run - the task to execute
      • submit

        public <T> Future<T> submit​(WithResources withResources,
                                    java.util.concurrent.Callable<T> call)
        Description copied from interface: ExecutorPlus
        Invoke task, returning a future representing this computation. The invoking thread will first instantiate the resources provided before invoking task, so that thread state may be modified and cleaned up. The invoking thread will execute something semantically equivlent to: try (Closeable close = withResources.get()) { return task.call(); }
        Specified by:
        submit in interface ExecutorPlus
        Parameters:
        withResources - the resources to create and hold while executing task
        call - the task to execute
      • inExecutor

        public boolean inExecutor()
        Specified by:
        inExecutor in interface ExecutorPlus
        Returns:
        true iff the caller is a worker thread actively serving this executor
      • newTaskFor

        protected <T> RunnableFuture<T> newTaskFor​(java.lang.Runnable runnable,
                                                   T value)
        Overrides:
        newTaskFor in class java.util.concurrent.AbstractExecutorService
      • newTaskFor

        protected <T> RunnableFuture<T> newTaskFor​(java.util.concurrent.Callable<T> callable)
        Overrides:
        newTaskFor in class java.util.concurrent.AbstractExecutorService
      • getThreadFactory

        public NamedThreadFactory getThreadFactory()
        Overrides:
        getThreadFactory in class java.util.concurrent.ThreadPoolExecutor
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Specified by:
        shutdownNow in interface java.util.concurrent.ExecutorService
        Overrides:
        shutdownNow in class java.util.concurrent.ScheduledThreadPoolExecutor
      • terminated

        protected void terminated()
        Overrides:
        terminated in class java.util.concurrent.ThreadPoolExecutor
      • getActiveTaskCount

        public int getActiveTaskCount()
        Description copied from interface: ResizableThreadPool
        Returns the approximate number of threads that are actively executing tasks.
        Specified by:
        getActiveTaskCount in interface ResizableThreadPool
        Returns:
        the number of threads
      • getPendingTaskCount

        public int getPendingTaskCount()
        Description copied from interface: ResizableThreadPool
        Returns the approximate total of tasks waiting to be executed. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation.
        Specified by:
        getPendingTaskCount in interface ResizableThreadPool
        Returns:
        the number of tasks
      • getCorePoolSize

        public int getCorePoolSize()
        Description copied from interface: ResizableThreadPool
        Returns core pool size of thread pool, the minimum number of workers (where that makes sense for a thread pool, SEPExecutor does not have a minimum size).
        Specified by:
        getCorePoolSize in interface ResizableThreadPool
        Overrides:
        getCorePoolSize in class java.util.concurrent.ThreadPoolExecutor
      • setCorePoolSize

        public void setCorePoolSize​(int number)
        Description copied from interface: ResizableThreadPool
        Allows user to resize minimum size of the thread pool.
        Specified by:
        setCorePoolSize in interface ResizableThreadPool
        Overrides:
        setCorePoolSize in class java.util.concurrent.ThreadPoolExecutor
      • getMaximumPoolSize

        public int getMaximumPoolSize()
        Description copied from interface: ResizableThreadPool
        Returns maximum pool size of thread pool.
        Specified by:
        getMaximumPoolSize in interface ResizableThreadPool
        Overrides:
        getMaximumPoolSize in class java.util.concurrent.ThreadPoolExecutor
      • setMaximumPoolSize

        public void setMaximumPoolSize​(int number)
        Description copied from interface: ResizableThreadPool
        Allows user to resize maximum size of the thread pool.
        Specified by:
        setMaximumPoolSize in interface ResizableThreadPool
        Overrides:
        setMaximumPoolSize in class java.util.concurrent.ThreadPoolExecutor