interrupt()方法的应用——中断线程
来源:互联网 发布:阿里云国际站网址 编辑:程序博客网 时间:2024/05/22 01:49
interrupt()方法作用
在JDK1.0中,可以用stop方法来终止,但是现在这种方法已经被禁用了,建议使用判断标志位方式终止线程,或者让线程自然终结。借用interrupt()方法也可以达到使线程终止的目的。
Thread.interrupt()方法本身并不会中断一个正在运行的线程。它的作用是,设置线程的一个域interrupt状态为true;
另外值得注意的是,在线程调用interrupt方法后,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞那么它会抛出一个中断信号,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
使用interrupt()方法结合interrupted()方法、isInterrupted()方法、sleep()方法中断线程
利用interruptedException异常终止线程。
在阻塞操作时如Thread.sleep()时被中断会抛出InterruptedException(注意,进行不能中断的IO操作而阻塞和要获得对象的锁调用对象的synchronized方法而阻塞时不会抛出InterruptedException)
假如我们有一个任务如下,交给一个Java线程来执行。
class ATask implements Runnable{ private double d = 0.0; public void run() { //死循环执行打印"I am running!" 和做消耗时间的浮点计算 try{ while (true) { System.out.println("I am running!"); for (int i = 0; i < 1000000; i++) { d = d + (Math.PI + Math.E) / d; } //休眠一断时间,中断时会抛出InterruptedException Thread.sleep(100); } } catch (InterruptedException e) { System.out.println("ATask.run() interrupted!"); } }}public class InterruptTaskTest { public static void main(String[] args) throws Exception{ //将任务交给一个线程执行 Thread t = new Thread(new ATask()); t.start(); //运行一断时间中断线程 Thread.sleep(100); System.out.println("****************************"); System.out.println("Interrupted Thread!"); System.out.println("****************************"); t.interrupt(); }}
程序运行结果如下:Java代码I am running!I am running!****************************Interrupted Thread!****************************ATask.run() interrupted!
利用interrupted方法作为判断标志位终线程。
Thread.interrupted()方法是静态方法,可以直接判断线程状态,调用之后会清除中断状态(变为false)。isInterrupted是实例方法,不会改变线程状态。
class ATask implements Runnable{ private double d = 0.0; public void run() { //检查程序是否发生中断 while (!Thread.interrupted()) { System.out.println("I am running!"); for (int i = 0; i < 1000000; i++) { d = d + (Math.PI + Math.E) / d; } } System.out.println("ATask.run() interrupted!"); }}
程序运行结果如下:Java代码I am running!I am running!I am running!I am running!I am running!I am running!I am running!****************************Interrupted Thread!****************************ATask.run() interrupted!
- 我们可结合使用两种方法来达到可以通过interrupt()中断线程.请看下面例子:
class ATask implements Runnable{ private double d = 0.0; public void run() { try { //检查程序是否发生中断 while (!Thread.interrupted()) { System.out.println("I am running!"); //point1 before sleep Thread.sleep(20); //point2 after sleep System.out.println("Calculating"); for (int i = 0; i < 900000; i++) { d = d + (Math.PI + Math.E) / d; } } } catch (InterruptedException e) { System.out.println("Exiting by Exception"); } System.out.println("ATask.run() interrupted!"); }}
在point1之前处point2之后发生中断会产生两种不同的结果,可以通过修改InterruptTaskTest main()里的Thread.sleep()的时间来达到在point1之前产生中断或在point2之后产生中断.
如果在point1之前发生中断,程序会在调用Thread.sleep()时抛出InterruptedException从而结束线程.这和在Thread.sleep()时被中断是一样的效果.程序运行结果可能如下:
I am running!CalculatingI am running!CalculatingI am running!CalculatingI am running!****************************Interrupted Thread!****************************Exiting by ExceptionATask.run() interrupted!
如果在point2之后发生中断,线程会继续执行到下一次while判断中断状态时.程序运行结果可能如下:
Java代码I am running!CalculatingI am running!CalculatingI am running!Calculating****************************Interrupted Thread!****************************ATask.run() interrupted!
阅读全文
0 0
- interrupt()方法的应用——中断线程
- interrupt方法中断线程
- java 线程的中断interrupt方法
- 使用interrupt方法中断线程
- 多线程的使用——中断线程详解(Interrupt)
- 多线程的使用——中断线程详解(Interrupt)
- 线程的中断-----使用线程对象的interrupt()方法
- Java之多线程interrupt中断线程的三种方法
- 线程的中断状态 interrupt
- 线程的中断(interrupt)机制
- java线程的中断(interrupt)
- 对Thread.interrupt()方法很详细的介绍 中断线程
- java线程中断,interrupt()方法的一些示例
- Java中用interrupt()方法中断阻塞线程
- 线程中断之interrupt和stop方法
- 中断线程 —— interrupt() 以及对其理解
- Java多线程(二)——线程中断interrupt
- 第二篇 多线程的使用——中断线程详解(Interrupt)
- python——文件的输入输出&&统计词频
- 计算机网络之IP地址
- Maven简介(一)
- Excel函数操作
- JAVA
- interrupt()方法的应用——中断线程
- 11月8日笔记
- 第三讲:c++初步
- 计算机网络之路由器与分组交换
- 帧间预测-AMVP 模式
- TMMi评估方法使用一次需要多少钱?
- 计算机网络之虚拟专用网VPN
- container_of和list_for_each_entry 及其相关函数的分析
- NO.18异常