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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值