• 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
  • 相关阅读:
    如何将英文PDF文献翻译成中文
    基于颜色的R2V软件快速矢量化
    ArcGIS下如何提取研究区域
    ArcGIS 如何设置地图显示范围大小
    基于GIS的空间分析功能分析芝加哥小熊队和白袜队的球迷范围
    C#中的字段,常量,属性与方法
    ArcGIS中的连接和关联表
    使用docker搭建Samba共享目录
    Docker国内镜像源的切换
    pl/sql中的取模运算
  • 原文地址:https://www.cnblogs.com/lvjianwei/p/10577495.html
Copyright © 2020-2023  润新知