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
. ALoggingFuture
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.
-
-
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 AtomicLong
activeThreads
The amount of active threads.protected AtomicLong
completedTasks
The amount of all tasks that have been completed.protected static float
HYSTERESIS
The hysteresis applying to going below the threshold.protected AtomicLong
submittedTasks
The 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 newAFThreadPoolExecutor
with 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 newAFThreadPoolExecutor
with 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 newThreadPoolExecutor
with 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 newThreadPoolExecutor
with the given initial parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterExecute(Runnable r, Throwable t)
protected void
beforeExecute(Thread t, Runnable r)
void
execute(Runnable command)
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).protected <T> RunnableFuture<T>
newTaskFor(Runnable runnable, T value)
protected <T> RunnableFuture<T>
newTaskFor(Callable<T> callable)
protected void
recalculateLogLimits()
Recalculates the log thresholds, i. e. the check size, and half of it.void
setCorePoolSize(int corePoolSize)
boolean
setLogExecuteCallStack(boolean logExecuteCallStack)
Sets whether to log the stack of the caller ofexecute(Runnable)
.void
setMaximumPoolSize(int maximumPoolSize)
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.-
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 newAFThreadPoolExecutor
with the given initial parameters and default rejected execution handler. Name and logger of theAdept2ThreadFactory
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 theRunnable
tasks submitted by theexecute
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
- ifworkQueue
orthreadFactory
are null.
-
AFThreadPoolExecutor
public AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics)
Creates a newAFThreadPoolExecutor
with the given initial parameters and default rejected execution handler. Name and logger of theAdept2ThreadFactory
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 theRunnable
tasks submitted by theexecute
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
- ifworkQueue
orthreadFactory
are null.
-
AFThreadPoolExecutor
public AFThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, Adept2ThreadFactory threadFactory, RejectedExecutionHandler handler, int queueLimitPoolStatistics)
Creates a newThreadPoolExecutor
with the given initial parameters. Name and logger of theAdept2ThreadFactory
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 theRunnable
tasks submitted by theexecute
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
- ifworkQueue
orthreadFactory
orhandler
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 newThreadPoolExecutor
with the given initial parameters. Name and logger of theAdept2ThreadFactory
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 theRunnable
tasks submitted by theexecute
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
- ifworkQueue
orthreadFactory
orhandler
are null.
-
-
Method Detail
-
beforeExecute
protected void beforeExecute(Thread t, Runnable r)
- Overrides:
beforeExecute
in classThreadPoolExecutor
-
execute
public void execute(Runnable command)
- Specified by:
execute
in interfaceExecutor
- Overrides:
execute
in classThreadPoolExecutor
-
newTaskFor
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value)
- Overrides:
newTaskFor
in classAbstractExecutorService
-
newTaskFor
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable)
- Overrides:
newTaskFor
in classAbstractExecutorService
-
afterExecute
protected void afterExecute(Runnable r, Throwable t)
- Overrides:
afterExecute
in classThreadPoolExecutor
-
setCorePoolSize
public void setCorePoolSize(int corePoolSize)
- Overrides:
setCorePoolSize
in classThreadPoolExecutor
-
setMaximumPoolSize
public void setMaximumPoolSize(int maximumPoolSize)
- Overrides:
setMaximumPoolSize
in 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 ofthresholdSync
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 ofexecute(Runnable)
. This allows to identify which code parts have enqueued some asynchronous work. The logger requiresLevel.FINE
for this.- Parameters:
logExecuteCallStack
- Whether to log the stack of the caller ofexecute(Runnable)
.- Returns:
- The previous value of
logExecuteCallStack
.
-
-