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

Monitoring Executor State: isShutdown and isTerminated

Using isShutdown() to check if shutdown was called and isTerminated() to verify all tasks have completed.

Monitoring Executor State: isShutdown and isTerminated

You'll learn how to check whether an executor has begun shutdown and whether it has fully completed all tasks.

Why Monitor Executor State?

After calling shutdown() or shutdownNow(), you often need to know: Has the executor started shutting down? and Has it actually finished all its work? These are two different questions, and Java provides two distinct methods to answer them.

The isShutdown Method

isShutdown() returns true if shutdown() or shutdownNow() has been called on the executor. Think of it as checking whether the "closing time" announcement has been made at a restaurant—even if customers are still finishing their meals, the restaurant is no longer accepting new diners.

ExecutorService executor = Executors.newFixedThreadPool(2);
executor.execute(() -> System.out.println("Task running"));

System.out.println(executor.isShutdown());  // false
executor.shutdown();
System.out.println(executor.isShutdown());  // true (shutdown initiated)

The isTerminated Method

isTerminated() returns true only after shutdown has been initiated and all tasks have completed execution. This is like confirming the restaurant is truly closed—the announcement was made and the last customer has left.

executor.shutdown();
System.out.println(executor.isTerminated());  // likely false (tasks still running)

// Wait for completion
while (!executor.isTerminated()) {
    // Polling loop
}
System.out.println(executor.isTerminated());  // true (all tasks done)

Key Distinction

isShutdown() tells you shutdown was requested. isTerminated() tells you shutdown is complete. You'll have a window where isShutdown() is true but isTerminated() is still false while tasks finish.

Key Takeaway: Use isShutdown() to check if the executor won't accept new tasks, and isTerminated() to verify that all work has actually finished and threads have stopped.