JAva ThreadPoolExecutorカスタムスレッドプールの利点
JAvaコンカレントスレッドプールは、ThreadPoolExecutorのコンストラクション関数を使用してカスタムスレッドプールを設定することをお勧めします.まず、このコンストラクション関数のパラメータリストを見てみましょう.
これらのパラメータリストから、カスタムスレッドプールを実装できます.1)スレッドプール内のコアスレッド数,2)スレッドプール内の最大スレッド数,3)スレッドプール内のスレッドの最大アイドル時間は,この時間を超えると空きスレッドが回収される.4)ブロックキューの定義は、ArrayListかLinkedListかのブロックキューを設定できます.5)スレッドファクトリは、カスタムスレッドファクトリを使用して、スレッドプール内のスレッドに独自のラベルを付け、デバッグを容易にすることができます.スレッドファクトリをカスタマイズするのは簡単で、ThreadFactoryを継承してnewThreadメソッドを書き換えるだけです.6)スレッド拒否ポリシー、典型的ないくつかのスレッドプールのデフォルトポリシーはすべて例外を投げ出すので、実はあなたはスレッド拒否ポリシーをカスタマイズすることができて、現在ThreadPoolExecutorは私達に4種類のポリシーを提供して、それぞれ:A:ThreadPoolExecutor.AbortPolicyは直接例外を投げ出すB:ThreadPoolExecutor.DiscardPolicyは現在拒否されているタスクを投げ出す(いかなる例外を投げ出さない)C:ThreadPoolExecutor.DiscardOldestPolicyはバッファの中で最も古いタスクを破棄し、拒否されたタスクD:ThreadPoolExecutor.CallerRunsPolicyがタスクの提出元スレッドで拒否されたタスクを実行して満足しなければ、自分で定義したスレッド拒否ポリシーを実現することができ、実現方法も簡単です.RejectedExecutionHandlerインタフェースで定義された方法を実装するだけでよい.
上記の6つのパラメータを使用すると、カスタムスレッドプールを実現できます.ここでは,スレッドコミットを拒否するhandlerがいつトリガーされるか,1)プール内のスレッド数がcorePoolSizeより小さい場合,新しいタスクはキューに並ばずに直接新しいスレッドを追加する.2)プール内のスレッド数がcorePoolSize以上の場合、workQueueは満たされていません.新しいスレッドを追加するのではなく、新しいタスクをworkQueueに追加することを優先します.3)プール内のスレッド数がcorePoolSize以上の場合、workQueueは満タンになりますが、スレッド数がmaximumPoolSize未満の場合、追加されたタスクを処理するために新しいスレッドを追加します.4)プール内のスレッド数がcorePoolSize以上であり,workQueueがいっぱいであり,スレッド数がmaximumPoolSize以上である場合,新しいタスクは拒否され,handlerを用いて拒否されたタスクを処理する.
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
これらのパラメータリストから、カスタムスレッドプールを実装できます.1)スレッドプール内のコアスレッド数,2)スレッドプール内の最大スレッド数,3)スレッドプール内のスレッドの最大アイドル時間は,この時間を超えると空きスレッドが回収される.4)ブロックキューの定義は、ArrayListかLinkedListかのブロックキューを設定できます.5)スレッドファクトリは、カスタムスレッドファクトリを使用して、スレッドプール内のスレッドに独自のラベルを付け、デバッグを容易にすることができます.スレッドファクトリをカスタマイズするのは簡単で、ThreadFactoryを継承してnewThreadメソッドを書き換えるだけです.6)スレッド拒否ポリシー、典型的ないくつかのスレッドプールのデフォルトポリシーはすべて例外を投げ出すので、実はあなたはスレッド拒否ポリシーをカスタマイズすることができて、現在ThreadPoolExecutorは私達に4種類のポリシーを提供して、それぞれ:A:ThreadPoolExecutor.AbortPolicyは直接例外を投げ出すB:ThreadPoolExecutor.DiscardPolicyは現在拒否されているタスクを投げ出す(いかなる例外を投げ出さない)C:ThreadPoolExecutor.DiscardOldestPolicyはバッファの中で最も古いタスクを破棄し、拒否されたタスクD:ThreadPoolExecutor.CallerRunsPolicyがタスクの提出元スレッドで拒否されたタスクを実行して満足しなければ、自分で定義したスレッド拒否ポリシーを実現することができ、実現方法も簡単です.RejectedExecutionHandlerインタフェースで定義された方法を実装するだけでよい.
上記の6つのパラメータを使用すると、カスタムスレッドプールを実現できます.ここでは,スレッドコミットを拒否するhandlerがいつトリガーされるか,1)プール内のスレッド数がcorePoolSizeより小さい場合,新しいタスクはキューに並ばずに直接新しいスレッドを追加する.2)プール内のスレッド数がcorePoolSize以上の場合、workQueueは満たされていません.新しいスレッドを追加するのではなく、新しいタスクをworkQueueに追加することを優先します.3)プール内のスレッド数がcorePoolSize以上の場合、workQueueは満タンになりますが、スレッド数がmaximumPoolSize未満の場合、追加されたタスクを処理するために新しいスレッドを追加します.4)プール内のスレッド数がcorePoolSize以上であり,workQueueがいっぱいであり,スレッド数がmaximumPoolSize以上である場合,新しいタスクは拒否され,handlerを用いて拒否されたタスクを処理する.