Home > Java > javaTutorial > How to use java interrupt() to interrupt blocking

How to use java interrupt() to interrupt blocking

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-05-02 15:55:18
forward
1855 people have browsed it

Explanation

1. Calling interrupt() can interrupt blocking. Interrupting blocking does not mean the end of the thread's life cycle, but only interrupts the blocking state of the current thread.

2. Once interrupted in the blocking state, an InterruptedException exception will be thrown. This exception is like a signal to notify that the current thread has been interrupted.

Example

public static void main(String[] args) throws InterruptedException{
    Thread thread = new Thread(()->{
        try{
            TimeUnit.SECONDS.sleep(10);
        }catch (InterruptedException e){
            System.out.println("Thread is interrupted.");
        }
    });
    thread.start();
    TimeUnit.SECONDS.sleep(1);
    thread.interrupt();
}
Copy after login

The above is the detailed content of How to use java interrupt() to interrupt blocking. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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