zoukankan      html  css  js  c++  java
  • Java interrupt 中断

    为什么要中断?

    根据需要停止某些持续的方法,这些方法可以被中断,所以又被称为可中断方法,包括:

    Object的wait(), wait(long), wait(long, int),

    Thraed的sleep(long), sleep(long, int), join(), join(long), join(long, int),

    InterruptibleChannel的io操作,

    Selector的wakeup方法,

    其他方法。

    如何中断?

    Thread的三个方法:

    方法 功能 特点
    public void interrupt() 中断当前线程 更新中断标记为true,抛出InterruptedException
    public void static boolean isInterrupted() 检查线程中断标记 返回查中断标记,可用于判断是否被调用中断
    public boolean interrupted() 检查线程中断标记,可能更新 返回中断标记,若为true则更新为false。

     

     

    当线程被中断时,会将中断标记置为true,并抛出InterruptedException异常,当捕获到这个异常时,说明当前线程的中断方法被调用。

    如果在可中断方法执行前,调用了interrupt()方法,那么当执行到可中断方法时,会立刻抛出InterruptedException。

    线程中catch (InterruptedException ex)会导致中断标志被擦除。

    示例

    interrupt:

    public static void main(String[] args) throws InterruptedException {
    
            Thread thread = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            TimeUnit.MINUTES.sleep(2);
                        } catch (InterruptedException ex) {
                            System.out.println("I am interrupted");
                            
                        }
    
                    }
                }
            };
    
            thread.start();
            TimeUnit.MICROSECONDS.sleep(2);
            thread.interrupt();       
    }
    output:
    I am interrupted

    interrupted:

    public static void main(String[] args) throws InterruptedException {
    
            Thread thread = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        //移除sleep和try-catch,因为可中断方法中断时,会将中断标志置为false
                    }
                }
            };
    
            thread.start();
            TimeUnit.MICROSECONDS.sleep(2);
            System.out.println("before invoke interrupte,thread is interrupted:" + thread.isInterrupted());
            thread.interrupt();
            System.out.println("after invoke interrupte is interrupted:" + thread.isInterrupted());       
    }
    output:
    before invoke interrupte,thread is interrupted:false
    after invoke interrupte is interrupted:true

    iterrupted

    public static void main(String[] args) throws InterruptedException {
    
            Thread thread = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        System.out.println("thread is interrupted:" + Thread.interrupted());
                    }
                }
            };
            thread.setDaemon(true);
            thread.start();
            TimeUnit.MICROSECONDS.sleep(2);
            System.out.println("before invoke interrupte,thread is interrupted:" + thread.isInterrupted());
            thread.interrupt();
            System.out.println("after invoke interrupte is interrupted:" + thread.isInterrupted());
            TimeUnit.MICROSECONDS.sleep(1);
            System.out.println("finally thread is interrupted:" + thread.isInterrupted());
    
        }

    输出:

    output:
    thread is interrupted:false
    before invoke interrupte,thread is interrupted:false
    thread is interrupted:false
    after invoke interrupte is interrupted:true
    thread is interrupted:true
    thread is interrupted:false
    finally thread is interrupted:false

    可中断方法执行前调用interrupt

    public static void main(String[] args) {
    
            System.out.println("thread is interrupted:" + Thread.interrupted());
    
            Thread.currentThread().interrupt();
            System.out.println("after interrupt,do something...");
            try {
                TimeUnit.MICROSECONDS.sleep(2);
            } 
            catch (InterruptedException e) {
                System.out.println("thread is interrupted:" + Thread.currentThread().isInterrupted());
            }
        }

    输出:

    thread is interrupted:false
    after interrupt,do something...
    thread is interrupted:false
  • 相关阅读:
    JAVA面试问题与解答(1-15)
    17个经典的Spring面试问答
    Linux下载——下载文件的命令
    MySQL入门——在Linux下安装和卸载MySQL
    MySQL入门——在Linux下安装和卸载MariaDB
    MySQL入门——MySQL数据库和SQL语言
    Linux性能分析——分析系统性能相关的命令
    Linux网络——查看网络连接情况的命令
    Linux网络——配置网络之iproute家族命令
    Linux网络——配置网络之ifconfig家族命令
  • 原文地址:https://www.cnblogs.com/lvjianwei/p/10577495.html
Copyright © 2011-2022 走看看