软件编程
位置:首页>> 软件编程>> java编程>> Java线程中断interrupt的常用方法

Java线程中断interrupt的常用方法

作者:smileluck  发布时间:2021-08-08 18:31:07 

标签:Java,线程中断,interrupt

前言

这里主要探讨中断常用的三个方法:

  • interrupt()。在一个线程中调用需要中断现成的interrupt()方法,会对该线程发出信号,将中断状态标志为true

  • isInterrupted()。判断当前线程的中断状态。

  • interrupted()。将线程的中断状态恢复。

主要使用的阻塞三个方法:

  • Object#wait。放弃锁+等待+重新获取锁

  • Thread#join。【协作】等待某个线程执行完毕

  • Thread#sleep。静态方法,线程休眠并让出CPU时间片

==注意:interrupt()不能中断在运行中的线程,它只能改变中断状态而已。实际完成的是让受阻塞的线程退出阻塞状态。==
确切的说:是被三种方法之一阻塞时,调用该线程的interrupt()方法,那么线程将抛出一个个InterruptedException中断异常,从而提早地终结被阻塞状态。

示例说明

public class Runner3 implements Runnable {
? ? @Override
? ? public void run() {
? ? ? ? while (true) {
? ? ? ? ? ? if (Thread.currentThread().isInterrupted()) {
? ? ? ? ? ? ? ? System.out.println("我进入中断了,但我还在跑");
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? System.out.println("我没有进入中断");
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public static void main(String[] args) {

? ? ? ? Runner3 runner3 = new Runner3();
? ? ? ? Thread thread3 = new Thread(runner3);
? ? ? ? thread3.start();

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

? ? ? ? thread3.interrupt();
? ? }
}

输出结果大致如下:

我没有进入中断
我没有进入中断
我进入中断了,但我还在跑
我进入中断了,但我还在跑
我进入中断了,但我还在跑
...

这里看到,执行interrupt()后,对线程执行中断后依然在执行,线程依然在运行。

我们调整一下run方法

public void run() {
       while (true) {
           if (Thread.currentThread().isInterrupted()) {
               System.out.println("我进入中断了,但我还在跑");
               Thread.interrupted();//重置状态
           } else {
               System.out.println("我没有进入中断");
           }
       }
   }

输出结果如下:

我没有进入中断
我没有进入中断
我进入中断了,但我还在跑
我没有进入中断
我没有进入中断
...

这里看到中断的状态重置了,那么我们如何去应用这个中断状态呢?

注意事项

  • 当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,中断状态已经被系统复位了,线程A调用Thread.interrupted()返回的是false。

  • 如果线程被调用了interrupt(),此时该线程并不在阻塞状态时,下次执行wait(),sleep(),join()时,一样会抛出InterruptedException,当然抛出后该线程的中断状态也会被系统复位。

案例1

public class Runner3 implements Runnable {
? ? @Override
? ? public void run() {
? ? ? ? while (true) {
? ? ? ? ? ? if (Thread.currentThread().isInterrupted()) {
? ? ? ? ? ? ? ? System.out.println("我进入中断了,但我还在跑");
// ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Thread.sleep(5000);
? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? System.out.println("2"+Thread.currentThread().isInterrupted());
? ? ? ? ? ? ? ? ? ? //输出false
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? System.out.println("我没有进入中断");
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? }

? ? public static void main(String[] args) {
? ? ? ? Runner3 runner3 = new Runner3();
? ? ? ? Thread thread3 = new Thread(runner3);
? ? ? ? thread3.start();
? ? ? ? try {
? ? ? ? ? ? Thread.sleep(2000);
? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? thread3.interrupt();
? ? }
}

执行上面的代码,我们可以看到在抛出异常后,Thread.currentThread().isInterrupted()输出为false,证明线程的中断状态已经复位了。
另外因为我们是先执行了interrupt()然后再进入睡眠状态,但是依然抛出了异常。

Object#wait 和 Thread.sleep 差异在哪里

因为Object#wait方法会阻塞线程,所以当我们执行interrupt时,会抛出InterruptedException异常。
那么Object#wait方法阻塞线程会导致的差异在哪里?
==最主要的差别在于sleep方法没有释放锁,而wait方法释放了锁,使得其它线程可以使用同步控制块或者方法。==

总结

  • 调用interrupt方法,会改变中断状态,但不会影响线程的运行状态。

  • 当执行了interrupt方法改变中断状态后,线程若执行Object#wait,Thread#sleep和Thread#join都会抛出InterruptedException异常,然后复位中断状态

  • 当执行了interrupt方法改变中断状态后,线程未阻塞,且将要执行Object#wait,Thread#sleep和Thread#join阻塞线程时,都会抛出InterruptedException异常,复位中断状态。

来源:https://juejin.cn/post/7107797736384118815

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com