Java中的interrupt、interrupted和isInterrupted方法区别详解

 更新时间:2023年12月16日 09:47:43   作者:小晨想好好学习  
这篇文章主要介绍了Java中的interrupt、interrupted和isInterrupted方法区别详解,interrupt用于中断线程,调用该方法的线程的状态将会被设置为中断状态,线程中断仅仅是设置线程的中断状态位,并不会停止线程,需要用户自己去监视线程的状态并作出处理,需要的朋友可以参考下

一、interrupt

interrupt用于中断线程。调用该方法的线程的状态将会被设置为“中断状态”。 【注意】线程中断仅仅是设置线程的中断状态位,并不会停止线程。需要用户自己去监视线程的状态并作出处理

1、打断正常运行的线程

public class InterruptTest {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i++);
                }
            }
        });
        t1.start();
        System.out.println("主线程正常运行中.....");
        System.out.println("打断t1线程");
        t1.interrupt();
        System.out.println("t1线程的打断标记:" + t1.isInterrupted());
        System.exit(0);
    }
}

在这里插入图片描述

【结论】t1线程被打断后,t1线程并没有中止,而是继续运行,只是将打断标记设置为true

2、使用interrupt打断sleep中的线程

使用interrupt打断sleep中的线程,会抛出异常,并且清除打断标记

private static void test1() throws InterruptedException {
        Thread t1 = new Thread(()->{
            sleep(1);
        }, "t1");
        t1.start();
        sleep(0.5);
        t1.interrupt();
        log.debug(" 打断状态: {}", t1.isInterrupted());
    }

在这里插入图片描述

3、使用interrupt打断join中的线程

使用interrupt打断join中的线程,会抛出异常,但是打断标记还是会置为true

private static void test11() throws InterruptedException {
        Thread t1 = new Thread(()->{
            int i = 0;
            while (i < 9999999) {
                i++;
            }
        }, "t1");
        Thread t2 = new Thread(()->{
            try {
                t1.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("等待t1线程执行结束");
        }, "t2");
        t1.start();
        t2.start();
        t2.interrupt();
        log.debug("t2打断状态: {}", t2.isInterrupted());
    }

在这里插入图片描述

4、打断 park 线程

之后补充,先了解下 park() 方法的使用

二、isInterrupted

查询当前线程的中断标志位是true还是false,利用中断标志位来结束线程

public class InterruptTest {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                // 当t1线程被打断后,条件不满足,t1线程执行结束
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(i++);
                }
            }
        });
        t1.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t1.interrupt();
    }
}

三、interrupted

interrupted是静态方法,查看线程中断信号是true还是false,并且清除中断信号;如果一个线程被中断了,第一次调用interrupted返回的是true,第二次调用则返回的是false

public class InterrupttedTest {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i++);
                    System.out.println("查看线程的中断标志位:"+ Thread.interrupted());
                }
            }
        });
        t1.start();
        t1.interrupt();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t1.stop();
    }
}

在这里插入图片描述

到此这篇关于Java中的interrupt、interrupted和isInterrupted方法区别详解的文章就介绍到这了,更多相关interrupt、interrupted和isInterrupted方法区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论