About thread termination methods Interrupt ()
Since the stop() method is outdated and obsolete, it is a flawed design in the JDK, so it is common to use interrupt() to terminate a thread. However, interrupt() does not terminate a thread as violently as stop(). Terminate the loop immediately. Calling interrupt() simply marks the current thread with a stop mark. It does not actually stop the thread. Let’s start with a simple example:
public class InterruptDemo extends Thread {
@Override
public void run() {
for (int i = 0; i < 500000; i++) {
System.out.println("i="+ (i + 1)); } } public static void main(String[] args) { try { InterruptDemo interruptDemo = new InterruptDemo(); interruptDemo.start(); Thread.sleep(2000); interruptDemo.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); }}}Copy the code
The result is as follows:
Difference between thread.interrupted () and this.isinterrupted ()
The JDK in Java provides two methods for determining whether a thread is in a stopped state:
Thread.interrupted(): tests whether the current Thread has been interrupted this.isinterrupted (): tests whether the Thread has been interruptedCopy the code
What’s the difference between these two methods? Let’s take a look at the JDK source code:
/** * Tests whether the current thread has been interrupted. The * <i>interrupted status</i> of the thread is cleared by this method. In * other words,if this method were to be called twice in succession, the
* second call would return false (unless the current thread were
* interrupted again, after the first call had cleared its interrupted
* status and before the second call had examined it).
*
* <p>A thread interruption ignored because a thread was not alive
* at the time of the interrupt will be reflected by this method
* returning false.
*
* @return <code>true</code> if the current thread has been interrupted;
* <code>false</code> otherwise.
* @see #isInterrupted()
* @revised 6.0
*/
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
/**
* Tests whether this thread has been interrupted. The <i>interrupted
* status</i> of the thread is unaffected by this method.
*
* <p>A thread interruption ignored because a thread was not alive
* at the time of the interrupt will be reflected by this method
* returning false.
*
* @return <code>true</code> if this thread has been interrupted;
* <code>false</code> otherwise.
* @see #interrupted()
* @revised 6.0
*/
public boolean isInterrupted() {
return isInterrupted(false);
}
/**
* Tests ifsome Thread has been interrupted. The interrupted state * is reset or not based on the value of ClearInterrupted that is * passed. */ private native boolean isInterrupted(boolean ClearInterrupted);Copy the code
First we see that both methods call isInterrupted(Boolean ClearInterrupted). This means that if a thread has been interrupted, Whether the interrupted status is reset depends on the value of ClearInterrupted. If ClearInterrupted is true, the interrupted status is reset, but if ClearInterrupted is false, the interrupted status is not reset.
If interrupted() is static, use thread.interrupted (), which calls currentThread().isinterrupted (true), That is, it returns the status value of whether the current thread has been interrupted, and there is a mechanism to clean up the interrupted state. IsInterrupted () is an instance method, so this.isinterrupted () calls isInterrupted(false), which returns the status of whether the thread has been interrupted, In contrast to the Thread.interrupted() method, it has no mechanism for cleaning up the interrupted state. (PS: It doesn’t matter if you don’t understand it here, we will use examples to verify it later)
1. Thread.interrupted()
Let’s first look at the thread.interrupted () method, as shown in the following example:
public class InterruptDemo1 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 50000; i++) {
System.out.println("i=" + (i + 1));
}
}
public static void main(String[] args) {
InterruptDemo1 thread=new InterruptDemo1();
thread.start();
try {
Thread.sleep(1000);
thread.interrupt();
System.out.println("Have you stopped 1? ="+Thread.interrupted());
System.out.println("Have you stopped 2? ="+Thread.interrupted());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end!");
}
Copy the code
The result is as follows:
In this case, the thread does not terminate, which proves that the interrupted() method tests whether the current thread has interrupted. This “current thread” is main, which has never interrupted, so both results are false. How do I terminate the main thread?
public class InterruptDemo2 {
public static void main(String[] args) {
Thread.currentThread().interrupt();
System.out.println("Stop 1? ="+Thread.interrupted());
System.out.println("Stop 2? ="+Thread.interrupted());
System.out.println("end!"); }}Copy the code
The result is as follows:
Stop 1? =trueStop 2? =false
end!
Copy the code
The interrupted() method does determine if the current thread is interrupted(stopped) as a result of its execution. If interrupted() is called twice in a row, the second call will return false (after the interrupt status has been cleared in the first call and before the interrupt status has been checked in the second call, Unless the current thread interrupts again).
2. this.isInterrupted()
After thread.interrupted (), let’s look at this.isinterrupted () :
public class InterruptDemo3 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 50000; i++) {
System.out.println("i=" + (i + 1));
}
}
public static void main(String[] args) {
InterruptDemo3 thread = new InterruptDemo3();
thread.start();
try {
Thread.sleep(1000);
thread.interrupt();
System.out.println("Have you stopped 1? =" + thread.isInterrupted());
System.out.println("Have you stopped 2? =" + thread.isInterrupted());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end!"); }}Copy the code
Execution Result:
This means that isInterrupted() does not clear the interrupted status flag, which is consistent with the source code.
conclusion
- Interrupted() is a static method that uses Thread.interrupted(), whereas isInterrupted() is an instance method that uses the Thread’s instance.
- Thread.interrupted() : Checks whether the current Thread is interrupted and clears the status flag to false.
- This.isinterrupted () : tests whether the Thread object isInterrupted, but does not clear the status flag.
reference
Java Multithreaded Programming Core Technology