What is Executor Framework?
Executor framework was introduced in Java 5. The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Executor framework is a collection of interfaces and classes under java.util.concurrent package. These interfaces/classes eases the development of concurrent application. Root of this framework is Executor.
One of the goal of executor framework is to separate the creation and management of threads from rest of the application.
The java.util.concurrent package contains a flexible thread pool implementation, but even more valuable, it contains an entire framework for managing the execution of tasks that implement Runnable.
What does Executor Framework contains?
Executor framework consists of following important interfaces.
- Callable: This interface is similar to runnable. The only difference in this interface is that its call method returns a generic value.
- Executor: This interface is responsible for submission of task does not do any thing with the execution. Execution is done by the implementer of this interface. It is the choice of Executor implementation that determines the execution policy. An Executor is generally used instead of creating threads.
- ExecutorService : This is an extension of Executor interface. This manages the life cycle of execution service. Its contains methods like shutdown() to stop and submit() for task submission. This interface manages three states of task running,shutting down and terminated.
- ScheduledExecutorService: This interface is used for scheduling the task or running a task periodically.
- Future: A Future represents the result of an asynchronous computation. The ExecutorService which can execute Callable tasks returns a Future object to return the result of the Callable task. A submission callable to an executor the framework returns a java.util.concurrent.Future. This futures can be used to check the status of a callable and to retrieve the result from the callable.
What are the benefits of using Executor Framework?
There are lots of benefits of using Executor framework.(Which we will discover as we move forward.) Some of them are listed below.
- This framework encapsulates lots of very low level programming which we have to write in pre-java 5 concurrent application.
- Executor framework separates the task submission from task execution and provide lots of execution policies.
- This framework eases the management of threads.
- Framework can be used to schedule a thread.
- Can get response back from the thread after completion of it.
- Provides lots of other utilities which were required in complex concurrent application.
- More importantly it standardizing invocation, scheduling, execution and control of asynchronous tasks according to a set of execution policies.