クラス ThreadPoolTaskExecutor
- 実装されているすべてのインターフェース:
SerializableSE、ExecutorSE、ThreadFactorySE、Aware、BeanNameAware、DisposableBean、InitializingBean、AsyncListenableTaskExecutor、AsyncTaskExecutor、TaskExecutor、SchedulingTaskExecutor
ThreadPoolExecutorSE を Bean スタイルで (その "corePoolSize"、"maxPoolSize"、"keepAliveSeconds"、"queueCapacity" プロパティを介して) 構成し、それを Spring TaskExecutor として公開することを可能にします。このクラスは、管理と監視 (JMX 経由など) にも適しており、いくつかの便利な属性を提供します: "corePoolSize"、"maxPoolSize"、"keepAliveSeconds" (すべて実行時の更新をサポート)、"poolSize"、"activeCount" (イントロスペクションのみ)。 デフォルトの構成はコアプールサイズ 1 で、最大プールサイズとキュー容量は無制限です。これは、すべてのタスクで単一のスレッドを共有する Executors.newSingleThreadExecutor() とほぼ同等です。"queueCapacity" を 0 に設定すると、Executors.newCachedThreadPool() を模倣し、プール内のスレッドを潜在的に非常に高い数に即座にスケーリングします。また、その時点で "maxPoolSize" を設定し、場合によってはより高い "corePoolSize" を設定することも検討してください(スケーリングの "allowCoreThreadTimeOut" モードも参照してください)。
注意 : このクラスは、Spring の TaskExecutor インターフェースと ExecutorSE インターフェースを実装します。前者はプライマリインターフェースであり、もう一方は単に二次的な利便性として機能します。このため、例外処理は、特に TaskRejectedException に関して、Executor 契約ではなく TaskExecutor 契約に従います。
別の方法として、コンストラクターインジェクションを使用して ThreadPoolExecutor インスタンスを直接セットアップするか、ExecutorsSE クラスを指すファクトリメソッド定義を使用することができます。このような生のエグゼキュータを Spring TaskExecutor として公開するには、単に ConcurrentTaskExecutor アダプターでラップします。
- 導入:
- 2.0
- 作成者:
- Juergen Hoeller, R é my Guihard, Sam Brannen
- 関連事項:
フィールドのサマリー
クラス org.springframework.scheduling.concurrent.ExecutorConfigurationSupport から継承されたフィールド
loggerインターフェース org.springframework.core.task.AsyncTaskExecutor から継承されたフィールド
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITEコンストラクターの概要
コンストラクターメソッドのサマリー
修飾子と型メソッド説明protected voidExecutorService.shutdownNow()から返された、実行を開始していない指定された残りのタスクをキャンセルします。protected BlockingQueueSE<RunnableSE>createQueue(int queueCapacity) BlockingQueue を作成して、ThreadPoolExecutor に使用します。voidexecute(RunnableSE task) 指定されたtaskを実行します。voidexecute(RunnableSE task, long startTimeout) 使用すべきではありません。int現在アクティブなスレッドの数を返します。intThreadPoolExecutor のコアプールサイズを返します。intThreadPoolExecutor のキープアライブ秒数を返します。intThreadPoolExecutor の最大プールサイズを返します。int現在のプールサイズを返します。intThreadPoolExecutor の BlockingQueue の容量を返します。int現在のキューサイズを返します。ネイティブアクセスの基になる ThreadPoolExecutor を返します。protected ExecutorServiceSEinitializeExecutor(ThreadFactorySE threadFactory, RejectedExecutionHandlerSE rejectedExecutionHandler) メモ: このメソッドは、ExecutorServiceSE をその基本クラスに公開しますが、実際のThreadPoolExecutorSE ハンドルを内部に保存します。voidsetAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) コアスレッドのタイムアウトを許可するかどうかを指定します。voidsetCorePoolSize(int corePoolSize) ThreadPoolExecutor のコアプールサイズを設定します。voidsetKeepAliveSeconds(int keepAliveSeconds) ThreadPoolExecutor のキープアライブ秒数を設定します。voidsetMaxPoolSize(int maxPoolSize) ThreadPoolExecutor の最大プールサイズを設定します。voidsetPrestartAllCoreThreads(boolean prestartAllCoreThreads) すべてのコアスレッドを開始して、アイドル状態で作業を待機させるかどうかを指定します。voidsetQueueCapacity(int queueCapacity) ThreadPoolExecutor の BlockingQueue の容量を設定します。voidsetTaskDecorator(TaskDecorator taskDecorator) 実行しようとしているRunnableSE に適用するカスタムTaskDecoratorを指定します。FutureSE<?>submit(RunnableSE task) 実行可能な Runnable タスクを送信し、そのタスクを表す Future を受け取ります。<T> FutureSE<T>submit(CallableSE<T> task) Callable タスクを送信して実行し、そのタスクを表す Future を受け取ります。submitListenable(RunnableSE task) Runnableタスクを実行のために送信し、そのタスクを表すListenableFutureを受け取ります。<T> ListenableFuture<T>submitListenable(CallableSE<T> task) Callableタスクを実行のために送信し、そのタスクを表すListenableFutureを受け取ります。クラス org.springframework.scheduling.concurrent.ExecutorConfigurationSupport から継承されたメソッド
afterPropertiesSet, destroy, initialize, setAwaitTerminationMillis, 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.ObjectSE から継承されたメソッド
clone, equalsSE, finalize, getClass, hashCode, notify, notifyAll, toString, wait, waitSE, waitSEインターフェース org.springframework.core.task.AsyncTaskExecutor から継承されたメソッド
submitCompletable, submitCompletableインターフェース org.springframework.scheduling.SchedulingTaskExecutor から継承されたメソッド
prefersShortLivedTasks
コンストラクターの詳細
ThreadPoolTaskExecutor
public ThreadPoolTaskExecutor()
メソッドの詳細
setCorePoolSize
public void setCorePoolSize(int corePoolSize) ThreadPoolExecutor のコアプールサイズを設定します。デフォルトは 1 です。この設定は、JMX などを介して実行時に変更できます。
getCorePoolSize
public int getCorePoolSize()ThreadPoolExecutor のコアプールサイズを返します。setMaxPoolSize
public void setMaxPoolSize(int maxPoolSize) ThreadPoolExecutor の最大プールサイズを設定します。デフォルトはInteger.MAX_VALUEです。この設定は、JMX などを介して実行時に変更できます。
getMaxPoolSize
public int getMaxPoolSize()ThreadPoolExecutor の最大プールサイズを返します。setKeepAliveSeconds
public void setKeepAliveSeconds(int keepAliveSeconds) ThreadPoolExecutor のキープアライブ秒数を設定します。デフォルトは 60 です。
この設定は、JMX などを介して実行時に変更できます。
getKeepAliveSeconds
public int getKeepAliveSeconds()ThreadPoolExecutor のキープアライブ秒数を返します。setQueueCapacity
public void setQueueCapacity(int queueCapacity) ThreadPoolExecutor の BlockingQueue の容量を設定します。デフォルトは
Integer.MAX_VALUEです。正の値は、LinkedBlockingQueue インスタンスにつながります。その他の値は、SynchronousQueue インスタンスにつながります。
getQueueCapacity
public int getQueueCapacity()ThreadPoolExecutor の BlockingQueue の容量を返します。- 導入:
- 5.3.21
- 関連事項:
setAllowCoreThreadTimeOut
public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) コアスレッドのタイムアウトを許可するかどうかを指定します。これにより、ゼロ以外のキューと組み合わせても動的な拡大と縮小が可能になります(最大プールサイズはキューがいっぱいになると拡大するため)。デフォルトは「false」。
setPrestartAllCoreThreads
public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads) すべてのコアスレッドを開始して、アイドル状態で作業を待機させるかどうかを指定します。デフォルトは「false」。
- 導入:
- 5.3.14
- 関連事項:
setTaskDecorator
実行しようとしているRunnableSE に適用するカスタムTaskDecoratorを指定します。このようなデコレータは、必ずしもユーザー提供の
Runnable/Callableに適用されるのではなく、実際の実行コールバック(ユーザー提供のタスクのラッパーである可能性があります)に適用されることに注意してください。主な使用例は、タスクの呼び出しの周囲に実行コンテキストを設定するか、タスク実行の監視 / 統計を提供することです。
注意 :
TaskDecorator実装での例外処理は、execute呼び出しによる単純なRunnable実行に制限されています。#submit呼び出しの場合、公開されたRunnableは、例外を伝搬しないFutureTaskになります。例外を評価するには、キャストしてFuture#getを呼び出す必要がある場合があります。このようなFutureケースで例外にアクセスする方法の例については、ThreadPoolExecutor#afterExecutejavadoc を参照してください。- 導入:
- 4.3
initializeExecutor
protected ExecutorServiceSE initializeExecutor(ThreadFactorySE threadFactory, RejectedExecutionHandlerSE rejectedExecutionHandler) メモ: このメソッドは、ExecutorServiceSE をその基本クラスに公開しますが、実際のThreadPoolExecutorSE ハンドルを内部に保存します。このメソッドをオーバーライドして、executor を置き換えるのではなく、単にExecutorServiceハンドルを装飾したり、カスタム状態を保管したりしてください。- 次で指定:
- クラス
ExecutorConfigurationSupportのinitializeExecutor - パラメーター:
threadFactory- 使用する ThreadFactoryrejectedExecutionHandler- 使用する RejectedExecutionHandler- 戻り値:
- 新しい ExecutorService インスタンス
- 関連事項:
createQueue
BlockingQueue を作成して、ThreadPoolExecutor に使用します。正の容量値に対して LinkedBlockingQueue インスタンスが作成されます。SynchronousQueue その他。
- パラメーター:
queueCapacity- 指定されたキュー容量- 戻り値:
- BlockingQueue インスタンス
- 関連事項:
getThreadPoolExecutor
ネイティブアクセスの基になる ThreadPoolExecutor を返します。- 戻り値:
- 基になる ThreadPoolExecutor (非
null) - 例外:
IllegalStateExceptionSE- ThreadPoolTaskExecutor がまだ初期化されていない場合
getPoolSize
public int getPoolSize()現在のプールサイズを返します。getQueueSize
public int getQueueSize()現在のキューサイズを返します。- 導入:
- 5.3.21
- 関連事項:
getActiveCount
public int getActiveCount()現在アクティブなスレッドの数を返します。execute
インターフェースからコピーされた説明:TaskExecutor指定されたtaskを実行します。実装が非同期実行戦略を使用している場合、呼び出しはすぐに戻るか、同期実行の場合はブロックする可能性があります。
- 次で指定:
- インターフェース
ExecutorSEのexecuteSE - 次で指定:
- インターフェース
TaskExecutorのexecute - パラメーター:
task- 実行するRunnable(非null)
execute
使用すべきではありません。インターフェースからコピーされた説明:AsyncTaskExecutor指定されたtaskを実行します。- 次で指定:
- インターフェース
AsyncTaskExecutorのexecute - パラメーター:
task- 実行するRunnable(非null)startTimeout- タスクが開始される予定の期間(ミリ秒)。これはエグゼキューターへのヒントとして意図されており、即時タスクの優先処理を可能にします。一般的な値はAsyncTaskExecutor.TIMEOUT_IMMEDIATEまたはAsyncTaskExecutor.TIMEOUT_INDEFINITE(TaskExecutor.execute(Runnable)で使用されるデフォルト)です。- 関連事項:
submit
インターフェースからコピーされた説明:AsyncTaskExecutor実行可能な Runnable タスクを送信し、そのタスクを表す Future を受け取ります。Future は完了時にnullの結果を返します。- 次で指定:
- インターフェース
AsyncTaskExecutorのsubmit - パラメーター:
task- 実行するRunnable(非null)- 戻り値:
- タスクの保留中の完了を表す Future
submit
インターフェースからコピーされた説明:AsyncTaskExecutorCallable タスクを送信して実行し、そのタスクを表す Future を受け取ります。Future は、完了時に Callable の結果を返します。- 次で指定:
- インターフェース
AsyncTaskExecutorのsubmit - パラメーター:
task- 実行するCallable(非null)- 戻り値:
- タスクの保留中の完了を表す Future
submitListenable
インターフェースからコピーされた説明:AsyncListenableTaskExecutorRunnableタスクを実行のために送信し、そのタスクを表すListenableFutureを受け取ります。Future は完了時にnullの結果を返します。- 次で指定:
- インターフェース
AsyncListenableTaskExecutorのsubmitListenable - パラメーター:
task- 実行するRunnable(非null)- 戻り値:
- 保留中のタスクの完了を表す
ListenableFuture
submitListenable
インターフェースからコピーされた説明:AsyncListenableTaskExecutorCallableタスクを実行のために送信し、そのタスクを表すListenableFutureを受け取ります。Future は、完了時に Callable の結果を返します。- 次で指定:
- インターフェース
AsyncListenableTaskExecutorのsubmitListenable - パラメーター:
task- 実行するCallable(非null)- 戻り値:
- 保留中のタスクの完了を表す
ListenableFuture
cancelRemainingTask
クラスからコピーされた説明:ExecutorConfigurationSupportExecutorService.shutdownNow()から返された、実行を開始していない指定された残りのタスクをキャンセルします。- オーバーライド:
- クラス
ExecutorConfigurationSupportのcancelRemainingTask - パラメーター:
task- キャンセルするタスク (通常、RunnableFutureSE )- 関連事項: