Home > Java > javaTutorial > body text

How to solve the problem of high concurrency InterruptedException in java

WBOY
Release: 2023-04-30 08:46:06
forward
1617 people have browsed it

Preface

InterruptedException may not be as simple as you think!

When we call the wait() method of a Java object or the sleep() method of a thread, we need to catch and handle the InterruptedException exception. If we improperly handle InterruptedException, unexpected consequences will occur!

Program Case

For example, in the following program code, the InterruptedTask class implements the Runnable interface. In the run() method, the handle of the current thread is obtained, and in the while(true) loop, Use the isInterrupted() method to detect whether the current thread is interrupted. If the current thread is interrupted, exit the while(true) loop. At the same time, in the while(true) loop, there is also a line of Thread.sleep(100) code and capture InterruptedException exception.

The entire code is as follows.

package io.binghe.concurrent.lab08;
/**
 * @author binghe
 * @version 1.0.0
 * @description 线程测试中断
 */
public class InterruptedTask implements Runnable{
    @Override
    public void run() {
        Thread currentThread = Thread.currentThread();
        while (true){
            if(currentThread.isInterrupted()){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
Copy after login

The original intention of the above code is to check whether the thread has been interrupted through the isInterrupted() method, and if it is interrupted, exit the while loop. Other threads interrupt the execution thread by calling the interrupt() method of the execution thread. At this time, the interrupt flag bit of the execution thread is set, so that currentThread.isInterrupted() returns true, so that the while loop can be exited.

This seems to be no problem! But is this really the case? We create an InterruptedTest class for testing, the code is as follows.

package io.binghe.concurrent.lab08;
/**
 * @author binghe
 * @version 1.0.0
 * @description 测试线程中断
 */
public class InterruptedTest {
    public static void main(String[] args){
        InterruptedTask interruptedTask = new InterruptedTask();
        Thread interruptedThread = new Thread(interruptedTask);
        interruptedThread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        interruptedThread.interrupt();
    }
}
Copy after login

We run the main method as shown below.

How to solve the problem of high concurrency InterruptedException in java

Problem Analysis

The above code clearly calls the thread's interrupt() method to interrupt the thread, but it has no effect. The reason is that when the run() method of the thread is executed, it is blocked on sleep(100) most of the time. When other threads interrupt the execution thread by calling the interrupt() method of the execution thread, InterruptedException will be triggered with a high probability. Exception, when the InterruptedException exception is triggered, the JVM will clear the interrupt flag bit of the thread at the same time. Therefore, the currentThread.isInterrupted() judged in the run() method at this time will return false, and the current while loop will not exit. .

Now that the problem analysis has been cleared, How to interrupt the thread and exit the program?

Problem Solving

The correct way to handle it should be after catching the exception in the while(true) loop in the run() method in the InterruptedTask class Reset the interrupt flag bit, so the correct code for the InterruptedTask class is as follows.

package io.binghe.concurrent.lab08;
/**
 * @author binghe
 * @version 1.0.0
 * @description 中断线程测试
 */
public class InterruptedTask implements Runnable{
    @Override
    public void run() {
        Thread currentThread = Thread.currentThread();
        while (true){
            if(currentThread.isInterrupted()){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
                currentThread.interrupt();
            }
        }
    }
}
Copy after login

As you can see, we have added a new line of code in the catch code block that captures the InterruptedException exception.

currentThread.interrupt();
Copy after login

This allows us toafter catching the InterruptedException exception, we can reset the interrupt flag bit of the thread, thereby interrupting the currently executing thread.

We run the main method of the InterruptedTest class again, as shown below.

How to solve the problem of high concurrency InterruptedException in java

Summary

Be careful when handling InterruptedException. If an InterruptedException is thrown when calling the interrupt() method of the execution thread to interrupt the execution thread, When the InterruptedException exception is triggered, the JVM will clear the interrupt flag bit of the execution thread at the same time. At this time, when the isInterrupted() method of the execution thread is called, false will be returned.

At this time, the correct way to handle it is to capture the InterruptedException exception in the run() method of the execution thread and reset the interrupt flag bit (that is, in the catch code block that captures the InterruptedException exception, re-call the current Thread's interrupt() method).

The above is the detailed content of How to solve the problem of high concurrency InterruptedException in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!