Class AFThreadPoolExecutor

  • All Implemented Interfaces:
    Executor, ExecutorService
    Direct Known Subclasses:
    AbstractPipeliningExecutor, CachedThreadPoolExecutor

    public class AFThreadPoolExecutor
    extends ThreadPoolExecutor
    This thread pool executor enhances a normal thread pool by:
    • prepending the state of a thread to the thread name,
    • logging exceptions (and results) of futures where no one has been interested in and
    • logging statistics.
    Thread names will be prefixed as following:
    • NEW: Used for newly created threads.
    • ACT: Used for active threads that are currently executing tasks.
    • IDL: Used for idle threads, i.e. threads that may be reused.
    Every task is wrapped by a LoggingFuture. A LoggingFuture logs an occurred exception if no one retrieved the exception. This way the exception will not be lost. Additionally, it will log if the result is relevant but no one retrieved it.
    Statistics logging will take place if the size of the queue reaches the number of allowed threads. Logs will be repeated when reaching a size that is multiple of the limit. For single thread pools the statistics will be logged, if the size of the queue is larger than 100. This allows to recognise thread pools, whose configuration may be optimised.
    • Field Detail

      • submittedTasks

        protected final AtomicLong submittedTasks
        The amount of all tasks that have been submitted (known via execute(Runnable).
      • completedTasks

        protected final AtomicLong completedTasks
        The amount of all tasks that have been completed.
      • activeThreads

        protected final AtomicLong activeThreads
        The amount of active threads. A thread counts as active from right before executing a task until right after executing a tasks. The amount is the same as the amount of threads of which the name starts with "[ACT]".
    • Constructor Detail

      • AFThreadPoolExecutor

        public AFThreadPoolExecutor​(int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    Adept2ThreadFactory threadFactory,
                                    int queueLimit)
        Creates a new AFThreadPoolExecutor with the given initial parameters and default rejected execution handler. Name and logger of the Adept2ThreadFactory will be used to log statistics.
        Parameters:
        corePoolSize - the number of threads to keep in the pool, even if they are idle.
        maximumPoolSize - the maximum number of threads to allow in the pool.
        keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
        unit - the time unit for the keepAliveTime argument.
        workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
        threadFactory - the factory to use when the executor creates a new thread.
        queueLimit - The amount of tasks in the queue before warnings will be logged. Use -1 to use the default limits.
        Throws:
        IllegalArgumentException - if corePoolSize or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.
        NullPointerException - if workQueue or threadFactory are null.
      • AFThreadPoolExecutor

        public AFThreadPoolExecutor​(int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    Adept2ThreadFactory threadFactory,
                                    int queueLimit,
                                    Level minLogLevelPoolStatistics)
        Creates a new AFThreadPoolExecutor with the given initial parameters and default rejected execution handler. Name and logger of the Adept2ThreadFactory will be used to log statistics.
        Parameters:
        corePoolSize - the number of threads to keep in the pool, even if they are idle.
        maximumPoolSize - the maximum number of threads to allow in the pool.
        keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
        unit - the time unit for the keepAliveTime argument.
        workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
        threadFactory - the factory to use when the executor creates a new thread.
        queueLimit - The amount of tasks in the queue before warnings will be logged. Use -1 to use the default limits.
        minLogLevelPoolStatistics - The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
        Throws:
        IllegalArgumentException - if corePoolSize or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.
        NullPointerException - if workQueue or threadFactory are null.
      • AFThreadPoolExecutor

        public AFThreadPoolExecutor​(int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    Adept2ThreadFactory threadFactory,
                                    RejectedExecutionHandler handler,
                                    int queueLimit)
        Creates a new ThreadPoolExecutor with the given initial parameters. Name and logger of the Adept2ThreadFactory will be used to log statistics.
        Parameters:
        corePoolSize - the number of threads to keep in the pool, even if they are idle.
        maximumPoolSize - the maximum number of threads to allow in the pool.
        keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
        unit - the time unit for the keepAliveTime argument.
        workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
        threadFactory - the factory to use when the executor creates a new thread.
        handler - the handler to use when execution is blocked because the thread bounds and queue capacities are reached.
        queueLimit - The amount of tasks in the queue before warnings will be logged. Use -1 to use the default limits.
        Throws:
        IllegalArgumentException - if corePoolSize or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.
        NullPointerException - if workQueue or threadFactory or handler are null.
      • AFThreadPoolExecutor

        public AFThreadPoolExecutor​(int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    Adept2ThreadFactory threadFactory,
                                    RejectedExecutionHandler handler,
                                    int queueLimit,
                                    Level minLogLevelPoolStatistics)
        Creates a new ThreadPoolExecutor with the given initial parameters. Name and logger of the Adept2ThreadFactory will be used to log statistics.
        Parameters:
        corePoolSize - the number of threads to keep in the pool, even if they are idle.
        maximumPoolSize - the maximum number of threads to allow in the pool.
        keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
        unit - the time unit for the keepAliveTime argument.
        workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
        threadFactory - the factory to use when the executor creates a new thread.
        handler - the handler to use when execution is blocked because the thread bounds and queue capacities are reached.
        queueLimit - The amount of tasks in the queue before warnings will be logged. Use -1 to use the default limits.
        minLogLevelPoolStatistics - The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
        Throws:
        IllegalArgumentException - if corePoolSize or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.
        NullPointerException - if workQueue or threadFactory or handler are null.