public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor
ThreadPoolExecutor
SE を構成し( "corePoolSize"、"maxPoolSize"、"keepAliveSeconds"、"queueCapacity" プロパティを使用して)、Spring TaskExecutor
として公開できる JavaBean。このクラスは、管理および監視(JMX など)にも適しています。いくつかの有用な属性を提供します。"corePoolSize"、"maxPoolSize"、"keepAliveSeconds" (実行時のすべての更新をサポート)。"poolSize"、"activeCount" (イントロスペクションのみ)。 デフォルトの構成はコアプールサイズ 1 で、最大プールサイズとキュー容量は無制限です。これは、すべてのタスクで単一のスレッドを共有する Executors.newSingleThreadExecutor()
SE とほぼ同等です。"queueCapacity"
を 0 に設定すると、Executors.newCachedThreadPool()
SE を模倣し、プール内のスレッドを潜在的に非常に高い数に即座にスケーリングします。また、その時点で "maxPoolSize"
を設定し、場合によってはより高い "corePoolSize"
を設定することも検討してください(スケーリングの "allowCoreThreadTimeOut"
モードも参照してください)。
注意 : このクラスは、Spring の TaskExecutor
インターフェースと Executor
SE インターフェースを実装します。前者はプライマリインターフェースであり、もう一方は単に二次的な利便性として機能します。このため、例外処理は、特に TaskRejectedException
に関して、Executor 契約ではなく TaskExecutor 契約に従います。
別の方法として、コンストラクターインジェクションを使用して ThreadPoolExecutor インスタンスを直接セットアップするか、Executors
SE クラスを指すファクトリメソッド定義を使用することができます。このような生のエグゼキュータを Spring TaskExecutor
として公開するには、単に ConcurrentTaskExecutor
アダプターでラップします。
TaskExecutor
, ThreadPoolExecutor
SE, ThreadPoolExecutorFactoryBean
, ConcurrentTaskExecutor
, 直列化された形式 logger
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
コンストラクターと説明 |
---|
ThreadPoolTaskExecutor() |
修飾子と型 | メソッドと説明 |
---|---|
protected void | cancelRemainingTask(RunnableSE task) ExecutorService.shutdownNow() SE から返された、実行を決して推奨しない残りのタスクをキャンセルします。 |
protected BlockingQueueSE<RunnableSE> | createQueue(int queueCapacity) BlockingQueue を作成して、ThreadPoolExecutor に使用します。 |
void | execute(RunnableSE task) 指定された task を実行します。 |
void | execute(RunnableSE task, long startTimeout) 指定された task を実行します。 |
int | getActiveCount() 現在アクティブなスレッドの数を返します。 |
int | getCorePoolSize() ThreadPoolExecutor のコアプールサイズを返します。 |
int | getKeepAliveSeconds() ThreadPoolExecutor のキープアライブ秒数を返します。 |
int | getMaxPoolSize() ThreadPoolExecutor の最大プールサイズを返します。 |
int | getPoolSize() 現在のプールサイズを返します。 |
ThreadPoolExecutorSE | getThreadPoolExecutor() ネイティブアクセスの基になる ThreadPoolExecutor を返します。 |
protected ExecutorServiceSE | initializeExecutor(ThreadFactorySE threadFactory, RejectedExecutionHandlerSE rejectedExecutionHandler) メモ: このメソッドは、 ExecutorService SE をその基本クラスに公開しますが、実際の ThreadPoolExecutor SE ハンドルを内部に保存します。 |
void | setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) コアスレッドのタイムアウトを許可するかどうかを指定します。 |
void | setCorePoolSize(int corePoolSize) ThreadPoolExecutor のコアプールサイズを設定します。 |
void | setKeepAliveSeconds(int keepAliveSeconds) ThreadPoolExecutor のキープアライブ秒数を設定します。 |
void | setMaxPoolSize(int maxPoolSize) ThreadPoolExecutor の最大プールサイズを設定します。 |
void | setPrestartAllCoreThreads(boolean prestartAllCoreThreads) すべてのコアスレッドを開始して、アイドル状態で作業を待機させるかどうかを指定します。 |
void | setQueueCapacity(int queueCapacity) ThreadPoolExecutor の BlockingQueue の容量を設定します。 |
void | setTaskDecorator(TaskDecorator taskDecorator) 実行しようとしている Runnable SE に適用するカスタム TaskDecorator を指定します。 |
<T> FutureSE<T> | submit(CallableSE<T> task) Callable タスクを送信して実行し、そのタスクを表す Future を受け取ります。 |
FutureSE<?> | submit(RunnableSE task) 実行可能な Runnable タスクを送信し、そのタスクを表す Future を受け取ります。 |
<T> ListenableFuture<T> | submitListenable(CallableSE<T> task) Callable タスクを実行のために送信し、そのタスクを表す ListenableFuture を受け取ります。 |
ListenableFuture<?> | submitListenable(RunnableSE task) Runnable タスクを実行のために送信し、そのタスクを表す ListenableFuture を受け取ります。 |
afterPropertiesSet, destroy, initialize, setAwaitTerminationMillis, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdown
newThread
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriority
cloneSE, equalsSE, finalizeSE, getClassSE, hashCodeSE, notifySE, notifyAllSE, toStringSE, waitSE, waitSE, waitSE
prefersShortLivedTasks
public 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 インスタンスにつながります。
public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
デフォルトは「false」。
public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads)
デフォルトは「false」。
ThreadPoolExecutor.prestartAllCoreThreads()
SEpublic void setTaskDecorator(TaskDecorator taskDecorator)
Runnable
SE に適用するカスタム TaskDecorator
を指定します。 このようなデコレータは、必ずしもユーザー提供の Runnable
/Callable
に適用されるのではなく、実際の実行コールバック(ユーザー提供のタスクのラッパーである可能性があります)に適用されることに注意してください。
主な使用例は、タスクの呼び出しの周囲に実行コンテキストを設定するか、タスク実行の監視 / 統計を提供することです。
注意 : TaskDecorator
実装での例外処理は、execute
呼び出しによる単純な Runnable
実行に制限されています。#submit
呼び出しの場合、公開された Runnable
は、例外を伝搬しない FutureTask
になります。例外を評価するには、キャストして Future#get
を呼び出す必要がある場合があります。このような Future
ケースで例外にアクセスする方法の例については、ThreadPoolExecutor#afterExecute
javadoc を参照してください。
protected ExecutorServiceSE initializeExecutor(ThreadFactorySE threadFactory, RejectedExecutionHandlerSE rejectedExecutionHandler)
ExecutorService
SE をその基本クラスに公開しますが、実際の ThreadPoolExecutor
SE ハンドルを内部に保存します。このメソッドをオーバーライドして、executor を置き換えるのではなく、単に ExecutorService
ハンドルを装飾したり、カスタム状態を保管したりしてください。ExecutorConfigurationSupport
の initializeExecutor
threadFactory
- 使用する ThreadFactoryrejectedExecutionHandler
- 使用する RejectedExecutionHandlerExecutorConfigurationSupport.afterPropertiesSet()
protected BlockingQueueSE<RunnableSE> createQueue(int queueCapacity)
正の容量値に対して LinkedBlockingQueue インスタンスが作成されます。SynchronousQueue その他。
queueCapacity
- 指定されたキュー容量 LinkedBlockingQueue
SE, SynchronousQueue
SEpublic ThreadPoolExecutorSE getThreadPoolExecutor() throws IllegalStateExceptionSE
null
)IllegalStateExceptionSE
- ThreadPoolTaskExecutor がまだ初期化されていない場合 public int getPoolSize()
public int getActiveCount()
public void execute(RunnableSE task)
TaskExecutor
task
を実行します。実装が非同期実行戦略を使用している場合、呼び出しはすぐに戻るか、同期実行の場合はブロックする可能性があります。
ExecutorSE
の executeSE
TaskExecutor
の execute
task
- 実行する Runnable
(非 null
)public void execute(RunnableSE task, long startTimeout)
AsyncTaskExecutor
task
を実行します。AsyncTaskExecutor
の execute
task
- 実行する Runnable
(非 null
)startTimeout
- タスクが開始される予定の期間(ミリ秒)。これはエグゼキューターへのヒントとして意図されており、即時タスクの優先処理を可能にします。一般的な値は AsyncTaskExecutor.TIMEOUT_IMMEDIATE
または AsyncTaskExecutor.TIMEOUT_INDEFINITE
(TaskExecutor.execute(Runnable)
で使用されるデフォルト)です。public FutureSE<?> submit(RunnableSE task)
AsyncTaskExecutor
null
の結果を返します。AsyncTaskExecutor
の submit
task
- 実行する Runnable
(非 null
)public <T> FutureSE<T> submit(CallableSE<T> task)
AsyncTaskExecutor
AsyncTaskExecutor
の submit
task
- 実行する Callable
(非 null
)public ListenableFuture<?> submitListenable(RunnableSE task)
AsyncListenableTaskExecutor
Runnable
タスクを実行のために送信し、そのタスクを表す ListenableFuture
を受け取ります。Future は完了時に null
の結果を返します。AsyncListenableTaskExecutor
の submitListenable
task
- 実行する Runnable
(非 null
)ListenableFuture
public <T> ListenableFuture<T> submitListenable(CallableSE<T> task)
AsyncListenableTaskExecutor
Callable
タスクを実行のために送信し、そのタスクを表す ListenableFuture
を受け取ります。Future は、完了時に Callable の結果を返します。AsyncListenableTaskExecutor
の submitListenable
task
- 実行する Callable
(非 null
)ListenableFuture
protected void cancelRemainingTask(RunnableSE task)
ExecutorConfigurationSupport
ExecutorService.shutdownNow()
SE から返された、実行を決して推奨しない残りのタスクをキャンセルします。ExecutorConfigurationSupport
の cancelRemainingTask
task
- キャンセルするタスク (通常、RunnableFuture
SE )ExecutorConfigurationSupport.shutdown()
, Future.cancel(boolean)
SE