Skip to main content
  1. Java Concurrency (java.util.concurrent)/

Executor Interface

1 min

The Executor interface is the fundamental building block of the java.util.concurrent package. It establishes a powerful abstraction: decoupling task submission from task execution.

Source Code & Implementation #

View Source on GitHub

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

Canonical Usage #

When to use: Use Executor when you have “fire-and-forget” tasks and do not need to track their progress, handle their return values, or manage the lifecycle of the underlying threads.

Common Patterns:

  • Decoupling Task Submission: Instead of hardcoding new Thread(r).start(), pass an Executor into your class. This allows you to switch from a single-threaded execution to a thread pool without changing your business logic.
  • Event Dispatching: In UI frameworks or event-driven systems, an Executor is often used to offload work from a high-priority thread (like the UI thread) to a background worker.
public class DataProcessor {
    private final Executor executor;

    public DataProcessor(Executor executor) {
        this.executor = executor;
    }

    public void process(Data data) {
        executor.execute(() -> {
            // Background work here
            saveToDatabase(data);
        });
    }
}