Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

During a rolling deploy, the application calls executor.shutdownNow() and awaitTermination(30, SECONDS), but awaitTermination returns false and some tasks keep running. List the reasons a task ignores interruption and how you would make tasks cancellable.

shutdownNow() only interrupts the worker threads, so a task keeps running when it ignores that signal. The usual reasons are a loop that never checks Thread.currentThread().isInterrupted(), a catch block that swallows InterruptedException and carries on, and a thread blocked in an operation that does not respond to interruption, such as a classic java.io socket read or waiting to enter a synchronized block. We make tasks cancellable by checking the flag in loops, restoring it after catching, using interruptible APIs such as NIO channels, lockInterruptibly() or calls with timeouts, and by setting I/O timeouts so even non-interruptible calls end within a known bound.

Why this question is asked

During a rolling deploy, every old instance must stop quickly so the new one can take its place. If tasks ignore the stop signal, the instance either waits out its full grace period and gets killed, or it is killed mid-task with work half done. Interviewers ask this question because the answer shows whether we know what shutdownNow() really does and which code can and cannot be cancelled. In this answer, we will recap what shutdownNow() and awaitTermination() promise, go through each reason a task ignores interruption with an example, and then look at how to make every kind of task cancellable.

1A Quick Recap of shutdownNow() and awaitTermination()

An ExecutorService can be shut down in two ways. shutdown() stops accepting new tasks and lets queued and running tasks finish. shutdownNow() also stops accepting tasks, removes the tasks still waiting in the queue and returns them as a list, and calls interrupt() on every worker thread that is running a task. awaitTermination(30, SECONDS) then just waits until all workers have finished, or the timeout passes, and returns true or false.

Notice what is missing: nothing in this sequence forces a running task to stop. Interruption sets a flag on the thread and wakes it from certain blocking calls. If the task's code does not react to that, the task runs to its natural end, however long that takes. So awaitTermination() returning false is not a failure of the executor; it is a report that some task did not cooperate.

Step throughWill a running task stop after shutdownNow()?
  1. shutdownNow() interrupts the workerflag = true
  2. Is the task blocked in an interruptible call?
    YesCall throws InterruptedExceptionNoGo to the next check
  3. Does the task check the flag or rethrow?
    YesTask stopsNoTask keeps running
  4. Is it in a non-interruptible call?
    YesOnly a timeout or closing the resource ends itNoIt finishes on its own schedule

4 steps in order. Play them, or step through them one at a time.

Each no-branch is one of the reasons in this article.

2Reason 1: The Loop Never Checks the Flag

A task that does CPU work or calls only non-blocking code never meets a method that throws InterruptedException. If its loop does not ask whether it has been interrupted, the flag is set and ignored.

Java
// Ignores interruption: nothing in the loop looks at the flag
executor.submit(() -> {
for (Invoice inv : invoices) { // 2 million invoices, about 5 minutes
totals.add(compute(inv));
}
});
// Cancellable: check at a safe point in every iteration
executor.submit(() -> {
for (Invoice inv : invoices) {
if (Thread.currentThread().isInterrupted()) return; // stops within one invoice
totals.add(compute(inv));
}
});

The check costs almost nothing. We place it at a point where stopping leaves our data consistent, here between two invoices, which is the whole point of cooperative cancellation.

3Reason 2: The Task Swallows InterruptedException

When a blocking call such as sleep() or BlockingQueue.take() is interrupted, it clears the flag and throws InterruptedException. A catch block that logs and continues, or that wraps it in a RuntimeException and is then caught by a generic handler, throws the signal away.

Java
while (true) {
try {
process(queue.take());
} catch (InterruptedException e) {
log.warn("interrupted"); // flag cleared, loop continues: never stops
}
}
// Fixed: restore the flag and leave
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}

