---
title: "并发编程陷阱系列(三)使用Thread.interrupt()中断线程"
author: "Copperfield"
date: "2013-04-02T10:26:32Z"
tags: []
categories: ["编程语言"]
slug: "thread-interrupt"
draft: false
lastmod: "2013-04-01T17:12:22Z"
id: 1839640
url: "https://copperfield.iteye.com/blog/1839640"
---

Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是，在线程受到阻塞时抛出一个中断信号，这样线程就得以退出 阻塞的状态。更确切的说，如果线程被Object.wait, Thread.join,Thread.sleep三种方法之一阻塞，那么，它将接收到一个中断异常（InterruptedException），从而提早地终结被阻塞状态。

 

建议使用外部的布尔变量进行控制，比如：

```
class MyThread extends Thread
{
  volatile boolean finished = false;

  public void stopMe()
  {
    finished = true;
  }

  public void run()
  {
    while (!finished)
    {
      //do dirty work
    }
  }
}
```

   

 

```
public class AlternateStop implements Runnable {
	private volatile boolean stopRequested;
	private Thread runThread;

	public void run() {
		runThread = Thread.currentThread();
		stopRequested = false;
		int count = 0;
		while (!stopRequested) {
			System.out.println("Running ... count=" + count);
			count++;
			try {
				Thread.sleep(300);
			} catch (InterruptedException x) {
				System.out.println("捕获异常后---"+Thread.currentThread().isInterrupted());
				Thread.currentThread().interrupt(); // re-assert interrupt
				System.out.println("重新设置打断状态---"+Thread.currentThread().isInterrupted());
			}
		}

		System.out.println("stoped");
	}

	public void stopRequest() {
		stopRequested = true;
		if (runThread != null) {
			runThread.interrupt();
		}
	}

	public static void main(String[] args) {
		AlternateStop as = new AlternateStop();
		Thread t = new Thread(as);
		t.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException x) {
			// ignore
		}
		as.stopRequest();
	}
}
```

 注意当一个阻塞方法检测到中断并抛出 InterruptedException 时，它清除中断状态，为了让上层代码知道    该线程的状态，重新设置打断状态。

```
System.out.println("捕获异常后---"+Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt(); // re-assert interrupt
System.out.println("重新设置打断状态---"+Thread.currentThread().isInterrupted());
```

   以上三行代码会打印：

  捕获异常后---false  
  重新设置打断状态---true