iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Java中出现线程中断的原因有哪些
  • 359
分享到

Java中出现线程中断的原因有哪些

2023-06-06 11:06:57 359人浏览 泡泡鱼
摘要

这篇文章将为大家详细讲解有关Java中出现线程中断的原因有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、前言大家肯定都使用过 Java 线程开发(Thread / Runnable)

这篇文章将为大家详细讲解有关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 threadThread running...Thread running.........interrupt threadThread running...Thread running.........thread's status = trueThread 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 threadThread will sleep 10s ------------------------- runninginterrupt threadmain exitthread interrupted...Thread run, total sleep = 3002(ms)Thread will sleep 10s ------------------------- runningThread run, total sleep = 10002(ms)Thread will sleep 10s ------------------------- running

我们可以看到,线程启动后,进入睡眠(10s),3秒后被中断唤醒,执行完一个 while 后再次进入第二次睡眠(10s),然后周而复始。

3.3、一般标志位法退出线程

public class InterruptDemo implements Runnable { private static final AtomicBoolean running = new AtomicBoolean(true); @Override public void run() {  while (running.get()) {   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();  running.set(false);  System.out.println("main exit"); }}

输出结果:

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

我们通过使用一个 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 threadThread run, total sleep = 0(ms)Thread run, total sleep = 0(ms)Thread run, total sleep = 0(ms)main exitThread 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 threadThread will sleep 10s ------------------------- runninginterrupt threadmain exitInterrupted... Todo other things then exit......Thread exit

关于Java中出现线程中断的原因有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

--结束END--

本文标题: Java中出现线程中断的原因有哪些

本文链接: https://www.lsjlt.com/news/246414.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java中出现线程中断的原因有哪些
    这篇文章将为大家详细讲解有关Java中出现线程中断的原因有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、前言大家肯定都使用过 Java 线程开发(Thread / Runnable)...
    99+
    2023-06-06
  • java中出现string的原因有哪些
    在Java中,出现字符串的原因可能有以下几种: 字符串字面量:字符串字面量是由双引号括起来的字符序列,表示一个字符串对象。在代码...
    99+
    2023-10-23
    java string
  • Java内存中出现OOM的原因有哪些
    Java内存中出现OOM的原因有哪些?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一,jvm内存区域1,程序计数器一块很小的内存空间,作用是当前线程所执行的字节码的行号指示器。...
    99+
    2023-05-31
    java oom ava
  • java出现oom的原因有哪些
    Java中出现OOM(Out Of Memory)的原因有以下几种:1. 堆内存不足:Java程序在运行时会将对象分配到堆内存中,当...
    99+
    2023-10-11
    java
  • MySQL中出现死锁的原因有哪些
    这篇文章给大家介绍MySQL中出现死锁的原因有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。  MySQL死锁问题原因有哪些  1、MySQL常用存储引擎的锁机制  MyISAM和...
    99+
    2024-04-02
  • Java中多线程的中断机制有哪些
    本篇文章为大家展示了Java中多线程的中断机制有哪些,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1、interrupt()public void interrupt()&nbs...
    99+
    2023-05-30
    java 多线程
  • 云服务器出现网络中断异常的原因有哪些
    云服务器出现网络中断异常的原因有:1.机房线路维护不到位,带宽插口松动;2.网络出口异常,运营商网络异常;3.设备故障,路由错误;4.网络攻击,DDoS、CC攻击等;具体分析如下:机房线路维护不到位服务器带宽是从服务器机房里面接入的。机房会...
    99+
    2024-04-02
  • 香港服务器出现网络中断的常见原因有哪些
    香港服务器出现网络中断的常见原因有:1、电信网出入口出现异常导致网络中断;2、香港服务器硬件配置出现故障问题导致;3、香港服务器的开关电源出现故障问题导致;4、香港服务器机器设备出现故障导致;5、香港服务器正在被大规模网络攻击导致无法正常运...
    99+
    2024-04-02
  • 美国服务器出现网络中断的常见原因有哪些
    美国服务器出现网络中断的常见原因有:1、出口异常,运营商网络异常;2、硬件故障,硬件设备老化;3、电源故障,电源短路或不可抗力因素造成;4、安全攻击,DDoS 等高强度流量攻击;具体分析如下:出口异常由于运营商网络异常的缘故,有时会出现大陆...
    99+
    2024-04-02
  • SSL服务器中断的原因有哪些
    SSL服务器中断的原因可能有以下几种:1. 证书问题:服务器证书过期、无效或不匹配,导致SSL握手失败。2. 密码套件问题:客户端和...
    99+
    2023-08-25
    SSL服务器 服务器
  • pycharm中import出错的原因有哪些
    在PyCharm中,import出错的原因可能包括: 模块不存在:如果导入的模块不存在,或者没有正确安装,就会导致import出...
    99+
    2023-10-23
    pycharm
  • Node.js中出现内存泄漏的原因有哪些
    本篇文章给大家分享的是有关Node.js中出现内存泄漏的原因有哪些,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。内存泄漏(Memory  Leak)指由于疏忽或错误造...
    99+
    2023-06-17
  • php出现gd2中文乱码的原因有哪些
    这篇文章主要介绍php出现gd2中文乱码的原因有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!php出现gd2中文乱码的原因:1、服务器的编码与gd2中的“imagettftext()”函数的默认编码不同,函数默...
    99+
    2023-06-14
  • linux出现killed的原因有哪些
    这篇文章主要介绍“linux出现killed的原因有哪些”,在日常操作中,相信很多人在linux出现killed的原因有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux出现killed的原因有哪些...
    99+
    2023-07-02
  • python出现nan的原因有哪些
    在Python中,nan代表"not a number",通常出现在数学运算中,表示无效的数字或不可用的数字。以...
    99+
    2024-04-08
    python
  • java堆栈溢出的原因有哪些
    Java堆栈溢出的主要原因包括:1. 递归调用:如果一个方法在执行过程中不断地调用自身,而没有终止条件或者终止条件不满足时,就会导致...
    99+
    2023-10-10
    java
  • java内存溢出的原因有哪些
    Java内存溢出的原因主要有以下几点:1. 无限循环:当程序中存在无限循环或递归调用时,会导致内存溢出。每次循环或递归调用都会产生一...
    99+
    2023-08-11
    java
  • 拨号VPS中出现691错误的原因有哪些
    小编给大家分享一下拨号VPS中出现691错误的原因有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!用户名或密码错误有的时候往往会忽略了用户名和密码,错了会出现...
    99+
    2023-06-04
  • redis是单线程的原因有哪些
    小编给大家分享一下redis是单线程的原因有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、Redis为什么是单线程的?因...
    99+
    2024-04-02
  • java中groovy报错的原因有哪些
    在Java中使用Groovy时,可能会出现以下几种常见的错误原因: Groovy语法错误:Groovy语法与Java有所不同,可...
    99+
    2024-03-11
    java
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作