---
title: "Android Thread interrupt 中断JAVA线程"
author: "helpbs"
date: "2011-12-29T08:37:00Z"
tags: []
slug: "android-thread-interrupt-java"
draft: false
lastmod: "2012-04-24T08:51:02Z"
id: 1495943
url: "https://helpbs.iteye.com/blog/1495943"
---

假如我们有一个任务如下，交给一个Java线程来执行，如何才能保证调用interrupt()来中断它呢？

Java代码

01. classATaskimplementsRunnable{
02. privatedoubled =0.0;
03. publicvoidrun() {
04. //死循环执行打印"I am running!" 和做消耗时间的浮点计算
05. while(true) {
06. System.out.println("I am running!");
07. for(inti =0; i &lt;900000; i++) {
08. d = d + (Math.PI + Math.E) / d;
09. }
10. //给线程调度器可以切换到其它进程的信号
11. Thread.yield();
12. }
13. }
14. }
15. publicclassInterruptTaskTest {
16. publicstaticvoidmain(String\[] args)throwsException{
17. //将任务交给一个线程执行
18. Thread t =newThread(newATask());
19. t.start();
20. //运行一断时间中断线程
21. Thread.sleep(100);
22. System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
23. System.out.println("Interrupted Thread!");
24. System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
25. t.interrupt();
26. }
27. }

运行这个程序，我们发现调用interrupt()后，程序仍在运行，如果不强制结束，程序将一直运行下去，如下所示:

Java代码

01. ......
02. I am running!
03. I am running!
04. I am running!
05. I am running!
06. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
07. Interrupted Thread!
08. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
09. I am running!
10. I am running!
11. I am running!
12. I am running!
13. I am running!
14. ....

虽然中断发生了，但线程仍然在进行，离开线程有两种常用的方法：  
抛出InterruptedException和用Thread.interrupted()检查是否发生中断,下面分别看一下这两种方法：  
1.在阻塞操作时如Thread.sleep()时被中断会抛出InterruptedException(注意，进行不能中断的IO操作而阻塞和要获得对象的锁调用对象的synchronized方法而阻塞时不会抛出InterruptedException)

Java代码

01. classATaskimplementsRunnable{
02. privatedoubled =0.0;
03. publicvoidrun() {
04. //死循环执行打印"I am running!" 和做消耗时间的浮点计算
05. try{
06. while(true) {
07. System.out.println("I am running!");
08. for(inti =0; i &lt;900000; i++) {
09. d = d + (Math.PI + Math.E) / d;
10. }
11. //休眠一断时间,中断时会抛出InterruptedException
12. Thread.sleep(50);
13. }
14. }catch(InterruptedException e) {
15. System.out.println("ATask.run() interrupted!");
16. }
17. }
18. }

程序运行结果如下:

Java代码

1. I am running!
2. I am running!
3. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
4. Interrupted Thread!
5. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
6. ATask.run() interrupted!

可以看到中断任务时让任务抛出InterruptedException来离开任务．

2.Thread.interrupted()检查是否发生中断.Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记，所以程序不会两次通知你线程发生了中断．

Java代码

01. classATaskimplementsRunnable{
02. privatedoubled =0.0;
03. publicvoidrun() {
04. //检查程序是否发生中断
05. while(!Thread.interrupted()) {
06. System.out.println("I am running!");
07. for(inti =0; i &lt;900000; i++) {
08. d = d + (Math.PI + Math.E) / d;
09. }
10. }
11. System.out.println("ATask.run() interrupted!");
12. }
13. }

程序运行结果如下：

Java代码

01. I am running!
02. I am running!
03. I am running!
04. I am running!
05. I am running!
06. I am running!
07. I am running!
08. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
09. Interrupted Thread!
10. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
11. ATask.run() interrupted!

我们可结合使用两种方法来达到可以通过interrupt()中断线程.请看下面例子:

Java代码

01. classATaskimplementsRunnable{
02. privatedoubled =0.0;
03. publicvoidrun() {
04. try{
05. //检查程序是否发生中断
06. while(!Thread.interrupted()) {
07. System.out.println("I am running!");
08. //point1 before sleep
09. Thread.sleep(20);
10. //point2 after sleep
11. System.out.println("Calculating");
12. for(inti =0; i &lt;900000; i++) {
13. d = d + (Math.PI + Math.E) / d;
14. }
15. }
16. }catch(InterruptedException e) {
17. System.out.println("Exiting by Exception");
18. }
19. System.out.println("ATask.run() interrupted!");
20. }
21. }

在point1之前处point2之后发生中断会产生两种不同的结果，可以通过修改InterruptTaskTest main()里的Thread.sleep()的时间来达到在point1之前产生中断或在point2之后产生中断.  
如果在point1之前发生中断，程序会在调用Thread.sleep()时抛出InterruptedException从而结束线程．这和在Thread.sleep()时被中断是一样的效果.程序运行结果可能如下：

Java代码

01. I am running!
02. Calculating
03. I am running!
04. Calculating
05. I am running!
06. Calculating
07. I am running!
08. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
09. Interrupted Thread!
10. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
11. Exiting by Exception
12. ATask.run() interrupted!

如果在point2之后发生中断，线程会继续执行到下一次while判断中断状态时．程序运行结果可能如下：

Java代码

01. I am running!
02. Calculating
03. I am running!
04. Calculating
05. I am running!
06. Calculating
07. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
08. Interrupted Thread!
09. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
10. ATask.run() interrupted!