Class AFThreadPoolExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- de.aristaflow.adept2.util.threading.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.
- 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.
LoggingFuture. ALoggingFuturelogs 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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
-
-
Field Summary
Fields Modifier and Type Field Description protected AtomicLongactiveThreadsThe amount of active threads.protected AtomicLongcompletedTasksThe amount of all tasks that have been completed.protected static floatHYSTERESISThe hysteresis applying to going below the threshold.protected AtomicLongsubmittedTasksThe amount of all tasks that have been submitted (known viaexecute(Runnable).
-
Constructor Summary
Constructors Constructor Description AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics)Creates a newAFThreadPoolExecutorwith the given initial parameters and default rejected execution handler.AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics)Creates a newAFThreadPoolExecutorwith the given initial parameters and default rejected execution handler.AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, RejectedExecutionHandler handler, int queueLimitPoolStatistics)Creates a newThreadPoolExecutorwith the given initial parameters.AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, RejectedExecutionHandler handler, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics)Creates a newThreadPoolExecutorwith the given initial parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterExecute(Runnable r, Throwable t)protected voidbeforeExecute(Thread t, Runnable r)voidexecute(Runnable command)protected voidlogPoolStatistics(Runnable command, boolean afterExecute)Checks whether to log the current pool statistics depending on whether a threshold has been reached (from below or above).protected <T> RunnableFuture<T>newTaskFor(Runnable runnable, T value)protected <T> RunnableFuture<T>newTaskFor(Callable<T> callable)protected voidrecalculateLogLimits()Recalculates the log thresholds, i. e.voidsetCorePoolSize(int corePoolSize)booleansetLogExecuteCallStack(boolean logExecuteCallStack)Sets whether to log the stack of the caller ofexecute(Runnable).voidsetMaximumPoolSize(int maximumPoolSize)voidupdateQueueLimit(int limit)Update the queue limit to inform the thread pool about a changed situation, i. e.-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setKeepAliveTime, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toString
-
-
-
-
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 viaexecute(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 newAFThreadPoolExecutorwith the given initial parameters and default rejected execution handler. Name and logger of theAdept2ThreadFactorywill 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 theRunnabletasks submitted by theexecutemethod.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-1to 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- ifworkQueueorthreadFactoryare null.
-
AFThreadPoolExecutor
public AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics)Creates a newAFThreadPoolExecutorwith the given initial parameters and default rejected execution handler. Name and logger of theAdept2ThreadFactorywill 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 theRunnabletasks submitted by theexecutemethod.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-1to 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- ifworkQueueorthreadFactoryare null.
-
AFThreadPoolExecutor
public AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, RejectedExecutionHandler handler, int queueLimitPoolStatistics)Creates a newThreadPoolExecutorwith the given initial parameters. Name and logger of theAdept2ThreadFactorywill 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 theRunnabletasks submitted by theexecutemethod.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-1to 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- ifworkQueueorthreadFactoryorhandlerare 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 newThreadPoolExecutorwith the given initial parameters. Name and logger of theAdept2ThreadFactorywill 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 theRunnabletasks submitted by theexecutemethod.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-1to 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- ifworkQueueorthreadFactoryorhandlerare null.
-
-
Method Detail
-
beforeExecute
protected void beforeExecute(Thread t, Runnable r)
- Overrides:
beforeExecutein classThreadPoolExecutor
-
execute
public void execute(Runnable command)
- Specified by:
executein interfaceExecutor- Overrides:
executein classThreadPoolExecutor
-
newTaskFor
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value)
- Overrides:
newTaskForin classAbstractExecutorService
-
newTaskFor
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable)
- Overrides:
newTaskForin classAbstractExecutorService
-
afterExecute
protected void afterExecute(Runnable r, Throwable t)
- Overrides:
afterExecutein classThreadPoolExecutor
-
setCorePoolSize
public void setCorePoolSize(int corePoolSize)
- Overrides:
setCorePoolSizein classThreadPoolExecutor
-
setMaximumPoolSize
public void setMaximumPoolSize(int maximumPoolSize)
- Overrides:
setMaximumPoolSizein classThreadPoolExecutor
-
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 ofthresholdSyncwhile 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 ofexecute(Runnable). This allows to identify which code parts have enqueued some asynchronous work. The logger requiresLevel.FINEfor this.- Parameters:
logExecuteCallStack- Whether to log the stack of the caller ofexecute(Runnable).- Returns:
- The previous value of
logExecuteCallStack.
-
-