public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport implements org.springframework.core.task.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).
The default configuration is a core pool size of 1, with unlimited max pool size
and unlimited queue capacity. This is roughly equivalent to
Executors.newSingleThreadExecutor(), sharing a single
thread for all tasks. Setting "queueCapacity" to 0 mimics
Executors.newCachedThreadPool(), with immediate scaling
of threads in the pool to a potentially very high number. Consider also setting a
"maxPoolSize" at that point, as well as possibly a higher
"corePoolSize" (see also the
"allowCoreThreadTimeOut" mode of scaling).
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.
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.
TaskExecutor,
ThreadPoolExecutor,
ThreadPoolExecutorFactoryBean,
ConcurrentTaskExecutor,
Serialized Formlogger| Constructor and Description |
|---|
ThreadPoolTaskExecutor() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
cancelRemainingTask(Runnable task)
Cancel the given remaining task which never commended execution,
as returned from
ExecutorService.shutdownNow(). |
protected BlockingQueue<Runnable> |
createQueue(int queueCapacity)
Create the BlockingQueue to use for the ThreadPoolExecutor.
|
void |
execute(Runnable task) |
void |
execute(Runnable task,
long startTimeout)
Deprecated.
|
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.
|
ThreadPoolExecutor |
getThreadPoolExecutor()
Return the underlying ThreadPoolExecutor for native access.
|
protected ExecutorService |
initializeExecutor(ThreadFactory threadFactory,
RejectedExecutionHandler rejectedExecutionHandler)
Note: This method exposes an
ExecutorService to its base class
but stores the actual ThreadPoolExecutor handle internally. |
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 |
setPrestartAllCoreThreads(boolean prestartAllCoreThreads)
Specify whether to start all core threads, causing them to idly wait for work.
|
void |
setQueueCapacity(int queueCapacity)
Set the capacity for the ThreadPoolExecutor's BlockingQueue.
|
void |
setTaskDecorator(org.springframework.core.task.TaskDecorator taskDecorator)
Specify a custom
TaskDecorator to be applied to any Runnable
about to be executed. |
<T> Future<T> |
submit(Callable<T> task) |
Future<?> |
submit(Runnable task) |
<T> org.springframework.util.concurrent.ListenableFuture<T> |
submitListenable(Callable<T> task) |
org.springframework.util.concurrent.ListenableFuture<?> |
submitListenable(Runnable task) |
afterPropertiesSet, destroy, initialize, setAwaitTerminationMillis, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdownnewThreadcreateThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriorityclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitprefersShortLivedTaskspublic 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,
SynchronousQueuepublic void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Default is "false".
public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads)
Default is "false".
ThreadPoolExecutor.prestartAllCoreThreads()public void setTaskDecorator(org.springframework.core.task.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.
NOTE: Exception handling in TaskDecorator implementations
is limited to plain Runnable execution via execute calls.
In case of #submit calls, the exposed Runnable will be a
FutureTask which does not propagate any exceptions; you might
have to cast it and call Future#get to evaluate exceptions.
See the ThreadPoolExecutor#afterExecute javadoc for an example
of how to access exceptions in such a Future case.
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, 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 ExecutorConfigurationSupportthreadFactory - the ThreadFactory to userejectedExecutionHandler - the RejectedExecutionHandler to useExecutorConfigurationSupport.afterPropertiesSet()protected BlockingQueue<Runnable> createQueue(int queueCapacity)
A LinkedBlockingQueue instance will be created for a positive capacity value; a SynchronousQueue else.
queueCapacity - the specified queue capacityLinkedBlockingQueue,
SynchronousQueuepublic ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException
null)IllegalStateException - if the ThreadPoolTaskExecutor hasn't been initialized yetpublic int getPoolSize()
ThreadPoolExecutor.getPoolSize()public int getActiveCount()
ThreadPoolExecutor.getActiveCount()public void execute(Runnable task)
@Deprecated public void execute(Runnable task, long startTimeout)
execute in interface org.springframework.core.task.AsyncTaskExecutorpublic Future<?> submit(Runnable task)
submit in interface org.springframework.core.task.AsyncTaskExecutorpublic <T> Future<T> submit(Callable<T> task)
submit in interface org.springframework.core.task.AsyncTaskExecutorpublic org.springframework.util.concurrent.ListenableFuture<?> submitListenable(Runnable task)
submitListenable in interface org.springframework.core.task.AsyncListenableTaskExecutorpublic <T> org.springframework.util.concurrent.ListenableFuture<T> submitListenable(Callable<T> task)
submitListenable in interface org.springframework.core.task.AsyncListenableTaskExecutorprotected void cancelRemainingTask(Runnable task)
ExecutorConfigurationSupportExecutorService.shutdownNow().cancelRemainingTask in class ExecutorConfigurationSupporttask - the task to cancel (typically a RunnableFuture)ExecutorConfigurationSupport.shutdown(),
Future.cancel(boolean)