There are three ways to stop a thread in Java:
- Custom interrupt identifier to stop the thread.
- Stops a thread with the thread interrupt method.
- Use stop to stop the thread.
The “stop” method is Deprecated. It is not recommended to use the “stop” method because it will stop the thread directly, so that the thread does not have enough time to process the saving work before stopping. As a result, the data will be incomplete. There are also some problems with custom interrupt flags, so in summary, the interrupt method is the ideal way to stop a thread. Let’s look at the differences.
1. Customize interrupt identifiers
Custom interrupt identifier is to define a variable in the program to determine whether to interrupt the execution of the thread, the implementation code is as follows:
class FlagThread extends Thread {
// Custom interrupt identifier
public volatile boolean isInterrupt = false;
@Override
public void run(a) {
// If true -> abort execution
while(! isInterrupt) {// Business logic processing}}}Copy the code
But the problem with custom interrupt identifiers is that threads don’t interrupt quickly enough. Because the thread is executing, it cannot call while(! IsInterrupt) to determine whether the thread is terminated. It can only determine whether to terminate the current thread on the next run, so it does not interrupt the thread in a timely manner, such as the following code:
class InterruptFlag {
// A custom interrupt identifier
private static volatile boolean isInterrupt = false;
public static void main(String[] args) throws InterruptedException {
// Create interruptible thread instances
Thread thread = new Thread(() -> {
while(! isInterrupt) {// Stop the thread if isInterrupt=true
System.out.println("Thread perform Step 1: The thread is about to go to sleep");
try {
/ / sleep 1 s
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread executes Step 2: The thread executes the task"); }}); thread.start();// Start the thread
// Sleep for 100ms and wait for the thread to run
Thread.sleep(100);
System.out.println("Main thread: Attempts to terminate thread");
// Modify interrupt identifier to interrupt thread
isInterrupt = true; }}Copy the code
The execution result of the above code is shown below:We expect that thread execution after step 1, receive interrupt thread’s instructions, then you don’t step 2 again, but can be seen from the above results, using a custom interrupt identifier is unable to achieve expected results, it is the custom interrupt identifier, problems timely response.
Threads are interrupted
It does not interrupt the thread directly. Instead, it sends a signal to interrupt the thread, leaving it up to the writer to decide whether or not the thread is being interrupted. Compared with the custom interrupt identifier, it can receive interrupt instructions in a more timely manner, as shown in the following code:
public static void main(String[] args) throws InterruptedException {
// Create interruptible thread instances
Thread thread = new Thread(() -> {
while(! Thread.currentThread().isInterrupted()) { System.out.println("Thread perform Step 1: The thread is about to go to sleep");
try {
/ / sleep 1 s
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(The thread receives the interrupt instruction and performs the interrupt operation.);
// Interrupts the task execution of the current thread
break;
}
System.out.println("Thread executes Step 2: The thread executes the task"); }}); thread.start();// Start the thread
// Sleep for 100ms and wait for the thread to run
Thread.sleep(100);
System.out.println("Main thread: Attempts to terminate thread");
// Modify interrupt identifier to interrupt thread
thread.interrupt();
}
Copy the code
The execution result of the above code is shown below:As can be seen from the above results,The thread interrupts the thread immediately after receiving the interrupt instruction. Compared with the previous method, it can respond to the interrupt instruction in a more timely manner.
3. Stop Stops the thread
The stop method can stop a Thread, but it is not recommended to use the deprecated method. This can be seen from the source code of the Thread class.As you can see from the image above, the stop method is Deprecated and Deprecated, and the first sentence of the comment states that the stop method is an unsafe method. In the latest version of Java, this method has been removed directly, so it is strongly discouraged.
conclusion
This article describes three ways to stop a thread:
- The disadvantage of this method is that it cannot respond to the interrupt request in time.
- Use the interrupt thread method, which sends an interrupt signal to the thread so that it can respond promptly to the interrupt and is the most recommended method.
- Finally, there is the stop method, which, although it can also stop a thread, is an outdated and deprecated method and has been removed directly in the latest version of Java, so it is not recommended.
Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.
Public number: Java interview analysis
Interview collection: gitee.com/mydb/interv…