It is worth searching the codebase for catch (InterruptedException blocks with no interrupt() call and no rethrow, because each one is a task that shutdownNow() cannot stop. Generic retry helpers are a common hiding place: they catch Exception, which includes InterruptedException, and retry.

4Reason 3: The Task Is Blocked in a Non-Interruptible Call

Some blocking operations simply do not look at the interrupt flag. The classic ones are reading from a java.io InputStream obtained from a Socket on a platform thread, which keeps waiting for bytes, and waiting to enter a synchronized block or method, which waits until the lock is free. Many older database drivers and HTTP clients sit on top of such socket reads.

Java
synchronized (ledger) { // waiting here ignores interrupt()
ledger.append(entry);
}
int n = socket.getInputStream().read(buf); // platform thread: waits for data, ignores interrupt()

So a task can do everything right in its own code and still hang, because the call it is stuck in never returns. For these cases we need different tools: interruptible replacements, timeouts, or closing the resource from another thread, which makes the blocked call fail. Virtual threads change part of this, because the JDK makes many socket operations interruptible on them, but check your JDK version and do not rely on it for drivers that use their own I/O.

5Making Every Task Cancellable

We can now make a short standard for task code. Loops check isInterrupted() at safe points. Every catch of InterruptedException either rethrows it or restores the flag and returns. Lock waits use ReentrantLock.lockInterruptibly() or tryLock(timeout) instead of synchronized when the wait may be long. I/O uses interruptible APIs where possible: an NIO channel that implements InterruptibleChannel is closed when the thread is interrupted, and the blocked call throws ClosedByInterruptException.

Java
ReentrantLock lock = new ReentrantLock();
lock.lockInterruptibly(); // throws InterruptedException if interrupted while waiting
try { ledger.append(entry); } finally { lock.unlock(); }
try (SocketChannel ch = SocketChannel.open(address)) {
ch.read(buffer); // interrupt() closes the channel: ClosedByInterruptException
}
Future<Result> f = client.sendAsync(request);
Result r = f.get(5, TimeUnit.SECONDS); // bounded wait, interruptible too

When a task holds a resource it cannot read interruptibly, such as a third-party client, it can register a cancellation hook that closes the resource. Closing a socket from another thread makes a blocked read throw a SocketException, which ends the task.

6Setting I/O Timeouts as the Final Bound

Even with all of this, some calls will remain non-interruptible. The last line of defence is a timeout on every network call: a connect timeout and a read timeout on HTTP clients, a socket timeout and a query timeout on the JDBC driver and the connection pool. With a 10-second read timeout, a stuck read ends in at most 10 seconds, interrupt or not. We then size the shutdown budget to match: if the longest bounded operation is 10 seconds and awaitTermination() waits 30, every task has ended well before Kubernetes sends its final kill.

During the deploy, logging which tasks are still running when awaitTermination() returns false, with a thread dump of the pool's threads, quickly shows which of the three reasons applies to each one.

7Follow-up questions

Why does shutdownNow() not just stop the threads?

Because Java has no safe way to stop a thread at an arbitrary point. Thread.stop() did that, could leave shared data half-updated, and since Java 20 it throws UnsupportedOperationException. Interruption is the only stop mechanism, and it relies on the task to stop at a safe place.

What should we do with the list that shutdownNow() returns?

It holds the tasks that never started. Depending on the work, we log them, persist them for a later retry, or hand them back to a durable queue, so a deploy does not silently drop accepted work.

Is a longer awaitTermination() timeout a fix?

No. It only hides the problem until the platform's own deadline, after which the process is killed with tasks half done. A longer timeout makes sense only after tasks are bounded, to match the longest legitimate operation.

8Conclusion

In this answer, we saw that shutdownNow() only interrupts worker threads and awaitTermination() only waits, so a running task stops only if it cooperates. Tasks ignore the signal when their loops never check the flag, when they swallow InterruptedException, and when they are blocked in non-interruptible calls such as java.io socket reads or synchronized lock waits. We made tasks cancellable with flag checks, restored interrupts, interruptible locks and channels, bounded waits, and timeouts on all I/O as the final bound.

What to say in the interviewAbout 90 seconds aloud

Open with

shutdownNow doesn't stop anything by force. It drains the queue and calls interrupt on each worker that's running a task, and awaitTermination just waits. So if a task doesn't respond to interruption, it keeps running and awaitTermination returns false.

Then, if they want more

There are three usual reasons. First, the task has a loop that never checks Thread.currentThread().isInterrupted, for example a CPU loop over two million invoices, so the flag is set and nobody looks at it. Second, the task catches InterruptedException and carries on.

Catching it clears the flag, so the signal is gone; generic retry helpers that catch Exception do this too. Third, the task is stuck in a call that doesn't respond to interruption, like a classic java.io socket read on a platform thread, or waiting to enter a synchronized block.

To make tasks cancellable, I check the flag at safe points in loops, and every catch of InterruptedException either rethrows or restores the flag with Thread.currentThread().interrupt() and returns.

For locks that can wait long I use lockInterruptibly or tryLock with a timeout, for I/O I prefer interruptible channels or async calls with bounded get, and for resources I can't interrupt I close them from a cancellation hook.

Finally, every network call gets connect and read timeouts, so even a non-interruptible call ends within a known bound that fits inside the shutdown budget.

About this answer

Last updated
12 November 2026
Versions it names
Java 20
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "During a rolling deploy, the application calls executor.shutdownNow() and...": https://scrivio-interview-library.netlify.app/java/threads/f7beee9612/