1.在阻塞操作时如Thread.sleep()时被中断会抛出InterruptedException(注意,进行不能中断的IO操作而阻塞和要获得对象的锁调用对象的synchronized方法而阻塞时不会抛出InterruptedException)
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- //死循环执行打印"I am running!" 和做消耗时间的浮点计算
- try {
- while (true) {
- System.out.println("I am running!");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- //休眠一断时间,中断时会抛出InterruptedException
- Thread.sleep(50);
- }
- } catch (InterruptedException e) {
- System.out.println("ATask.run() interrupted!");
- }
- }
- }
程序运行结果如下:
- I am running!
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- ATask.run() interrupted!
可以看到中断任务时让任务抛出InterruptedException来离开任务.
2.Thread.interrupted()检查是否发生中断.Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记,所以程序不会两次通知你线程发生了中断.
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- //检查程序是否发生中断
- while (!Thread.interrupted()) {
- System.out.println("I am running!");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- }
- System.out.println("ATask.run() interrupted!");
- }
- }
程序运行结果如下:
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- ATask.run() interrupted!
我们可结合使用两种方法来达到可以通过interrupt()中断线程.请看下面例子:
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- try {
- //检查程序是否发生中断
- while (!Thread.interrupted()) {
- System.out.println("I am running!");
- //point1 before sleep
- Thread.sleep(20);
- //point2 after sleep
- System.out.println("Calculating");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- }
- } catch (InterruptedException e) {
- System.out.println("Exiting by Exception");
- }
- System.out.println("ATask.run() interrupted!");
- }
- }
在point1之前处point2之后发生中断会产生两种不同的结果,可以通过修改InterruptTaskTest main()里的Thread.sleep()的时间来达到在point1之前产生中断或在point2之后产生中断.
如果在point1之前发生中断,程序会在调用Thread.sleep()时抛出InterruptedException从而结束线程.这和在Thread.sleep()时被中断是一样的效果.程序运行结果可能如下:
- I am running!
- Calculating
- I am running!
- Calculating
- I am running!
- Calculating
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- Exiting by Exception
- ATask.run() interrupted!
如果在point2之后发生中断,线程会继续执行到下一次while判断中断状态时.程序运行结果可能如下:
- I am running!
- Calculating
- I am running!
- Calculating
- I am running!
- Calculating
- ****************************
- Interrupted Thread!
- ****************************
- ATask.run() interrupted!
. interrupt()只是改变中断状态而已
interrupt()不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态,所以线程A不会抛出InterruptedException,而会一直执行着自己的操作.当线程A终于执行到wait(),sleep(),join()时,才马上会抛出InterruptedException.
若没有调用sleep(),wait(),join()这些方法,或是没有在线程里自己检查中断状态自己抛出InterruptedException的话,那InterruptedException是不会被抛出来的.