org.springframework.scheduling.concurrent

## 类 ThreadPoolTaskExecutor

- java.lang.Object
- - org.springframework.util.CustomizableThreadCreator
  - - org.springframework.scheduling.concurrent.CustomizableThreadFactory
    - - org.springframework.scheduling.concurrent.ExecutorConfigurationSupport
      - - org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

- 所有已实现的接口:
    Serializable, Executor, ThreadFactory, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.core.task.AsyncListenableTaskExecutor, org.springframework.core.task.AsyncTaskExecutor, org.springframework.core.task.TaskExecutor, SchedulingTaskExecutor

      public class ThreadPoolTaskExecutor
      extends ExecutorConfigurationSupport
      implements org.springframework.core.task.AsyncListenableTaskExecutor, SchedulingTaskExecutor

  JavaBean that allows for configuring a `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.

  从以下版本开始:
    2.0
  作者:
    Juergen Hoeller
  另请参阅:
    `TaskExecutor`, `ThreadPoolExecutor`, `ThreadPoolExecutorFactoryBean`, `ConcurrentTaskExecutor`, 序列化表格

- - ### 字段概要

    - ### 从类继承的字段 org.springframework.scheduling.concurrent.ExecutorConfigurationSupport

      `logger`

    - ### 从接口继承的字段 org.springframework.core.task.AsyncTaskExecutor

      `TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE`

  - ### 构造器概要

    构造器
    构造器和说明
    `ThreadPoolTaskExecutor()`

  - ### 方法概要

    所有方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `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)`
    `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.
    `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(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)`

    - ### 从类继承的方法 org.springframework.scheduling.concurrent.ExecutorConfigurationSupport

      `afterPropertiesSet, destroy, initialize, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdown`

    - ### 从类继承的方法 org.springframework.scheduling.concurrent.CustomizableThreadFactory

      `newThread`

    - ### 从类继承的方法 org.springframework.util.CustomizableThreadCreator

      `createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriority`

    - ### 从类继承的方法 java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 构造器详细资料

    - #### ThreadPoolTaskExecutor

          public ThreadPoolTaskExecutor()

  - ### 方法详细资料

    - #### setCorePoolSize

          public void setCorePoolSize(int corePoolSize)

      Set the ThreadPoolExecutor's core pool size. Default is 1.

      This setting can be modified at runtime, for example through JMX.

    - #### getCorePoolSize

          public int getCorePoolSize()

      Return the ThreadPoolExecutor's core pool size.

    - #### setMaxPoolSize

          public void setMaxPoolSize(int maxPoolSize)

      Set the ThreadPoolExecutor's maximum pool size. Default is `Integer.MAX_VALUE`.

      This setting can be modified at runtime, for example through JMX.

    - #### getMaxPoolSize

          public int getMaxPoolSize()

      Return the ThreadPoolExecutor's maximum pool size.

    - #### setKeepAliveSeconds

          public void setKeepAliveSeconds(int keepAliveSeconds)

      Set the ThreadPoolExecutor's keep-alive seconds. Default is 60.

      This setting can be modified at runtime, for example through JMX.

    - #### getKeepAliveSeconds

          public int getKeepAliveSeconds()

      Return the ThreadPoolExecutor's keep-alive seconds.

    - #### setQueueCapacity

          public void setQueueCapacity(int queueCapacity)

      Set the capacity for the ThreadPoolExecutor's BlockingQueue. Default is `Integer.MAX_VALUE`.

      Any positive value will lead to a LinkedBlockingQueue instance; any other value will lead to a SynchronousQueue instance.

      另请参阅:
        `LinkedBlockingQueue`, `SynchronousQueue`

    - #### setAllowCoreThreadTimeOut

          public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)

      Specify whether to allow core threads to time out. This enables dynamic growing and shrinking even in combination with a non-zero queue (since the max pool size will only grow once the queue is full).

      Default is "false".

      另请参阅:
        `ThreadPoolExecutor.allowCoreThreadTimeOut(boolean)`

    - #### setTaskDecorator

          public void setTaskDecorator(org.springframework.core.task.TaskDecorator taskDecorator)

      Specify a custom `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.

      从以下版本开始:
        4.3

    - #### initializeExecutor

          protected ExecutorService initializeExecutor(ThreadFactory threadFactory,
                                                       RejectedExecutionHandler rejectedExecutionHandler)

      Note: This method exposes an `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` 在类中 `ExecutorConfigurationSupport`
      参数:
        `threadFactory` - the ThreadFactory to use
        `rejectedExecutionHandler` - the RejectedExecutionHandler to use
      返回:
        a new ExecutorService instance
      另请参阅:
        `ExecutorConfigurationSupport.afterPropertiesSet()`

    - #### createQueue

          protected BlockingQueue<Runnable> createQueue(int queueCapacity)

      Create the BlockingQueue to use for the ThreadPoolExecutor.

      A LinkedBlockingQueue instance will be created for a positive capacity value; a SynchronousQueue else.

      参数:
        `queueCapacity` - the specified queue capacity
      返回:
        the BlockingQueue instance
      另请参阅:
        `LinkedBlockingQueue`, `SynchronousQueue`

    - #### getThreadPoolExecutor

          public ThreadPoolExecutor getThreadPoolExecutor()
                                                   throws IllegalStateException

      Return the underlying ThreadPoolExecutor for native access.
      返回:
        the underlying ThreadPoolExecutor (never `null`)
      抛出:
        `IllegalStateException` - if the ThreadPoolTaskExecutor hasn't been initialized yet

    - #### getPoolSize

          public int getPoolSize()

      Return the current pool size.
      另请参阅:
        `ThreadPoolExecutor.getPoolSize()`

    - #### getActiveCount

          public int getActiveCount()

      Return the number of currently active threads.
      另请参阅:
        `ThreadPoolExecutor.getActiveCount()`

    - #### execute

          public void execute(Runnable task)

      指定者:
        `execute` 在接口中 `Executor`
      指定者:
        `execute` 在接口中 `org.springframework.core.task.TaskExecutor`

    - #### execute

          public void execute(Runnable task,
                              long startTimeout)

      指定者:
        `execute` 在接口中 `org.springframework.core.task.AsyncTaskExecutor`

    - #### submit

          public Future<?> submit(Runnable task)

      指定者:
        `submit` 在接口中 `org.springframework.core.task.AsyncTaskExecutor`

    - #### submit

          public <T> Future<T> submit(Callable<T> task)

      指定者:
        `submit` 在接口中 `org.springframework.core.task.AsyncTaskExecutor`

    - #### submitListenable

          public org.springframework.util.concurrent.ListenableFuture<?> submitListenable(Runnable task)

      指定者:
        `submitListenable` 在接口中 `org.springframework.core.task.AsyncListenableTaskExecutor`

    - #### submitListenable

          public <T> org.springframework.util.concurrent.ListenableFuture<T> submitListenable(Callable<T> task)

      指定者:
        `submitListenable` 在接口中 `org.springframework.core.task.AsyncListenableTaskExecutor`

    - #### cancelRemainingTask

          protected void cancelRemainingTask(Runnable task)

      从类复制的说明: `ExecutorConfigurationSupport`
      Cancel the given remaining task which never commended execution, as returned from `ExecutorService.shutdownNow()`.
      覆盖:
        `cancelRemainingTask` 在类中 `ExecutorConfigurationSupport`
      参数:
        `task` - the task to cancel (typically a `RunnableFuture`)
      另请参阅:
        `ExecutorConfigurationSupport.shutdown()`, `Future.cancel(boolean)`

    - #### prefersShortLivedTasks

          public boolean prefersShortLivedTasks()

      This task executor prefers short-lived work units.
      指定者:
        `prefersShortLivedTasks` 在接口中 `SchedulingTaskExecutor`
      返回:
        `true` if this `TaskExecutor` prefers short-lived tasks