Course contentsShow
← Java
Lesson 2710 of 2,87050. Executor FrameworkFree lesson

The Executor Interface: Decoupling Task Submission from Execution

How Executor provides a single execute(Runnable) method that separates what runs from how it runs.

The Executor Interface: Decoupling Task Submission from Execution

What you'll learn: How the Executor interface separates what runs from how it runs using a single execute(Runnable) method.

The Core Problem Revisited

In the previous lesson, we saw how manual thread management becomes painful: creating threads directly, tracking them, managing their lifecycle. Every time you submit a task, you're forced to decide both what the task does and how it executes (new thread? existing thread? priority?).

Enter the Executor Interface

The Executor interface provides a single, elegant method:

public interface Executor {
    void execute(Runnable command);
}

This simple contract decouples two concerns:

  • What to run: The Runnable task containing your business logic
  • How to run it: The execution policy hidden behind the implementation

The Restaurant Analogy

Think of submitting tasks like placing orders at a restaurant. You (the caller) say what you want (the dish/task). You don't specify how the kitchen runs—whether they use one chef, ten chefs, or prepare dishes in batches. The Executor is the kitchen manager deciding execution strategy.

Why This Matters

Before Executor:

new Thread(myTask).start(); // Tightly coupled to thread creation

With Executor:

executor.execute(myTask); // You have no idea how it runs—and that's good!

The implementation might run your task immediately on the current thread, queue it for a thread pool, or delay it. You don't know, and you don't need to care. This separation allows the execution strategy to evolve independently from your task logic.

Key Takeaway: The Executor interface's single execute(Runnable) method cleanly separates task submission from execution strategy, allowing flexible threading policies without changing task code.