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

      • HYSTERESIS

        protected static final float HYSTERESIS
        The hysteresis applying to going below the threshold. This will not be signalled until the amount is below (1 - HYSTERESIS) * threshold. The hysteresis prevents continuous logging while going around the threshold amount.
        The hysteresis will only apply if the threshold is big enough, e. g. a threshold of 5 and a hysteresis of 0.2 is the normal behaviour of differing by 1.
        See Also:
        Constant Field Values
      • 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 queueLimitPoolStatistics)
        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.
        queueLimitPoolStatistics - 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 queueLimitPoolStatistics,
                                    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.
        queueLimitPoolStatistics - 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 queueLimitPoolStatistics)
        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.
        queueLimitPoolStatistics - 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 queueLimitPoolStatistics,
                                    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.
        queueLimitPoolStatistics - 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.
    • Method Detail

      • updateQueueLimit

        public void updateQueueLimit​(int limit)
        Update the queue limit to inform the thread pool about a changed situation, i. e. the initial worklist update is completed.
        Parameters:
        limit - The new queue limit.
      • recalculateLogLimits

        protected void recalculateLogLimits()
        Recalculates the log thresholds, i. e. the check size, and half of it. These have to be recalculated when changing the queue limit or the core or maximum pool size.

        Make sure to have the synchronized-monitor of thresholdSync while executing this method.

      • logPoolStatistics

        protected void logPoolStatistics​(Runnable command,
                                         boolean afterExecute)
        Checks whether to log the current pool statistics depending on whether a threshold has been reached (from below or above). The log level depends on the threshold.
        Parameters:
        command - The command which is about to be executed or has been executed.
        afterExecute - Whether the call is after the designated command has been executed (or right before it is executed).
      • setLogExecuteCallStack

        public boolean setLogExecuteCallStack​(boolean logExecuteCallStack)
        Sets whether to log the stack of the caller of execute(Runnable). This allows to identify which code parts have enqueued some asynchronous work. The logger requires Level.FINE for this.
        Parameters:
        logExecuteCallStack - Whether to log the stack of the caller of execute(Runnable).
        Returns:
        The previous value of logExecuteCallStack.