Home  >  Article  >  Java  >  How to solve the problem of high concurrency InterruptedException in java

How to solve the problem of high concurrency InterruptedException in java

WBOY
WBOYforward
2023-04-30 08:46:061660browse

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();
            }
        }
    }
}

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();
    }
}

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();
            }
        }
    }
}

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

currentThread.interrupt();

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete