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

Submitting Tasks and Receiving Futures

Understand how ExecutorService.submit() returns a Future for tracking task completion and retrieving results.

Submitting Tasks and Receiving Futures

What You'll Learn

You'll learn how ExecutorService.submit() returns a Future object that lets you track task progress and retrieve results asynchronously.

The Core Mechanism

When you call submit() on an ExecutorService, you're handing off a task for execution by the thread pool, but you're not executing it immediately in the current thread. The method returns a Future object instantly—think of it as a claim ticket for results that will arrive later.

The Future acts as a handle to the task running somewhere in the pool. You can use it to:

  • Check if the task is done
  • Wait for completion and retrieve the result
  • Cancel the task if needed

This is fundamentally different from execute(), which accepts a Runnable and returns nothing—it's a true "fire-and-forget" approach.

How It Works

ExecutorService executor = Executors.newFixedThreadPool(2);

// Submit a Callable that returns a result
Future<Integer> future = executor.submit(() -> {
    // Simulate work
    Thread.sleep(1000);
    return 42;
});

// The task is now running (or queued)
System.out.println("Task submitted");

// Later, retrieve the result (blocks if not ready)
Integer result = future.get();  // returns 42

When you submit a Runnable instead of a Callable, the Future still comes back, but calling get() returns null (or a placeholder you provided). The Future still lets you check completion status.

Key Takeaway: ExecutorService.submit() returns a Future immediately, giving you a handle to track and retrieve results from tasks executing asynchronously in the thread pool—bridging the gap between task submission and eventual completion.