高级牛人带你详细解读Java 线程中断怎么做(附代码)

本文深入探讨了Java线程中断的概念,分析了`Thread.stop`被废弃的原因,强调了中断并不直接结束线程,而是唤醒阻塞的线程。通过实例展示了如何使用中断标志和`interrupt()`方法安全退出线程,提供了多种线程退出的实现方式,帮助读者理解并正确使用线程中断机制。
摘要由CSDN通过智能技术生成

一、前言

大家肯定都使用过 Java 线程开发(Thread / Runnable),启动一个线程的做法通常是:

new Thread(new Runnable(
 @Override
 public void run() {
   
  // todo sth...
 }
)).start();

然而线程退出,大家是如何做的呢?一般做法可能不外乎以下两种:

  1. 设置一个标志位:true / false 来退出;
  2. 强制退出:thread.stop;(我相信,现在应该没人会使用这种方式了,因为JDK也很早就废弃了该方法)

可能还会有人提出,我可以用中断来退出线程! 我只能说:Too Young Too Simple!中断并不会使得线程结束而退出,中断(interrupt)只是唤醒被阻塞的线程而已。

本篇,我们就来好好的聊聊:线程中断,以及如何正确的使用线程中断,和正确的线程退出。

二、为何 Thread.stop 被废弃

This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it 
has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the 
objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to 
other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply 
modifies some variable to indicate that the target thread should stop running. The target thread should check this 
variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop 
running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method 
should be used to interrupt the wait.

以上是官方 JDK 中的源码注释说明,其含义如下:

Thread.stop 方法天生就不安全。使用该方法来停止线程,将会导致其它因为监视器锁『监视器我们在 synchronized 中就讲过,是 Java 的内置锁』而被锁住的线程全部都解锁!(本质的后果是:没有检查的 ThreadDeath 异常会在栈中传播,因而使得监视器锁解锁)。如果任何一个被监视器锁给锁住的对象处于一个不一致的状态,那么其被解锁后将会被其它线程可见,潜在的结果是产生任何后果。我们应该使用一个变量来代替使用 stop 方法,告诉目标线程退出『这里就是我们开头所说的第一种方法,设置一个标志位』。目标线程应该周期性的检查这个变量,并根据这个变量来正确的退出 run 方法。如果目标线程处于阻塞/休眠状态(如:使用 wait、sleep、yield 方法后,线程让出了 CPU 使用权,进而阻塞/休眠),此时,该标志位变量将不会起作用,那么,应该使用 interrupt 方法来中断目标线程的阻塞/休眠状态,将其唤醒!

对于 ThreadDeath 对象,官方还有补充:

  1. 线程可以在几乎任何地方抛出 ThreadDeath 异常。由于这一点,所有的同步方法和(代码)块将必须被考虑得事无巨细。
  2. 线程在清理第一个 ThreadDeath 异常的时候(在 catch 或 finally 语句中),可能会抛出第二个。清理工作将不得不重复直到到其成功。保障这一点的代码将会很复杂。
    所以,我们也别想着去 try-catch ThreadDeath Exception!

同样,被废弃的还有 Thread.resume 和 Thread.suspend。这俩方法有造成死锁的危险:

  1. 使用suspend时,并不会释放锁;
  2. 如果存在某种情况要先获取该锁,再进行resume,那么就造成死锁了;
  3. 取代这两方法的正确方式是:Object.wait 和 Object.notify :
    因为 Object.wait 进入阻塞时,会释放锁。

三、线程中断的含义

Thread 中有三个与中断相关的方法:

  1. 成员方法 interrupt():设置线程中断标志为 true ;
  2. 成员方法 isInterrupted():获取线程的中断状态,默认为 false,调用 interrupt() 后,该方法返回 true;
  3. 静态方法 Thread.interrupted():获取线程的中断状态,并且清除中断状态(设置为 false);
    注:如果线程中断后,连续两次调用 Thread.interrupted(),第一次是 true & 清除状态,第二次结果是 false。

3.1、初步了解

我们先来通过一个例子来初步了解 thread.interrupt :

public class InterruptDemo implements Runnable {
   
 @Override
 public void run() {
   
  while (true) {
   
   System.out.println("Thread running...");
  }
 }

 public static void main(String[] args) throws InterruptedException {
   
  Thread thread = new Thread(new InterruptDemo(), "InterruptDemo");

  System.out.println("start thread");
  thread.start();

  Thread.sleep(50);
  System.out.println("interrupt thread");
  thread.interrupt()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值