public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor
ThreadPoolExecutor を Bean スタイルで (その "corePoolSize"、"maxPoolSize"、"keepAliveSeconds"、"queueCapacity" プロパティを介して) 構成し、それを Spring TaskExecutor として公開することを可能にします。このクラスは、管理と監視 (JMX 経由など) にも適しており、いくつかの便利な属性を提供します: "corePoolSize"、"maxPoolSize"、"keepAliveSeconds" (すべて実行時の更新をサポート)、"poolSize"、"activeCount" (イントロスペクションのみ)。 別の方法として、コンストラクターインジェクションを使用して ThreadPoolExecutor インスタンスを直接セットアップするか、Executors クラスを指すファクトリメソッド定義を使用することができます。このような未加工のエグゼキュータを Spring TaskExecutor として公開するには、ConcurrentTaskExecutor アダプターでラップするだけです。
注意 : このクラスは、Spring の TaskExecutor インターフェースと Executor インターフェースを実装します。前者はプライマリインターフェースであり、もう一方はセカンダリの利便性として機能します。このため、例外処理は、特に TaskRejectedException に関して、Executor 契約ではなく TaskExecutor 契約に従います。
代わりにネイティブ ExecutorService 露出を好む場合は、このクラスの代わりに ThreadPoolExecutorFactoryBean を検討してください。
TaskExecutor, ThreadPoolExecutor, ConcurrentTaskExecutor, 連載形式 loggerTIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE| コンストラクターと説明 |
|---|
ThreadPoolTaskExecutor() |
| 修飾子と型 | メソッドと説明 |
|---|---|
protected void | cancelRemainingTask(java.lang.Runnable task)ExecutorService.shutdownNow() から返された、実行を決して賞賛しなかった残りのタスクをキャンセルします。 |
protected java.util.concurrent.BlockingQueue<java.lang.Runnable> | createQueue(int queueCapacity)BlockingQueue を作成して、ThreadPoolExecutor に使用します。 |
void | execute(java.lang.Runnable task) 指定された task を実行します。 |
void | execute(java.lang.Runnable task, long startTimeout) 指定された task を実行します。 |
int | getActiveCount() 現在アクティブなスレッドの数を返します。 |
int | getCorePoolSize()ThreadPoolExecutor のコアプールサイズを返します。 |
int | getKeepAliveSeconds()ThreadPoolExecutor のキープアライブ秒数を返します。 |
int | getMaxPoolSize()ThreadPoolExecutor の最大プールサイズを返します。 |
int | getPoolSize() 現在のプールサイズを返します。 |
java.util.concurrent.ThreadPoolExecutor | getThreadPoolExecutor() ネイティブアクセスの基になる ThreadPoolExecutor を返します。 |
protected java.util.concurrent.ExecutorService | initializeExecutor(java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler) メモ: このメソッドは、 ExecutorService をその基本クラスに公開しますが、実際の ThreadPoolExecutor ハンドルを内部に格納します。 |
boolean | prefersShortLivedTasks() このタスクエグゼキュータは、短期間のワークユニットを優先します。 |
void | setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) コアスレッドのタイムアウトを許可するかどうかを指定します。 |
void | setCorePoolSize(int corePoolSize)ThreadPoolExecutor のコアプールサイズを設定します。 |
void | setKeepAliveSeconds(int keepAliveSeconds)ThreadPoolExecutor のキープアライブ秒数を設定します。 |
void | setMaxPoolSize(int maxPoolSize)ThreadPoolExecutor の最大プールサイズを設定します。 |
void | setQueueCapacity(int queueCapacity)ThreadPoolExecutor の BlockingQueue の容量を設定します。 |
void | setTaskDecorator(TaskDecorator taskDecorator) 実行される Runnable に適用されるカスタム TaskDecorator を指定します。 |
<T> java.util.concurrent.Future<T> | submit(java.util.concurrent.Callable<T> task)Callable タスクを送信して実行し、そのタスクを表す Future を受け取ります。 |
java.util.concurrent.Future<?> | submit(java.lang.Runnable task) 実行可能な Runnable タスクを送信し、そのタスクを表す Future を受け取ります。 |
<T> ListenableFuture<T> | submitListenable(java.util.concurrent.Callable<T> task)Callable タスクを実行のために送信し、そのタスクを表す ListenableFuture を受け取ります。 |
ListenableFuture<?> | submitListenable(java.lang.Runnable task)Runnable タスクを実行のために送信し、そのタスクを表す ListenableFuture を受け取ります。 |
afterPropertiesSet, destroy, initialize, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdownnewThreadcreateThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPrioritypublic void setCorePoolSize(int corePoolSize)
この設定は、JMX などを介して実行時に変更できます。
public int getCorePoolSize()
public void setMaxPoolSize(int maxPoolSize)
Integer.MAX_VALUE です。この設定は、JMX などを介して実行時に変更できます。
public int getMaxPoolSize()
public void setKeepAliveSeconds(int keepAliveSeconds)
この設定は、JMX などを介して実行時に変更できます。
public int getKeepAliveSeconds()
public void setQueueCapacity(int queueCapacity)
Integer.MAX_VALUE です。正の値は、LinkedBlockingQueue インスタンスにつながります。その他の値は、SynchronousQueue インスタンスにつながります。
LinkedBlockingQueue, SynchronousQueuepublic void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
デフォルトは「false」。
ThreadPoolExecutor.allowCoreThreadTimeOut(boolean)public void setTaskDecorator(TaskDecorator taskDecorator)
Runnable に適用されるカスタム TaskDecorator を指定します。 このようなデコレータは、必ずしもユーザー提供の Runnable/Callable に適用されるのではなく、実際の実行コールバック(ユーザー提供のタスクのラッパーである可能性があります)に適用されることに注意してください。
主な使用例は、タスクの呼び出しの周囲に実行コンテキストを設定するか、タスク実行の監視 / 統計を提供することです。
protected java.util.concurrent.ExecutorService initializeExecutor(java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)ExecutorService をその基本クラスに公開しますが、実際の ThreadPoolExecutor ハンドルを内部に格納します。エグゼキュータを置き換えるためにこのメソッドをオーバーライドしないでください。ExecutorService ハンドルを装飾したり、カスタム状態を保存したりするためだけに使用してください。ExecutorConfigurationSupport の initializeExecutor threadFactory - 使用する ThreadFactoryrejectedExecutionHandler - 使用する RejectedExecutionHandlerExecutorConfigurationSupport.afterPropertiesSet()protected java.util.concurrent.BlockingQueue<java.lang.Runnable> createQueue(int queueCapacity)
正の容量値に対して LinkedBlockingQueue インスタンスが作成されます。SynchronousQueue その他。
queueCapacity - 指定されたキュー容量 LinkedBlockingQueue, SynchronousQueuepublic java.util.concurrent.ThreadPoolExecutor getThreadPoolExecutor()
throws java.lang.IllegalStateExceptionnull)java.lang.IllegalStateException - ThreadPoolTaskExecutor がまだ初期化されていない場合 public int getPoolSize()
ThreadPoolExecutor.getPoolSize()public int getActiveCount()
ThreadPoolExecutor.getActiveCount()public void execute(java.lang.Runnable task)
TaskExecutortask を実行します。実装が非同期実行戦略を使用している場合、呼び出しはすぐに戻るか、同期実行の場合はブロックする可能性があります。
java.util.concurrent.Executor の execute TaskExecutor の execute task - 実行する Runnable (非 null)public void execute(java.lang.Runnable task,
long startTimeout)AsyncTaskExecutortask を実行します。AsyncTaskExecutor の execute task - 実行する Runnable (非 null)startTimeout - タスクが開始される予定の期間(ミリ秒)。これはエグゼキューターへのヒントとして意図されており、即時タスクの優先処理を可能にします。一般的な値は AsyncTaskExecutor.TIMEOUT_IMMEDIATE または AsyncTaskExecutor.TIMEOUT_INDEFINITE(TaskExecutor.execute(Runnable) で使用されるデフォルト)です。public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
AsyncTaskExecutornull の結果を返します。AsyncTaskExecutor の submit task - 実行する Runnable (非 null)public <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
AsyncTaskExecutorAsyncTaskExecutor の submit task - 実行する Callable (非 null)public ListenableFuture<?> submitListenable(java.lang.Runnable task)
AsyncListenableTaskExecutorRunnable タスクを実行のために送信し、そのタスクを表す ListenableFuture を受け取ります。Future は完了時に null の結果を返します。AsyncListenableTaskExecutor の submitListenable task - 実行する Runnable (非 null)ListenableFuture public <T> ListenableFuture<T> submitListenable(java.util.concurrent.Callable<T> task)
AsyncListenableTaskExecutorCallable タスクを実行のために送信し、そのタスクを表す ListenableFuture を受け取ります。Future は、完了時に Callable の結果を返します。AsyncListenableTaskExecutor の submitListenable task - 実行する Callable (非 null)ListenableFuture protected void cancelRemainingTask(java.lang.Runnable task)
ExecutorConfigurationSupportExecutorService.shutdownNow() から返された、実行を決して賞賛しなかった残りのタスクをキャンセルします。ExecutorConfigurationSupport の cancelRemainingTask task - キャンセルするタスク (通常は RunnableFuture)ExecutorConfigurationSupport.shutdown(), Future.cancel(boolean)public boolean prefersShortLivedTasks()
SchedulingTaskExecutor の prefersShortLivedTasks true は、この TaskExecutor が短期タスクを好む場合