Java多线程之线程中断

1. API

1.1. void interrupt()

中断线程,其实只是给线程一个中断标志,线程仍会继续运行。

package com.ywq.concurrency.MyTest;

public class TestInterrupt {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while (true) {
                if (!Thread.currentThread().isInterrupted()) {
                    System.out.println("我在运行");
                } else {
                    System.out.println("我现在被打断了");
                }
            }
        });

        t.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t.interrupt();

    }
}

运行结果:

我在运行
我在运行
我在运行
我在运行
我在运行
我在运行
我现在被打断了
我现在被打断了
我现在被打断了
我现在被打断了
我现在被打断了
我现在被打断了
我现在被打断了
我现在被打断了
.......

由这个例子可以看出,线程中断后并不是停止运行,只是给了它一个中断的信号,中断状态变为了true。

1.2. static boolean interrupted()

测试当前线程是否已经中断。如果线程处于中断状态,返回true,否则返回false,同时还会清除线程的中断状态。所以,如果连续两次调用该方法,第二次一定会返回false。

package com.ywq.concurrency.MyTest;

public class TestInterrupt {
    public static void main(String[] args) {
        Thread t = new Thread();
        t.start();
        t.interrupt();
        
        System.out.println(Thread.interrupted());
        System.out.println(Thread.interrupted());

    }
}

运行结果

false
false

两次调用均返回false,因为是子线程调用了interrupt()方法,进入中断状态,main线程并没有中断,验证了上面说的interrupted()方法是返回当前线程的中断状态。

package com.ywq.concurrency.MyTest;

public class TestInterrupt {
    public static void main(String[] args) {
        Thread t = new Thread();
        t.start();
        
        Thread.currentThread().interrupt();
        System.out.println(Thread.interrupted());
        System.out.println(Thread.interrupted());
    }
}

运行结果

true
false

这里在主方法中让当前线程也就是main线程调用interrupt()方法,再调用两次interrupted(),第一次返回true,第二次返回false,验证了上面说的调用该方法会清除线程的中断状态。

1.3. boolean isInterrupted()

测试调用该方法的对象所表示的那个线程是否已经中断。线程的中断状态不受该方法影响。

package com.ywq.concurrency.MyTest;

public class TestInterrupt {
    public static void main(String[] args) {
        Thread t = new Thread();
        t.start();

        t.interrupt();

        System.out.println(t.isInterrupted());
        System.out.println(t.isInterrupted());

        System.out.println(Thread.currentThread().isInterrupted());
        System.out.println(Thread.currentThread().isInterrupted());
    }
}

运行结果

true
true
false
false

前两次返回结果都为true,说明isInterrupted()方法并没有清除中断状态的功能。后两次返回结果都是false,因为当前的main线程并没有被中断,所以肯定是返回false啊。这也说明哪个对象调用isInterrupted()方法,返回的就是这个对象表示的线程的中断状态。

2. 中断阻塞线程和非阻塞线程

中断非阻塞线程,不抛出异常

当线程阻塞时中断线程,会抛出InterruptedException异常,并且清除线程的中断状态。

package com.ywq.concurrency.MyTest;

public class TestInterrupt2 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            try{
                Thread.sleep(10000);
            }catch(InterruptedException e){
                e.printStackTrace();
                System.out.println(Thread.currentThread().isInterrupted());
            }

        });

        t.start();
        t.interrupt();
    }
}

运行结果

false
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at com.ywq.concurrency.MyTest.TestInterrupt2.lambda$main$0(TestInterrupt2.java:7)
	at java.lang.Thread.run(Thread.java:748)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值