This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

This article is from chaoCode. Please send a personal message and link to the author and the original address at the beginning of the article.

Violators, the author reserves the right to pursue.

preface

Today we’re going to talk briefly about how to terminate threads.

If you are not familiar with how threads are created, it is recommended to watch concurrent programming — The 6 States of Java threads and their switching. If you are not familiar with how threads are created, it is recommended to watch concurrent Programming — The starting of threads

Termination of the thread

How to terminate a thread

1. Use marker variables

We create a custom Thread class by inheriting Thread, where the live property is used to control whether the Thread terminatesUse the main method to test, start the thread, delay it for 20 milliseconds, and then change the live so that it breaks out of the loop, continues down, and completes the run, so that a thread terminates naturally.The following is the result of the execution, and it is ready to terminate when the result breaks out of the loop as we expected.2. Use the stop method

When we first look at the source code for Stop, we see that the @deprecated annotation is used to indicate that it is not recommended and is Deprecated. This method can still achieve the purpose of terminating the thread, but it is unsafe, or violent interruption of execution, resulting in incomplete code, incomplete logic, and release of all locked resources, affecting atomic operations, data inconsistency. It is not recommended if you do not know how the current thread is performing. The JDK documentation also includes an article explaining Why these methods are Deprecated: Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?

Why stop is deprecated:

  1. Calling stop() immediately stops all remaining work in the run() method, including ina catch or finally statement, and throws a ThreadDeath exception (which normally doesn’t need to show the catch), thus potentially leaving some clear work unfinished. Closure of files, databases, etc.
  2. Calling stop() immediately releases all locks held by the thread, resulting in data inconsistency that is not synchronized.

3. Use interrupt

There are three methods related to interrupt in the Thread class: interrupt(), Interrupted(),isInterrupted(). Here’s a look at the differences between these three methods and how to use them.

isInterrupted()So we can sort of guess through object-oriented thinking, and then we see that the return value is a Boolean, and we know in all likelihood that the method is to get the value of the current interrupt signal.

If you go further, you will find that the method called by the native method accepts a parameter indicating whether or not the Interrupted flag was cleared by the operator, but isInterrupted() passes a false, indicating that the flag will not be cleared by the operator.

The following test results show that this method does not clear the Interrupted flag after the thread is Interrupted, or true.

interrupted()

For a start, we see that this method is a static method, as interrupted () also calls the isInterrupted(true) method, but it passes true, indicating that the interrupt flag will be cleared. The following test results show that this method will clear the Interrupted flag after the thread is Interrupted.

interrupt()

The first two are ways to tell if an interrupt is there, and interrupt () is the way to actually trigger an interrupt. To interrupt a thread is to set the thread’s identity bit to true.

Interrupt () the source code:Using isInterrupted() test code:Run result diagram: We find that after calling interrupt(), the flag bit becomes true, breaking out of the loop and achieving an interrupt ready-made purpose.

Using the interrupted() test code:

Run result diagram: We find that after interrupt() is called, the flag bit becomes true, enters the loop a second time, finds a nonconformance, breaks out of the loop, and achieves an interrupt ready-made purpose. But using Interrupted () will clear the flag, so the last print will be false.

These are the three methods to be clear, this section may ask you in the interview the difference between the three methods.

Thank you for watching, and feel free to let us know in the comments section if you have any mistakes. If this post has helped you, feel free to like it at 👍.