欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

并发编程陷阱系列(三)使用Thread.interrupt()中断线程

程序员文章站 2022-04-05 22:20:36
...

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