Java Reference
In-Depth Information
The Executor interface in the java.util.concurrent package is the foundation for the executor framework. It is
an interface with only one method, as shown:
public interface Executor {
void execute (Runnable command);
}
You can use the executor framework to execute the above-mentioned three tasks as follows:
// Get an executor instance.
// Method Executors.newCachedThreadPool() will be explainedshortly.
Executor executor = Executors.newCachedThreadPool();
// Submit three tasks to the executor
executor.execute(task1);
executor.execute(task2);
executor.execute(task3);
Note that when you used an executor, you did not create three threads to execute the three tasks. The executor
will decide that for you. You just called the execute() method of the executor to submit a task. The executor will
manage the threads that will execute the tasks and other details about the task execution.
The executor framework provides a class library to select the policies on the thread usage to execute the tasks.
You can choose to run all tasks in one thread, in a fixed number of threads, or in a variable number of threads. In fact,
you can choose a thread pool to execute your tasks, and the thread pool is configurable as to how many threads will be
in the pool and how those threads will be maintained. In any case, all threads in the pool are reused as they become
available. Using a thread pool to execute the submitted tasks has two important advantages:
The overhead of creating and destroying new threads is reduced. The executor reuses the
threads from the thread pool.
If a thread is available in the thread pool at the time of a task submission, the task may
start immediately. This eliminates the time delay between the thread creation and the
task execution.
It is important to mention another interface called ExecutorService at this point. It provides some advanced
features of an executor, which include managing the shutdown of the executor and checking the status of the
submitted tasks. It inherits the Executor interface. Some of the important methods of this interface are shutdown() ,
shutdownNow() , submit() , and awaitTermination() . I will discuss them shortly.
It is important that you shut down the executor when it is no longer needed. The executor framework creates
non-daemon threads to execute the tasks. Generally, when a thread is done executing a task, it is not destroyed.
Rather it is kept in the thread pool for reuse in the future. (Whether a thread is destroyed or kept depends on the
thread pool configuration). A Java application will not exit if some non-daemon threads are still alive. Therefore, if you
forget to shut down the executor, your application may never exit.
How does an executor handle a task execution? To avoid a detailed and lengthy discussion, here is a simple
explanation. You specify the type of thread pool that the executor should use to manage the tasks at the time you
create the executor. All tasks that you submit to an executor are queued in a queue known as the work queue . As a
thread becomes available, it removes a task from the work queue and executes it. When a thread is done executing a
task, depending on your thread pool type, your executor either destroys the thread or puts it back into the pool so it
Search WWH ::




Custom Search