阿里p9详解Java 线程中断怎么办

这篇文章主要介绍了Java 线程中断的相关资料,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下

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

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

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

  • 设置一个标志位:true / false 来退出;
  • 强制退出: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 对象,官方还有补充:

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

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

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

因为 Object.wait 进入阻塞时,会释放锁。

三、线程中断的含义
Thread 中有三个与中断相关的方法:

成员方法 interrupt():设置线程中断标志为 true ;
成员方法 isInterrupted():获取线程的中断状态,默认为 false,调用 interrupt() 后,该方法返回 true;
静态方法 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();
 
  Thread.sleep(50);
  System.out.println("thread's status = " + thread.isInterrupted());
 }
}

输出结果:

start thread
Thread running...
Thread running...
......
interrupt thread
Thread running...
Thread running...
......
thread's status = true
Thread running...
......

我们可以看到,即便我们调用了 thread.interrupt 方法,线程也并没有退出,仍旧继续运行。因此,这个例子证明了一点:我们并不能通过"我们所认为的"中断来试图"结束"正在运行的线程。

3.2、中断即唤醒阻塞/休眠的线程
同样,我们再来看一个例子:

public class InterruptDemo implements Runnable {
 @Override
 public void run() {
  while (true) {
   System.out.println("Thread will sleep 10s ------------------------- running");
   long timestamp = System.currentTimeMillis();
   try {
    Thread.sleep(10000);
   } catch (InterruptedException e) {
    System.out.println("thread interrupted...");
   }
 
   timestamp = System.currentTimeMillis() - timestamp;
   System.out.println("Thread run, total sleep = " + timestamp + "(ms)");
  }
 }
 
 public static void main(String[] args) throws InterruptedException {
  Thread thread = new Thread(new InterruptDemo(), "InterruptDemo");
 
  System.out.println("start thread");
  thread.start();
  Thread.sleep(3000);
  System.out.println("interrupt thread");
  thread.interrupt();
  System.out.println("main exit");
 }
}

输出结果:

start thread
Thread will sleep 10s ------------------------- running
interrupt thread
main exit
thread interrupted...
Thread run, total sleep = 3002(ms)
Thread will sleep 10s ------------------------- running
Thread run, total sleep = 10002(ms)
Thread will sleep 10s ------------------------- running

我们通过使用一个 AtomicBoolean 变量来当作标志位,使得我们的线程能正常退出。 我们也可以判断线程是否被中断而选择性的退出。

3.4、线程中断退出

public class InterruptDemo implements Runnable {
 @Override
 public void run() {
  while (!Thread.currentThread().isInterrupted()) {
   long timestamp = System.currentTimeMillis();
   timestamp = System.currentTimeMillis() - timestamp;
   System.out.println("Thread run, total sleep = " + timestamp + "(ms)");
  }
  System.out.println("Thread exit");
 }
 
 public static void main(String[] args) throws InterruptedException {
  Thread thread = new Thread(new InterruptDemo(), "InterruptDemo");
 
  System.out.println("start thread");
  thread.start();
  Thread.sleep(100);
  System.out.println("interrupt thread");
  thread.interrupt();
  System.out.println("main exit");
 }
}

输出结果:

start thread
.......
Thread run, total sleep = 0(ms)
interrupt thread
Thread run, total sleep = 0(ms)
Thread run, total sleep = 0(ms)
Thread run, total sleep = 0(ms)
main exit
Thread exit

3.5、标志位 + 线程中断结合

public class InterruptDemo implements Runnable {
 private static final AtomicBoolean running = new AtomicBoolean(true);
 
 @Override
 public void run() {
  while (running.get()) {
   System.out.println("Thread will sleep 10s ------------------------- running");
   long timestamp = System.currentTimeMillis();
   try {
    Thread.sleep(10000);
   } catch (InterruptedException e) {
    System.out.println("Interrupted... Todo other things then exit......");
    running.set(false);
    continue;
   }
   timestamp = System.currentTimeMillis() - timestamp;
   System.out.println("Thread run, total sleep = " + timestamp + "(ms)");
  }
  System.out.println("Thread exit");
 }
 
 public static void main(String[] args) throws InterruptedException {
  Thread thread = new Thread(new InterruptDemo(), "InterruptDemo");
 
  System.out.println("start thread");
  thread.start();
  Thread.sleep(3000);
  System.out.println("interrupt thread");
  thread.interrupt();
  System.out.println("main exit");
 }
}

输出结果:

start thread
Thread will sleep 10s ------------------------- running
interrupt thread
main exit
Interrupted... Todo other things then exit......
Thread exit

** 四、总结**
本文我们分析了线程的中断,并让大家了解了中断的含义:只是告诉该线程,你被『中断』了,至于你想干嘛,还是由你自己来决定。同时,我们也简单分析了几个废弃的方法的原因。希望大家学习了本文之后,能正确且合理的设计,线程如何安全的退出。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,509评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,093评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,632评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,848评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,855评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,353评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,738评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,706评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,240评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,268评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,398评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,012评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,709评论 3 337
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,178评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,312评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,932评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,464评论 2 365

推荐阅读更多精彩内容