What does java.lang.Thread.interrupt do?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Java's java.lang.Thread.interrupt() method is a significant part of the Java concurrency framework. Understanding how it works and its role in managing threads and dealing with interruption is crucial for writing effective multithreaded applications. This article explores the Thread.interrupt() method in detail, demonstrating its uses, behavior, and some best practices.
Overview of Thread Interruption in Java
In Java, "interrupting" a thread is a way to signal that the thread should stop what it is doing and do something else. It's important to note that the interrupt() method does not forcibly terminate the thread. Instead, it sets the thread’s interrupt status, creating a flag that the thread can check to decide whether it needs to stop execution.
The interrupt() Method
The interrupt() method belongs to the Thread class in Java and when called, it performs the following functions:
- It sets the thread's interrupt status to
true. - If the thread is currently in a blocking operation, such as
wait(),sleep(), or certainIOoperations, it immediately throws anInterruptedException. This exception must be handled explicitly, allowing the thread to take appropriate action.
Here is the method signature:
Checking and Clearing the Interrupt Status
A thread can check its interrupt status using the isInterrupted() method, which returns true if the thread has been interrupted. Additionally, the static method Thread.interrupted() serves to check the current thread's interrupt status and clear it simultaneously.
Method Signatures:
Example Usage
Below is a simple example that demonstrates how a thread can be interrupted using interrupt().
Detailed Explanation of the Example
- Main Thread: Starts a
RunnableTaskthread and sleeps for a second, simulating time needed to perform other operations. - Interrupt: After the sleep period,
interrupt()is called on theRunnableTaskthread. - RunnableTask Thread:
- Enters a loop while checking that it is not interrupted.
- When interrupted during the
Thread.sleep()call, it catchesInterruptedExceptionand breaks the loop. - Resets the interrupt status with
Thread.currentThread().interrupt()to enforce checking of the interrupted status in subsequent operations.
Considerations and Best Practices
- Responsibility to Stop: It's the responsibility of the thread itself to honor an interrupt request. Code running in the thread should handle interruptions appropriately, particularly in long-running loops or operation.
- Interrupt vs. Cancel: Interruption is a cooperative mechanism, while cancellation is more of a directive. Use interrupts as a suggestion to cease operations, and allow the thread to finish cleanly.
- Avoid Catch-All: Don't catch and supress
InterruptedExceptionwithout properly dealing with it—it should either reset the interrupt status or let the program properly terminate the thread's operation.
Key Points Summary
| Feature | Description |
| Interrupt Mechanism | Signals a thread to stop executing and perform another action. |
interrupt() Behavior | Sets interrupt status to true; throws InterruptedException during blocking operations. |
| Checking Interrupt Status | Use isInterrupted() or Thread.interrupted(). |
Handling InterruptedException | Catch the exception, handle it, or re-thread the interrupt status. |
| Proper Usage | Threads should cooperatively check and gracefully handle interruptions. |
Understanding the interrupt mechanism in Java allows developers to write more responsive and predictable multithreaded applications, making efficient use of system resources and ensuring robust performance under various conditions.

