Thread.interrupt()方法不会中断一个正在运行的线程。这一方法
实际上完成的是,在线程受到阻塞时抛出一个中断信号(将Thread的isInterrupted标志位置为1),这样线程就得以退出阻塞的状态。更
确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,
它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
class TestRunnable implements Runnable {
public void run() {
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
//false because after invoking thread.interrupt() interrupt status has been cleared
System.out.println("a:"+Thread.currentThread().isInterrupted());
}
}
}
public class TestThreadInterrupt {
public static void main(String[] args) {
Runnable tr = new TestRunnable();
Thread th1 = new Thread(tr);
th1.start();
th1.interrupt();
}
}