Forceful Shutdown: The shutdownNow Method
You'll learn how to forcibly halt an ExecutorService and retrieve tasks that never got a chance to run.
In the last lesson, you saw shutdown() — a polite way to let running tasks finish before the executor stops accepting new work. But what if you need to pull the emergency brake? That's where shutdownNow() comes in.
How shutdownNow Works
When you call shutdownNow() on an ExecutorService, it:
- Stops accepting new tasks (just like
shutdown()) - Attempts to stop all currently executing tasks by interrupting their threads
- Returns a list of tasks that were queued but never started
Think of it like a fire alarm during a factory shift: workers stop mid-task, evacuate immediately, and management gets a list of unfinished orders that were still waiting in the queue.
The "Attempt" Caveat
Notice the word attempts — shutdownNow() sends interrupt signals to running threads, but there's no guarantee they'll stop instantly. If your task code doesn't respond to interruption (doesn't check Thread.interrupted() or handle InterruptedException), it may keep running until completion anyway.
What You Get Back
The method returns List<Runnable> — the tasks that were waiting in the queue but never got assigned to a thread. This gives you a chance to log them, retry them elsewhere, or handle them specially.
When to Use It
Use shutdownNow() when:
- You need immediate termination (user cancellation, critical error)
- You can tolerate incomplete work
- You want to know what didn't get done
Key Takeaway:
shutdownNow() forcibly interrupts running tasks and returns the list of queued tasks that never started — it's the emergency shutdown when graceful termination isn't enough.