An Executor is an object that executes submitted Runnable tasks.

Executor is not a class, rather it’s an interface. Executor’s main role is to allow for a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. It’s meant to be used as an alternative to explicit creation of threads. For example, rather than invoking new Thread(new RunnableTask()).start() for each of a set of tasks, you might use:

  Executor executor = anExecutor();
 executor.execute(new RunnableTask1());
 executor.execute(new RunnableTask2());

It is not a must that all the time the Executor has to be asynchronous. In the simplest case, an executor can run the submitted task immediately in the caller’s thread:

  class DirectExecutor implements Executor {
   public void execute(Runnable r) {;

However typically, you’ll use an Executor to execute tasks asynchronously, that is in another thread other than the caller. This example will create and start a new thread for each task.

 class ThreadPerTaskExecutor implements Executor {
   public void execute(Runnable r) {
     new Thread(r).start();

Executor API Definition

Executor is an interface:

 public interface Executor 

It’s define the package java.util.concurrent:


Executor Methods

Executor is an interface that has only one method. This method takes a Runnable object.

 void execute (Runnable command)

This method is reponsible for executing the given command at some time in the future. The command may execute in a new [thread](/java/thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

Categorized in: