ThreadPoolExecutorのソース コード ドキュメントから
/*
* <p>Thread pools address two different problems: they usually
* provide improved performance when executing large numbers of
* asynchronous tasks, due to reduced per-task invocation overhead,
* and they provide a means of bounding and managing the resources,
* including threads, consumed when executing a collection of tasks.
* Each {@code ThreadPoolExecutor} also maintains some basic
* statistics, such as the number of completed tasks.
*
* <p>To be useful across a wide range of contexts, this class
* provides many adjustable parameters and extensibility
* hooks. However, programmers are urged to use the more convenient
* {@link Executors} factory methods {@link
* Executors#newCachedThreadPool} (unbounded thread pool, with
* automatic thread reclamation), {@link Executors#newFixedThreadPool}
* (fixed size thread pool) and {@link
* Executors#newSingleThreadExecutor} (single background thread), that
* preconfigure settings for the most common usage
* scenarios.
*/
ThreadPoolExecutor
同時実行を実現する 1 つの方法です。並行性を達成するには多くの方法があります。
Executorsフレームワークは、さまざまな API を提供します。重要な API の一部を以下に示します。
static ExecutorService newFixedThreadPool(int nThreads)
無制限の共有キューで動作する固定数のスレッドを再利用するスレッド プールを作成します。
static ExecutorService newCachedThreadPool()
必要に応じて新しいスレッドを作成するスレッド プールを作成しますが、以前に構築されたスレッドが利用可能になった場合はそれらを再利用します。
static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
指定された遅延後にコマンドを実行するか、定期的に実行するようにコマンドをスケジュールできるスレッド プールを作成します。
static ExecutorService newWorkStealingPool()
使用可能なすべてのプロセッサをターゲット並列処理レベルとして使用して、ワークスティーリング スレッド プールを作成します。
以下の SE の質問をご覧ください。
Java Fork/Join プール、ExecutorService および CountDownLatch
Java Executor を適切に使用するには?