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

isDone(): Checking Task Completion Non-Blockingly

Poll task completion status with isDone() without blocking, enabling responsive application logic.

isDone(): Checking Task Completion Non-Blockingly

What you'll learn: How to check if a Future task has completed without waiting, enabling your application to stay responsive and make intelligent decisions based on task status.

The Need for Non-Blocking Status Checks

You've already learned that get() blocks your thread until a task completes. But what if you want to check whether a task is finished without waiting? Maybe you want to display a progress indicator, handle other work while waiting, or decide whether to cancel a long-running task. That's where isDone() comes in.

How isDone() Works

The isDone() method returns a simple boolean:

  • true if the task has completed (successfully, with an exception, or via cancellation)
  • false if the task is still running

Crucially, isDone() returns immediately — it never blocks your thread.

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> {
    Thread.sleep(3000);
    return "Done!";
});

// Check status without blocking
while (!future.isDone()) {
    System.out.println("Task still running...");
    Thread.sleep(500);
}

System.out.println("Task completed!");
String result = future.get(); // Now safe to get result

Think of It Like Checking Your Mail

Using get() is like waiting at your mailbox until the letter carrier arrives. Using isDone() is like peeking into the mailbox to see if mail has already been delivered — you check instantly and move on with your day either way.

Practical Applications

Use isDone() to:

  • Poll multiple Future objects and process whichever completes first
  • Update UI or logs to show task progress
  • Implement timeout logic more flexibly than get(timeout)
  • Avoid blocking when results aren't immediately needed

Key Takeaway: isDone() provides instant, non-blocking insight into task completion status, letting you build responsive applications that react intelligently to asynchronous work without getting stuck waiting.