java.lang.InterruptedException
来源:互联网 发布:mac安装什么杀毒软件 编辑:程序博客网 时间:2024/06/14 15:12
线程的interrupt()调用不管是在该线程的阻塞方法调用前或调用后,都会导致该线程抛出InterruptedException;
(1)interrupt调用在阻塞方法调用前;
public class InterruptTest {public static class TestThread extends Thread{public volatile boolean go = false;public void run(){test();}private synchronized void test(){System.out.println("running");while(!go){}try {if(isInterrupted()){System.out.println("Interrupted");}wait();} catch (InterruptedException e) {e.printStackTrace();System.out.println("InterruptedException");}}}/** * @param args */public static void main(String[] args) {TestThread thread = new TestThread();thread.start();thread.interrupt();thread.go = true;}}输出:
running
Interrupted
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at InterruptTest$TestThread.test(InterruptTest.java:20)
at InterruptTest$TestThread.run(InterruptTest.java:6)
(2)interrupt调用在阻塞方法调用后;
public class InterruptTest {public static class TestThread extends Thread{public volatile boolean go = false;public void run(){test();}private synchronized void test(){System.out.println("running");try {if(isInterrupted()){System.out.println("Interrupted");}wait();} catch (InterruptedException e) {e.printStackTrace();System.out.println("InterruptedException");}}}/** * @param args */public static void main(String[] args) {TestThread thread = new TestThread();thread.start();try {Thread.currentThread().sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}thread.interrupt();}}输出:
running
InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at InterruptTest$TestThread.test(InterruptTest.java:20)
at InterruptTest$TestThread.run(InterruptTest.java:6)
0 0
- java.lang.InterruptedException
- java.lang.InterruptedException: sleep interrupted
- Java线程之InterruptedException
- Java如何处理InterruptedException?
- HDFS上传文件之WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException
- InterruptedException
- Java线程之 InterruptedException 异常
- Java正确处理InterruptedException的方法
- java 线程 stop sleep interruptedException
- Java正确处理InterruptedException的方法
- Java正确处理InterruptedException的方法
- JAVA中怎样处理InterruptedException
- Java正确处理InterruptedException的方法
- Java线程之 InterruptedException 异常
- Java线程之 InterruptedException 异常
- Java Thread.sleep的InterruptedException
- Java 理论与实践: 处理 InterruptedException
- Java 理论与实践: 处理 InterruptedException
- java提高篇—hashMap的底层原理(01)
- Android学习之 图解调用invalidate()和requestLayout()的过程
- 第三方登录
- ListView属性
- Java单例模式中双重检查锁的问题
- java.lang.InterruptedException
- 安装和测试Kafka
- 流和文件(文件的保存)
- 《機器學習實戰》學習筆記
- 时间工具类
- 在oschian上面建立自己的开源项目livefeng的过程
- 关于C++的模板类
- test
- java中abstract详解