public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor
ThreadPoolExecutor
in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity"
properties) and exposing it as a Spring TaskExecutor
.
This class is also well suited for management and monitoring (e.g. through JMX),
providing several useful attributes: "corePoolSize", "maxPoolSize", "keepAliveSeconds"
(all supporting updates at runtime); "poolSize", "activeCount" (for introspection only).
For an alternative, you may set up a ThreadPoolExecutor instance directly using
constructor injection, or use a factory method definition that points to the
Executors
class. To expose such a raw Executor as a
Spring TaskExecutor
, simply wrap it with a
ConcurrentTaskExecutor
adapter.
NOTE: This class implements Spring's
TaskExecutor
interface as well as the
Executor
interface, with the former being the primary
interface, the other just serving as secondary convenience. For this reason, the
exception handling follows the TaskExecutor contract rather than the Executor contract,
in particular regarding the TaskRejectedException
.
If you prefer native ExecutorService
exposure instead,
consider ThreadPoolExecutorFactoryBean
as an alternative to this class.
TaskExecutor
,
ThreadPoolExecutor
,
ConcurrentTaskExecutor
,
Serialized Formlogger
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
Constructor and Description |
---|
ThreadPoolTaskExecutor() |
Modifier and Type | Method and Description |
---|---|
protected void |
cancelRemainingTask(java.lang.Runnable task)
Cancel the given remaining task which never commended execution,
as returned from
ExecutorService.shutdownNow() . |
protected java.util.concurrent.BlockingQueue<java.lang.Runnable> |
createQueue(int queueCapacity)
Create the BlockingQueue to use for the ThreadPoolExecutor.
|
void |
execute(java.lang.Runnable task)
Execute the given
task . |
void |
execute(java.lang.Runnable task,
long startTimeout)
Execute the given
task . |
int |
getActiveCount()
Return the number of currently active threads.
|
int |
getCorePoolSize()
Return the ThreadPoolExecutor's core pool size.
|
int |
getKeepAliveSeconds()
Return the ThreadPoolExecutor's keep-alive seconds.
|
int |
getMaxPoolSize()
Return the ThreadPoolExecutor's maximum pool size.
|
int |
getPoolSize()
Return the current pool size.
|
java.util.concurrent.ThreadPoolExecutor |
getThreadPoolExecutor()
Return the underlying ThreadPoolExecutor for native access.
|
protected java.util.concurrent.ExecutorService |
initializeExecutor(java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
Note: This method exposes an
ExecutorService to its base class
but stores the actual ThreadPoolExecutor handle internally. |
boolean |
prefersShortLivedTasks()
This task executor prefers short-lived work units.
|
void |
setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Specify whether to allow core threads to time out.
|
void |
setCorePoolSize(int corePoolSize)
Set the ThreadPoolExecutor's core pool size.
|
void |
setKeepAliveSeconds(int keepAliveSeconds)
Set the ThreadPoolExecutor's keep-alive seconds.
|
void |
setMaxPoolSize(int maxPoolSize)
Set the ThreadPoolExecutor's maximum pool size.
|
void |
setQueueCapacity(int queueCapacity)
Set the capacity for the ThreadPoolExecutor's BlockingQueue.
|
void |
setTaskDecorator(TaskDecorator taskDecorator)
Specify a custom
TaskDecorator to be applied to any Runnable
about to be executed. |
<T> java.util.concurrent.Future<T> |
submit(java.util.concurrent.Callable<T> task)
Submit a Callable task for execution, receiving a Future representing that task.
|
java.util.concurrent.Future<?> |
submit(java.lang.Runnable task)
Submit a Runnable task for execution, receiving a Future representing that task.
|
<T> ListenableFuture<T> |
submitListenable(java.util.concurrent.Callable<T> task)
Submit a
Callable task for execution, receiving a ListenableFuture
representing that task. |
ListenableFuture<?> |
submitListenable(java.lang.Runnable task)
Submit a
Runnable task for execution, receiving a ListenableFuture
representing that task. |
afterPropertiesSet, destroy, initialize, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdown
newThread
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriority
public void setCorePoolSize(int corePoolSize)
This setting can be modified at runtime, for example through JMX.
public int getCorePoolSize()
public void setMaxPoolSize(int maxPoolSize)
Integer.MAX_VALUE
.
This setting can be modified at runtime, for example through JMX.
public int getMaxPoolSize()
public void setKeepAliveSeconds(int keepAliveSeconds)
This setting can be modified at runtime, for example through JMX.
public int getKeepAliveSeconds()
public void setQueueCapacity(int queueCapacity)
Integer.MAX_VALUE
.
Any positive value will lead to a LinkedBlockingQueue instance; any other value will lead to a SynchronousQueue instance.
LinkedBlockingQueue
,
SynchronousQueue
public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Default is "false".
ThreadPoolExecutor.allowCoreThreadTimeOut(boolean)
public void setTaskDecorator(TaskDecorator taskDecorator)
TaskDecorator
to be applied to any Runnable
about to be executed.
Note that such a decorator is not necessarily being applied to the
user-supplied Runnable
/Callable
but rather to the actual
execution callback (which may be a wrapper around the user-supplied task).
The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.
protected java.util.concurrent.ExecutorService initializeExecutor(java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
ExecutorService
to its base class
but stores the actual ThreadPoolExecutor
handle internally.
Do not override this method for replacing the executor, rather just for
decorating its ExecutorService
handle or storing custom state.initializeExecutor
in class ExecutorConfigurationSupport
threadFactory
- the ThreadFactory to userejectedExecutionHandler
- the RejectedExecutionHandler to useExecutorConfigurationSupport.afterPropertiesSet()
protected java.util.concurrent.BlockingQueue<java.lang.Runnable> createQueue(int queueCapacity)
A LinkedBlockingQueue instance will be created for a positive capacity value; a SynchronousQueue else.
queueCapacity
- the specified queue capacityLinkedBlockingQueue
,
SynchronousQueue
public java.util.concurrent.ThreadPoolExecutor getThreadPoolExecutor() throws java.lang.IllegalStateException
null
)java.lang.IllegalStateException
- if the ThreadPoolTaskExecutor hasn't been initialized yetpublic int getPoolSize()
ThreadPoolExecutor.getPoolSize()
public int getActiveCount()
ThreadPoolExecutor.getActiveCount()
public void execute(java.lang.Runnable task)
TaskExecutor
task
.
The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.
execute
in interface java.util.concurrent.Executor
execute
in interface TaskExecutor
task
- the Runnable
to execute (never null
)public void execute(java.lang.Runnable task, long startTimeout)
AsyncTaskExecutor
task
.execute
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)startTimeout
- the time duration (milliseconds) within which the task is
supposed to start. This is intended as a hint to the executor, allowing for
preferred handling of immediate tasks. Typical values are AsyncTaskExecutor.TIMEOUT_IMMEDIATE
or AsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used by TaskExecutor.execute(Runnable)
).public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
AsyncTaskExecutor
null
result upon completion.submit
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)public <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
AsyncTaskExecutor
submit
in interface AsyncTaskExecutor
task
- the Callable
to execute (never null
)public ListenableFuture<?> submitListenable(java.lang.Runnable task)
AsyncListenableTaskExecutor
Runnable
task for execution, receiving a ListenableFuture
representing that task. The Future will return a null
result upon completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Runnable
to execute (never null
)ListenableFuture
representing pending completion of the taskpublic <T> ListenableFuture<T> submitListenable(java.util.concurrent.Callable<T> task)
AsyncListenableTaskExecutor
Callable
task for execution, receiving a ListenableFuture
representing that task. The Future will return the Callable's result upon
completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Callable
to execute (never null
)ListenableFuture
representing pending completion of the taskprotected void cancelRemainingTask(java.lang.Runnable task)
ExecutorConfigurationSupport
ExecutorService.shutdownNow()
.cancelRemainingTask
in class ExecutorConfigurationSupport
task
- the task to cancel (typically a RunnableFuture
)ExecutorConfigurationSupport.shutdown()
,
Future.cancel(boolean)
public boolean prefersShortLivedTasks()
prefersShortLivedTasks
in interface SchedulingTaskExecutor
true
if this TaskExecutor
prefers
short-lived tasks