Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Executor — separating tasks from execution policy

java.util.concurrent.Executor accepts Runnable tasks through execute. It separates task submission from execution policy; it guarantees neither a separate thread nor asynchronous execution.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

void execute(Runnable command)

java.util.concurrent.Executor separates task submission from execution management. The caller supplies a Runnable, and the implementation chooses how to execute it. Tasks may run sequentially or concurrently, on a new thread, a pooled thread, or the calling thread.

Executor.execute(Runnable command) returns void, not a task result or a Future. Do not assume that returning from the method means the task has completed, or that the method always operates asynchronously. A null argument causes NullPointerException; the documentation specifies RejectedExecutionException when a task cannot be accepted.

Executor itself provides no shutdown or completion-waiting methods. Its subinterface ExecutorService provides richer task management and controlled shutdown. Executor is an interface, not the Executors pool factory.

In the example, Runnable::run creates a direct executor: execute calls run() on the current thread. The order before, task, after is therefore deterministic. This demonstrates the interface contract, not background processing.

Examples

import java.util.concurrent.Executor;

public class Main {
    public static void main(String[] args) {
        Executor direct = Runnable::run;
        System.out.println("before");
        direct.execute(() -> System.out.println("task"));
        System.out.println("after");
    }
}

Result

before
task
after

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources