Executor Framework
Java Concurrency API defines below three executor interfaces
that covers everything that is needed for creating and managing threads: -
Executors Framework - This is a framework for creating and
managing threads.
Executor - A simple interface for executing tasks or A
simple interface that contains a method called execute () to launch a task
specified by a Runnable object.
ExecutorService -
A more complex interface which contains additional methods for managing the tasks and the executor itself or A sub-interface of Executor that adds functionality to manage the lifecycle of
the tasks. It also provides a submit () method whose overloaded versions can
accept a Runnable as well as a Callable object. Callable objects are similar to
Runnable except that the task specified by a Callable object can also return a
value.
ScheduledExecutorService
- Extends ExecutorService with methods for scheduling the execution of a task or A sub-interface of ExecutorService. It adds functionality to schedule the
execution of the tasks.
Executors
framework helps to do following:
To Create a Group of Threads: We can create threads using
the available various methods, more specifically a pool of threads, that your
application can use to run the appropriate tasks concurrently.
Thread Management: It manages the life cycle of the threads
in the thread pool. You don’t need to worry about whether the threads in the
thread pool are active or busy or dead before submitting a task for execution.
Task submission and execution: Executors framework provides
methods for submitting tasks for execution in the thread pool, and also gives
us the provision to decide when the tasks will be executed. For example, you
can submit a task to be executed now or schedule them to be executed later or
make them execute periodically.
Q:How to Create an Executor?
If you want to create an Executor, it is possible to use
Executors class factory methods. Below are the most common methods, which are
used to create Executors:
An ExecutorService with a single
thread to execute commands with method newSingleThreadExecutor.
A ScheduledExecutorService with a
single thread to execute commands with the method
newSingleThreadScheduledExecutor.
An ExecutorService that use a
fixed length pool of threads to execute commands with the method
newFixedThreadPool.
An ExecutorService with a pool of
threads that creates a new thread if no thread is available and reuse an
existing thread if they are available with newCachedThreadPool.
A ScheduledExecutorService with a fixed length
pool of threads to execute scheduled commands with the method
newScheduledThreadPool.
Here are examples to creates ExecutorService and
ScheduledExecutorService instances:
// Creates a single thread ExecutorService
1) ExecutorService singleExecutorService =
Executors.newSingleThreadExecutor();
// Creates a single thread ScheduledExecutorService
2) ScheduledExecutorService singleScheduledExecutorService =
Executors.newSingleThreadScheduledExecutor();
// Creates an ExecutorService that use a pool of 5 threads
3) ExecutorService fixedExecutorService =
Executors.newFixedThreadPool(5);
// Creates an ExecutorService that use a pool that creates
threads on demand and that kill them after 60 seconds if they are not used
4) ExecutorService onDemandExecutorService =
Executors.newCachedThreadPool();
// Creates a ScheduledExecutorService that use a pool of 5
threads
5) ScheduledExecutorService fixedScheduledExecutorService =
Executors.newScheduledThreadPool(5);