java-Thread的interrupt笔记
2018-02-05 17:14
351 查看
1.interrupt() 中断线程
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
也就是只有在wait,join,sleep的时候才能抛出interruptedException,并且清除了中断状态,其他情况不会抛出中断异常
通过代码证明下
a.第一种情况 在不是wait,join,sleep的时候调用interrupt方法
执行结果:
====false
main 线程执行完毕
====true
^^^^^^^
从结果可以看出并没有抛出interruptException,并且curr.isInterrupted方法返回了ture,说明中断状态也没有被清除
b.在sleep的时候在调用interrupte方法看下什么情况,run方法加下sleep
执行结果:
====false
main 线程执行完毕
java.lang.InterruptedException: sleep interrupted
===false
at java.lang.Thread.sleep(Native Method)
at a.Interrupt1$ThreadTest.run(Interrupt1.java:22)
at java.lang.Thread.run(Unknown Source)
确实抛出了interruptedException,并且isinterrupted()返回了false说明中断状态被清除了,后面的判断就不管用了,所以拿这个方法中断线程还是不安全,自己用变量控制更靠谱
2.静态方法public static boolean
interrupted()
* Tests whether the current thread has been interrupted. The
* <i>interrupted status</i> of the thread is cleared by this method. In
* other words, if this method were to be called twice in succession, the
* second call would return false (unless the current thread were
* interrupted again, after the first call had cleared its interrupted
* status and before the second call had examined it).
检查当前中断状态,并且清除中断状态,连续调用2此的第二次肯定返回false
执行结果:
====false
main 线程执行完毕
java.lang.InterruptedException: sleep interrupted
先变成true===true
curr.isInterrupted===true
====bfalse
curr.isInterrupted===false
====cfalse
at java.lang.Thread.sleep(Native Method)
at a.Interrupt1$ThreadTest.run(Interrupt1.java:22)
at java.lang.Thread.run(Unknown Source)
内部调用是这样的
public boolean isInterrupted() {
return isInterrupted(false);
}
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
private native boolean isInterrupted(boolean ClearInterrupted);
isInterrupted不清除,静态方法的清除
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
也就是只有在wait,join,sleep的时候才能抛出interruptedException,并且清除了中断状态,其他情况不会抛出中断异常
通过代码证明下
a.第一种情况 在不是wait,join,sleep的时候调用interrupt方法
public class Interrupt1 { public static void main(String[] args) throws InterruptedException { ThreadTest thread=new ThreadTest(); Thread t=new Thread(thread); t.start(); Thread.sleep(1000); t.interrupt(); System.out.println("main 线程执行完毕"); } public static class ThreadTest implements Runnable{ public void run() { Thread curr=Thread.currentThread(); System.out.println("===="+curr.isInterrupted()); while(true){ if(curr.isInterrupted()){ System.out.println("===="+curr.isInterrupted()); break; } if(1==2) break; } System.out.println("^^^^^^^"); } } }
执行结果:
====false
main 线程执行完毕
====true
^^^^^^^
从结果可以看出并没有抛出interruptException,并且curr.isInterrupted方法返回了ture,说明中断状态也没有被清除
b.在sleep的时候在调用interrupte方法看下什么情况,run方法加下sleep
Thread curr=Thread.currentThread(); System.out.println("===="+curr.isInterrupted()); while(true){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("==="+curr.isInterrupted()); } if(curr.isInterrupted()) System.out.println("000000000"); if(1==2) break; } System.out.println("^^^^^^^");
执行结果:
====false
main 线程执行完毕
java.lang.InterruptedException: sleep interrupted
===false
at java.lang.Thread.sleep(Native Method)
at a.Interrupt1$ThreadTest.run(Interrupt1.java:22)
at java.lang.Thread.run(Unknown Source)
确实抛出了interruptedException,并且isinterrupted()返回了false说明中断状态被清除了,后面的判断就不管用了,所以拿这个方法中断线程还是不安全,自己用变量控制更靠谱
2.静态方法public static boolean
interrupted()
* Tests whether the current thread has been interrupted. The
* <i>interrupted status</i> of the thread is cleared by this method. In
* other words, if this method were to be called twice in succession, the
* second call would return false (unless the current thread were
* interrupted again, after the first call had cleared its interrupted
* status and before the second call had examined it).
检查当前中断状态,并且清除中断状态,连续调用2此的第二次肯定返回false
Thread curr=Thread.currentThread(); System.out.println("===="+curr.isInterrupted()); while(true){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); curr.interrupt(); System.out.println("先变成true==="+curr.isInterrupted()); System.out.println("curr.isInterrupted==="+Thread.interrupted()); System.out.println("中断状态清除:"+curr.isInterrupted()); System.out.println("curr.isInterrupted==="+Thread.interrupted()); System.out.println("====c"+curr.isInterrupted()); } if(curr.isInterrupted()) System.out.println("000000000"); if(1==2) break; } System.out.println("^^^^^^^"); }
执行结果:
====false
main 线程执行完毕
java.lang.InterruptedException: sleep interrupted
先变成true===true
curr.isInterrupted===true
====bfalse
curr.isInterrupted===false
====cfalse
at java.lang.Thread.sleep(Native Method)
at a.Interrupt1$ThreadTest.run(Interrupt1.java:22)
at java.lang.Thread.run(Unknown Source)
内部调用是这样的
public boolean isInterrupted() {
return isInterrupted(false);
}
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
private native boolean isInterrupted(boolean ClearInterrupted);
isInterrupted不清除,静态方法的清除
相关文章推荐
- think in java笔记: Thread的interrupt()方法
- 学习笔记5: java中Thread类中的interrupt()(补充)
- java.lang.ThreadGroup学习笔记
- Java Thread.interrupt 害人! 中断JAVA线程(zz)
- Java笔记(4)-线程、Thread、Runable、开子线程、线程运行状态、线程同步、线程死锁
- Java Thread.interrupt方法
- JAVA Thread的中断机制(interrupt)
- Java中的线程Thread方法之---interrupt()
- 【java】Thread 常搞混的几个概念sleep、wait、yield、interrupt
- 学习笔记4:java中Thread类的方法小结
- Java中的线程Thread方法之---interrupt()
- Java Thread.interrupt 害人! 中断JAVA线程
- Java Thread 学习笔记1
- java学习笔记-Thread
- Java_Thread_interrupt中断线程
- 学习笔记2:java中Thread类与线程的创建
- 学习java多线程的笔记1--Thread(Runnable t)与重写run()方法等
- Java基础知识强化之多线程笔记05:Java中继承thread类 与 实现Runnable接口的区别
- Java并发08:Thread的基本方法(5)-interrupt()、isInterrupted()
- 大数据课程体系-学习笔记-第一阶段-Java Thread