Home > Java > javaTutorial > body text

InterruptedException in Java - solution to thread interruption exception

王林
Release: 2023-06-25 11:15:07
Original
2310 people have browsed it

InterruptedException in Java - Solution to thread interruption exception

In Java multi-thread programming, thread interruption exception is a common problem and a problem that requires attention. When a thread is running and another thread wants to interrupt it, an InterruptedException is thrown. This article will discuss the causes and solutions of InterruptedException.

  1. Cause of InterruptedException

InterruptedException is an exception thrown due to the thread being interrupted. While a thread is running, another thread can interrupt it through the interrupt() method. If the interrupted thread is in a waiting state, such as waiting for IO operations or waiting for a lock, an InterruptedException will be thrown.

For example, in the following code, when thread t1 is executing Thread.sleep() to sleep, thread t2 interrupts it, so t1 will throw an InterruptedException exception.

Thread t1 = new Thread(() -> {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        System.out.println("Thread interrupted");
    }
});
t1.start();

Thread t2 = new Thread(() -> {
    t1.interrupt();
});
t2.start();
Copy after login
  1. Solution to InterruptedException

When a thread throws InterruptedException, we need to handle it according to the specific situation. Normally, we should call Thread.currentThread().interrupt() in the catch block to re-interrupt the thread so that the upper caller knows that the thread has been interrupted.

For example, in the following code, after thread t1 finishes executing, it will check whether it has been interrupted and re-interrupt itself in the catch block.

Thread t1 = new Thread(() -> {
    try {
        Thread.sleep(5000);
        System.out.println("Thread finished");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        System.out.println("Thread interrupted");
    }
});
t1.start();

Thread t2 = new Thread(() -> {
    t1.interrupt();
});
t2.start();
Copy after login

In addition, if the thread is performing some operations that require cleaning up resources, such as releasing locks or closing files, we should also perform cleaning operations in the catch block. For example, in the following code, after thread t1 completes execution, it will release resources and check whether it has been interrupted.

Thread t1 = new Thread(() -> {
    Lock lock = new ReentrantLock();
    lock.lock();
    try {
        Thread.sleep(5000);
        System.out.println("Thread finished");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        System.out.println("Thread interrupted");
    } finally {
        lock.unlock();
    }
});
t1.start();

Thread t2 = new Thread(() -> {
    t1.interrupt();
});
t2.start();
Copy after login

In short, defining an InterruptedException exception handling method is very critical. It can help us correctly handle thread interruption operations and reduce the complexity of the code as much as possible.

The above is the detailed content of InterruptedException in Java - solution to thread interruption exception. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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!