Executor interface in java.util.concurrent package lets you submit Runnable tasks. Executor then handles the execution of the task. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.

Contract of Executor interface has a single method: execute(Runnable command)

The following executor is a basic implementation. It runs the task on the main thread without creating a new thread.

public class DirectExecutor implements Executor {

    public static void main(String[] args) {
        Executor executor = new DirectExecutor();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("Simple task completed!");
            }
        });
    }

    @Override
    public void execute(Runnable command) {
        command.run();
    }
}

 

Leave a Reply

Close Menu