- Java Concurrency Utilities - java.util.concurrent
- Java BlockingQueue
- Java ExecutorService
- Java Callable
- Java Future
- Java Fork and Join using ForkJoinPool
java.util.concurrent.ThreadPoolExecutor is an implementation of the
ThreadPoolExecutor executes the given task (
using one of its internally pooled threads.
The thread pool contained inside the
ThreadPoolExecutor can contain a varying amount of threads.
The number of threads in the pool is determined by these variables:
If less than
corePoolSize threads are created in the the thread pool when a task is delegated
to the thread pool, then a new thread is created, even if idle threads exist in the pool.
If the internal queue of tasks is full, and
corePoolSize threads or more are running, but less
maximumPoolSize threads are running, then a new thread is created to execute the task.
Here is a diagram illustrating the
Creating a ThreadPoolExecutor
ThreadPoolExecutor has several constructors available. For instance:
int corePoolSize = 5; int maxPoolSize = 10; long keepAliveTime = 5000; ExecutorService threadPoolExecutor = new ThreadPoolExecutor( corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>() );
However, unless you need to specify all these parameters explicitly for your
it is often easier to use one of the factory methods in the
as shown in the ExecutorService text.