If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. 。
如果线程调用了wait ,jion, sleep方法而阻塞->>t.interrupt() -->将会抛出一个InterruptedException,异常抛出后会把刚才设置的中断状态标识也清除掉。
public class InterruptDemo { public static void main(String[] args) { Thread ex = new ExThread(); ex.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } ex.interrupt(); } } class ExThread extends Thread { @Override public void run() { super.run(); while (!Thread.currentThread().isInterrupted()) { System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("InterruptedException异常:" + isInterrupted());//这里是false,因为异常抛出后,会清除中断状态的标识。所以While循环条件依旧满足,线程继续在那run.. } } } }运行结果:
ExThread 运行中:false
ExThread 运行中:false
java.lang.InterruptedException: sleep interrupted
InterruptedException异常:false at java.lang.Thread.sleep(Native Method)
at com.thread.ExThread.run(InterruptDemo.java:28)
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
如果我们在catch (InterruptedException e) 中再设置一下 this.interrupt();
public class InterruptDemo { public static void main(String[] args) { Thread ex = new ExThread(); ex.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } ex.interrupt(); } } class ExThread extends Thread { @Override public void run() { super.run(); while (!Thread.currentThread().isInterrupted()) { System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("InterruptedException异常:" + isInterrupted()); this.interrupt(); System.out.println("是否中断:" + this.isInterrupted()); } } } }
运行结果如下:
ExThread 运行中:falseExThread 运行中:falseInterruptedException异常:false是否中断:truejava.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.thread.ExThread.run(InterruptDemo.java:28)
看吧,线程的whlie停止了。把上面的代码稍微改下:
public class InterruptDemo { public static void main(String[] args) { Thread ex = new ExThread(); ex.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } ex.interrupt(); } } class ExThread extends Thread { @Override public void run() { super.run(); while (!Thread.currentThread().isInterrupted()) { System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("InterruptedException异常:" + isInterrupted()); this.interrupt(); <span style="color:#ff0000;"> System.out.println("是否中断:" + Thread.interrupted()); System.out.println("isInterrupted:" + this.isInterrupted());</span> } } } }
运行结果变成:
ExThread 运行中:false
ExThread 运行中:false
java.lang.InterruptedException: sleep interrupted
InterruptedException异常:false
是否中断:true
isInterrupted:false
ExThread 运行中:false
at java.lang.Thread.sleep(Native Method)
at com.thread.ExThread.run(InterruptDemo.java:28)
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
为嘛呢? 因为Thread.interrupted()如果当前线程isAlive()不仅会返回当前线程的的中断状态值,并且会清除它,就是说返回true,后把它改为false.