Fundamentals 4 min read

Gracefully Stopping a Java Thread and the Difference Between isInterrupted() and interrupted()

The deprecated Thread.stop method should never be used; instead, Java threads are stopped gracefully by using a volatile stop flag or interrupting blocked threads, handling InterruptedException, and distinguishing between Thread.isInterrupted() (read‑only) and Thread.interrupted() (clears the flag).

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Gracefully Stopping a Java Thread and the Difference Between isInterrupted() and interrupted()

Can Thread.stop be used to terminate a thread? No. The method has been deprecated for a long time, throws an exception when called, and will eventually be removed. In early JDK versions it forced the thread to stop, releasing all monitors and resources, breaking consistency, and leaving file descriptors or network connections unreleased.

How to close a thread gracefully? Let the thread finish its work, release owned resources, and exit cleanly.

1. Common case : Define a volatile boolean stop flag. In the thread’s processing loop, check the flag; when it becomes true , break the loop and let the thread terminate.

Example (simplified Tomcat acceptor logic):

// set the volatile stop flag

// in the business loop

// provide a method to modify the stop flag

2. Special case – thread blocked in wait, join, sleep, etc. The stop flag alone does not work. Use Thread.interrupt() to set the interrupt status. If the thread is blocked, these methods throw InterruptedException , clear the interrupt status, and wake the thread.

Typical handling:

try { /* blocking operation */ } catch (InterruptedException e) { Thread.currentThread().interrupt(); /* handle shutdown */ }

After catching the exception, reset the interrupt status with Thread.currentThread().interrupt() and later check Thread.currentThread().isInterrupted() to decide whether to exit.

Example of the internal interrupt flag (from OpenJDK source):

volatile boolean interrupted;

Calling Thread.currentThread().interrupt() writes true to this field; the JVM may later clear it when the thread is awakened.

3. Extremely special case : Some blocking methods do not respond to interruption and do not throw InterruptedException . In such cases, interrupting the thread cannot achieve a graceful shutdown.

Difference between isInterrupted() and interrupted()

The instance method Thread.isInterrupted() reads the interrupt status without clearing it, while the static method Thread.interrupted() reads the status and clears it.

JavaconcurrencyThreadInterruptGraceful Shutdown
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.