Package org.jboss.threads
Class EnhancedQueueExecutor
java.lang.Object
java.util.concurrent.AbstractExecutorService
org.jboss.threads.EnhancedQueueExecutor
- All Implemented Interfaces:
AutoCloseable,Executor,ExecutorService,ScheduledExecutorService,ManageableThreadPoolExecutorService
public final class EnhancedQueueExecutor
extends AbstractExecutorService
implements ManageableThreadPoolExecutorService, ScheduledExecutorService
A task-or-thread queue backed thread pool executor service. Tasks are added in a FIFO manner, and consumers in a LIFO manner.
Threads are only ever created in the event that there are no idle threads available to service a task, which, when
combined with the LIFO-based consumer behavior, means that the thread pool will generally trend towards the minimum
necessary size. In addition, the optional growth resistance feature can
be used to further govern the thread pool size.
Additionally, this thread pool implementation supports scheduling of tasks. The scheduled tasks will execute on the main pool.
New instances of this thread pool are created by constructing and configuring a EnhancedQueueExecutor.Builder instance, and calling
its build() method.
- Author:
- David M. Lloyd
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe builder class for anEnhancedQueueExecutor. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanA global hint which establishes whether it is recommended to disable uses ofEnhancedQueueExecutor. -
Method Summary
Modifier and TypeMethodDescriptionvoidallowCoreThreadTimeOut(boolean value) Establish whether core threads are allowed to time out.booleanDetermine whether core threads are allowed to time out.booleanawaitTermination(long timeout, TimeUnit unit) Wait for the thread pool to complete termination.voidExecute a task.intGet an estimate of the number of threads which are currently doing work on behalf of the thread pool.longGet an estimate of the number of tasks completed by this thread pool.intGet the core pool size.Get the exception handler to use for uncaught exceptions.floatGet the thread pool growth resistance.Get the executor to delegate to in the event of task rejection.Get the thread keep-alive time.longgetKeepAliveTime(TimeUnit keepAliveUnits) Deprecated.intGet an estimate of the peak number of threads that the pool has ever held.intGet an estimate of the peak size of the queue.intGet the maximum pool size.intGet the maximum queue size.intGet an estimate of the current number of active threads in the pool.intGet an estimate of the current queue size.longGet an estimate of the total number of tasks ever rejected by this thread pool for any reason.Thread[]Get an array containing an approximate snapshot of the currently running threads in this executor.longGet an estimate of the total number of tasks ever submitted to this thread pool.Create or acquire an MXBean instance for this thread pool.booleanDetermine whether shutdown was requested on this thread pool.booleanDetermine whether shutdown has completed on this thread pool.booleanDetermine if this thread pool is in the process of terminating but has not yet completed.intStart all core threads.booleanStart an idle core thread.<V> ScheduledFuture<V> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) voidsetCorePoolSize(int corePoolSize) Set the core pool size.voidsetExceptionHandler(Thread.UncaughtExceptionHandler exceptionHandler) Set the exception handler to use for uncaught exceptions.voidsetGrowthResistance(float growthResistance) Set the growth resistance factor.voidsetHandoffExecutor(Executor handoffExecutor) Set the executor to delegate to in the event of task rejection.voidsetKeepAliveTime(long keepAliveTime, TimeUnit keepAliveUnits) Deprecated.UsesetKeepAliveTime(Duration)instead.voidsetKeepAliveTime(Duration keepAliveTime) Set the thread keep-alive time.voidsetMaximumPoolSize(int maxPoolSize) Set the maximum pool size.voidsetMaximumQueueSize(int maxQueueSize) Set the maximum queue size.voidsetTerminationTask(Runnable terminationTask) Set the termination task, overwriting any previous setting.voidshutdown()Request that shutdown be initiated for this thread pool.voidshutdown(boolean interrupt) Initiate shutdown of this thread pool.Attempt to stop the thread pool immediately by interrupting all running threads and de-queueing all pending tasks.Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
-
Field Details
-
DISABLE_HINT
public static final boolean DISABLE_HINTA global hint which establishes whether it is recommended to disable uses ofEnhancedQueueExecutor. This hint defaults tofalsebut can be changed totrueby setting thejboss.threads.eqe.disableproperty totruebefore this class is initialized.
-
-
Method Details
-
execute
Execute a task. -
shutdown
public void shutdown()Request that shutdown be initiated for this thread pool. This is equivalent to callingshutdown(false); see that method for more information.- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
Attempt to stop the thread pool immediately by interrupting all running threads and de-queueing all pending tasks. The thread pool might not be fully stopped when this method returns, if a currently running task does not respect interruption.- Specified by:
shutdownNowin interfaceExecutorService- Returns:
- a list of pending tasks (not
null)
-
isShutdown
public boolean isShutdown()Determine whether shutdown was requested on this thread pool.- Specified by:
isShutdownin interfaceExecutorService- Returns:
trueif shutdown was requested,falseotherwise
-
isTerminated
public boolean isTerminated()Determine whether shutdown has completed on this thread pool.- Specified by:
isTerminatedin interfaceExecutorService- Returns:
trueif shutdown has completed,falseotherwise
-
awaitTermination
Wait for the thread pool to complete termination. If the timeout expires before the thread pool is terminated,falseis returned. If the calling thread is interrupted before the thread pool is terminated, then anInterruptedExceptionis thrown.- Specified by:
awaitTerminationin interfaceExecutorService- Parameters:
timeout- the amount of time to wait (must be ≥ 0)unit- the unit of time to use for waiting (must not benull)- Returns:
trueif the thread pool terminated within the given timeout,falseotherwise- Throws:
InterruptedException- if the calling thread was interrupted before either the time period elapsed or the pool terminated successfully
-
schedule
- Specified by:
schedulein interfaceScheduledExecutorService
-
schedule
- Specified by:
schedulein interfaceScheduledExecutorService
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) - Specified by:
scheduleAtFixedRatein interfaceScheduledExecutorService
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) - Specified by:
scheduleWithFixedDelayin interfaceScheduledExecutorService
-
getThreadPoolMXBean
Description copied from interface:ManageableThreadPoolExecutorServiceCreate or acquire an MXBean instance for this thread pool. Note that the thread pool itself will not do anything in particular to register (or unregister) the MXBean with a JMX server; that is the caller's responsibility.- Specified by:
getThreadPoolMXBeanin interfaceManageableThreadPoolExecutorService- Returns:
- the MXBean instance (must not be
null)
-
shutdown
public void shutdown(boolean interrupt) Initiate shutdown of this thread pool. After this method is called, no new tasks will be accepted. Once the last task is complete, the thread pool will be terminated and its termination task will be called. Calling this method more than once has no additional effect, unless all previous calls had theinterruptparameter set tofalseand the subsequent call sets it totrue, in which case all threads in the pool will be interrupted.- Parameters:
interrupt-trueto request that currently-running tasks be interrupted;falseotherwise
-
isTerminating
public boolean isTerminating()Determine if this thread pool is in the process of terminating but has not yet completed.- Returns:
trueif the thread pool is terminating, orfalseif the thread pool is not terminating or has completed termination
-
prestartCoreThread
public boolean prestartCoreThread()Start an idle core thread. Normally core threads are only begun when a task was submitted to the executor but no thread is immediately available to handle the task.- Returns:
trueif a core thread was started, orfalseif all core threads were already running or if the thread failed to be created for some other reason
-
prestartAllCoreThreads
public int prestartAllCoreThreads()Start all core threads. CallsprestartCoreThread()in a loop until it returnsfalse.- Returns:
- the number of core threads created
-
getGrowthResistance
public float getGrowthResistance()Get the thread pool growth resistance. This is the average fraction of submitted tasks that will be enqueued (instead of causing a new thread to start) when there are no idle threads and the pool size is equal to or greater than the core size (but still less than the maximum size). A value of0.0indicates that tasks should not be enqueued until the pool is completely full; a value of1.0indicates that tasks should always be enqueued until the queue is completely full.- Returns:
- the configured growth resistance factor
- See Also:
-
setGrowthResistance
public void setGrowthResistance(float growthResistance) Set the growth resistance factor.- Parameters:
growthResistance- the thread pool growth resistance (must be in the range0.0f ≤ n ≤ 1.0f)- See Also:
-
getCorePoolSize
public int getCorePoolSize()Get the core pool size. This is the size below which new threads will always be created if no idle threads are available. If the pool size reaches the core size but has not yet reached the maximum size, a resistance factor will be applied to each task submission which determines whether the task should be queued or a new thread started.- Returns:
- the core pool size
- See Also:
-
setCorePoolSize
public void setCorePoolSize(int corePoolSize) Set the core pool size. If the configured maximum pool size is less than the configured core size, the core size will be reduced to match the maximum size when the thread pool is constructed.- Parameters:
corePoolSize- the core pool size (must be greater than or equal to 0, and less than 2^20)- See Also:
-
getMaximumPoolSize
public int getMaximumPoolSize()Get the maximum pool size. This is the absolute upper limit to the size of the thread pool.- Returns:
- the maximum pool size
- See Also:
-
setMaximumPoolSize
public void setMaximumPoolSize(int maxPoolSize) Set the maximum pool size. If the configured maximum pool size is less than the configured core size, the core size will be reduced to match the maximum size when the thread pool is constructed.- Parameters:
maxPoolSize- the maximum pool size (must be greater than or equal to 0, and less than 2^20)- See Also:
-
allowsCoreThreadTimeOut
public boolean allowsCoreThreadTimeOut()Determine whether core threads are allowed to time out. A "core thread" is defined as any thread in the pool when the pool size is below the pool's core pool size.- Returns:
trueif core threads are allowed to time out,falseotherwise- See Also:
-
allowCoreThreadTimeOut
public void allowCoreThreadTimeOut(boolean value) Establish whether core threads are allowed to time out. A "core thread" is defined as any thread in the pool when the pool size is below the pool's core pool size.- Parameters:
value-trueif core threads are allowed to time out,falseotherwise- See Also:
-
getKeepAliveTime
Deprecated.UsegetKeepAliveTime()instead.Get the thread keep-alive time. This is the minimum length of time that idle threads should remain until they exit. Unless core threads are allowed to time out, threads will only exit if the current thread count exceeds the core limit.- Parameters:
keepAliveUnits- the unit in which the result should be expressed (must not benull)- Returns:
- the amount of time (will be greater than zero)
- See Also:
-
getKeepAliveTime
Get the thread keep-alive time. This is the minimum length of time that idle threads should remain until they exit. Unless core threads are allowed to time out, threads will only exit if the current thread count exceeds the core limit.- Returns:
- the amount of time (will be greater than zero)
- See Also:
-
setKeepAliveTime
Deprecated.UsesetKeepAliveTime(Duration)instead.Set the thread keep-alive time. This is the minimum length of time that idle threads should remain until they exit. Unless core threads are allowed to time out, threads will only exit if the current thread count exceeds the core limit.- Parameters:
keepAliveTime- the thread keep-alive time (must be > 0)keepAliveUnits- the unit in which the value is expressed (must not benull)- See Also:
-
setKeepAliveTime
Set the thread keep-alive time. This is the minimum length of time that idle threads should remain until they exit. Unless core threads are allowed to time out, threads will only exit if the current thread count exceeds the core limit.- Parameters:
keepAliveTime- the thread keep-alive time (must not benull)- See Also:
-
getMaximumQueueSize
public int getMaximumQueueSize()Get the maximum queue size. If the queue is full and a task cannot be immediately accepted, rejection will result.- Returns:
- the maximum queue size
- See Also:
-
setMaximumQueueSize
public void setMaximumQueueSize(int maxQueueSize) Set the maximum queue size. If the new maximum queue size is smaller than the current queue size, there is no effect other than preventing tasks from being enqueued until the size decreases below the maximum again.- Parameters:
maxQueueSize- the maximum queue size (must be ≥ 0)- See Also:
-
getHandoffExecutor
Get the executor to delegate to in the event of task rejection.- Returns:
- the executor to delegate to in the event of task rejection (not
null)
-
setHandoffExecutor
Set the executor to delegate to in the event of task rejection.- Parameters:
handoffExecutor- the executor to delegate to in the event of task rejection (must not benull)
-
getExceptionHandler
Get the exception handler to use for uncaught exceptions.- Returns:
- the exception handler to use for uncaught exceptions (not
null)
-
setExceptionHandler
Set the exception handler to use for uncaught exceptions.- Parameters:
exceptionHandler- the exception handler to use for uncaught exceptions (must not benull)
-
setTerminationTask
Set the termination task, overwriting any previous setting.- Parameters:
terminationTask- the termination task, ornullto perform no termination task
-
getQueueSize
public int getQueueSize()Get an estimate of the current queue size.- Returns:
- an estimate of the current queue size or -1 when
jboss.threads.eqe.unlimited-queueis enabled
-
getLargestPoolSize
public int getLargestPoolSize()Get an estimate of the peak number of threads that the pool has ever held.- Returns:
- an estimate of the peak number of threads that the pool has ever held
-
getActiveCount
public int getActiveCount()Get an estimate of the number of threads which are currently doing work on behalf of the thread pool.- Returns:
- the active count estimate or -1 when
jboss.threads.eqe.statistics.active-countis disabled
-
getLargestQueueSize
public int getLargestQueueSize()Get an estimate of the peak size of the queue. return an estimate of the peak size of the queue or -1 whenjboss.threads.eqe.statisticsis disabled orjboss.threads.eqe.unlimited-queueis enabled -
getSubmittedTaskCount
public long getSubmittedTaskCount()Get an estimate of the total number of tasks ever submitted to this thread pool.- Returns:
- an estimate of the total number of tasks ever submitted to this thread pool
or -1 when
jboss.threads.eqe.statisticsis disabled
-
getRejectedTaskCount
public long getRejectedTaskCount()Get an estimate of the total number of tasks ever rejected by this thread pool for any reason.- Returns:
- an estimate of the total number of tasks ever rejected by this thread pool
or -1 when
jboss.threads.eqe.statisticsis disabled
-
getCompletedTaskCount
public long getCompletedTaskCount()Get an estimate of the number of tasks completed by this thread pool.- Returns:
- an estimate of the number of tasks completed by this thread pool
or -1 when
jboss.threads.eqe.statisticsis disabled
-
getPoolSize
public int getPoolSize()Get an estimate of the current number of active threads in the pool.- Returns:
- an estimate of the current number of active threads in the pool
-
getRunningThreads
Get an array containing an approximate snapshot of the currently running threads in this executor.- Returns:
- an array of running (unterminated) threads (not
null)
-
getKeepAliveTime()instead.