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

The get() Method: Blocking Until Completion

Learn how Future.get() blocks the calling thread until the task completes and returns the result.

The get() Method: Blocking Until Completion

What you'll learn: How Future.get() pauses the calling thread until the submitted task finishes and delivers its result.

The Core Idea

When you call submit() on an ExecutorService, you immediately receive a Future object—but the task itself may still be running in the background. To actually retrieve the result, you call future.get(). This method blocks (waits) the calling thread until the task completes, then returns whatever the task produced.

Think of it like ordering food for delivery: submit() places your order and gives you a tracking number (Future). Calling get() is like sitting at your door waiting—you literally cannot do anything else until the delivery arrives. Once it does, you receive your meal (the result).

How It Works

ExecutorService executor = Executors.newFixedThreadPool(2);

Callable<Integer> task = () -> {
    Thread.sleep(3000); // Simulates long computation
    return 42;
};

Future<Integer> future = executor.submit(task);

System.out.println("Task submitted, doing other work...");

// This line BLOCKS until the task completes (3+ seconds)
Integer result = future.get();

System.out.println("Result: " + result); // Prints: Result: 42

executor.shutdown();

The moment you call future.get(), your thread stops and waits—even if the task takes minutes. You cannot proceed past that line until the background task finishes.

Blocking Behavior in Practice

If the task is already complete when you call get(), it returns immediately. If not, your thread is parked until completion. This makes get() powerful but potentially dangerous—calling it on the main thread can freeze your application if the task takes too long.

Key Takeaway: Future.get() blocks the calling thread until the asynchronous task completes, then returns the computed result—use it carefully to avoid unintended freezes.