Submitting Callables and Obtaining Future Objects
What you'll learn: How to submit Callable tasks to an ExecutorService and receive a Future that represents the pending result.
The Core Pattern
Now that you understand what Callable tasks are, it's time to actually run them. When you submit a Callable to an ExecutorService, you immediately get back a Future object—a handle to the result that will eventually be computed by a thread in the pool.
The submit Method with Callable
The ExecutorService.submit() method accepts a Callable<V> and returns a Future<V> of the same type:
ExecutorService executor = Executors.newFixedThreadPool(3);
Callable<Integer> task = () -> {
Thread.sleep(1000);
return 42;
};
Future<Integer> future = executor.submit(task);
// Task is now running (or queued), future represents pending result
The Handoff
Think of this as ordering food at a restaurant: you place your order (submit the Callable), and the server gives you a receipt with an order number (the Future). You don't wait at the counter—you take your receipt and can do other things. Later, when you're ready, you use that receipt to claim your food (call future.get()).
The beauty here is non-blocking submission: your thread continues immediately after submitting, even though the result isn't ready yet.
Multiple Submissions
You can submit many Callable tasks and collect their Future objects:
List<Future<Integer>> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final int taskId = i;
Future<Integer> f = executor.submit(() -> taskId * taskId);
futures.add(f);
}
// All tasks running concurrently, futures collected for later retrieval
Key Takeaway: Submitting a Callable to an ExecutorService immediately returns a Future representing the asynchronous computation, allowing you to continue execution while the task runs in the background.