Java Thread isInterrupted() Method
Example
Check if the main thread is interrupted, before and after interrupt() is called:
public class Main {
public static void main(String[] args) {
Thread thread = Thread.currentThread();
System.out.println(thread.isInterrupted());
thread.interrupt();
System.out.println(thread.isInterrupted());
}
}
Definition and Usage
The isInterrupted() method checks if a thread has been interrupted, which means that interrupt() was called on it.
It does not change the interrupted status of the thread. A thread can check it in a loop, to find out if it should stop working.
Tip: The static Thread.interrupted() method also checks the current thread, but it sets the status back to false.
Syntax
public boolean isInterrupted()
Parameter Values
None.
Technical Details
| Returns: | true if the thread has been interrupted, otherwise false. |
|---|
Related Pages
❮ Thread Methods