interrupt()、interrupted()方法的使用和线程退出的时机
2011-04-15 21:45
351 查看
当你在线程上调用interrupt()时,线程中断发生的唯一时刻是在任务要进入到可中断的阻塞操作(如sleep,wait, join等)中,或者已经在阻塞操作内部时。此时线程会抛出InterruptedException,并从异常中退出。如果run()方法内部恰巧没有可以产生阻塞的部分,则run()方法可能就无法被interrupt方法中断。此时,可以调用interrupted()方法判断线程的终止状态,并根据状态控制线程的中断。
package Concurrent; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class InterruptedClass implements Runnable { @Override public void run() { double d = 0.0f; try { while(!Thread.interrupted()) { System.out.println("Running in InterrupedClass"); // before sleep, point 1 Thread.sleep(2000); // before calculating, point 2 System.out.println("Calculating..."); for (int i = 0; i < 900000; i++) { d = d + (Math.PI + Math.E) / d; } System.out.println("After calculating"); } }catch(InterruptedException e) { System.out.println("Exiting via InterruptedException"); }finally { System.out.println("In finally!"); } } } public class Interrupt { public static void main(String[] args) throws Exception { ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new InterruptedClass()); Thread.sleep(1000); // 如果睡眠1000ms,则在point1处之前就调用了shutdownNow,即调用了interrupt // 因此再调用sleep方法时会抛出异常并从catch处退出 //Thread.sleep(2001); // 如果睡眠2001ms,则在point2处调用interrupt,即在正常运行处调用interrupt // 因此会执行完循环中的大数计算,再词运行到while(!Thread.interrupted())处 // 时才回中断,退出while,不抛出异常 // finally或catch中可以添加释放占用资源的代码 exec.shutdownNow(); } }
相关文章推荐
- thinkinjava21.4.4----java中interrupt和interrupted方法的使用和线程退出的时机
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(一)
- java线程中的interrupt,isInterrupt,interrupted方法
- 使用interrupt()方法和isInterrupted()方法实现了线程
- 正确得到线程退出信息的方法详解-变量存储退出信息结构、使用动态存储的方式退出信息结构、使用全局变量方式推相互信息结构、使用main函数中的局部变量存储退出信息结构
- 关于java线程的interrupt方法的使用总结
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(二)
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- 使用退出标志终止线程时,终止方法里使用类名和this的区别
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- 使用interrupt方法中断线程
- java线程中的interrupt,isInterrupt,interrupted方法
- Java线程--interrupt join yield setDaemon常用方法的使用
- 线程的创建同步退出案例和使用方法
- java线程中的interrupt,isInterrupt,interrupted方法
- 转:java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法