中断线程的方法
2017-05-14 16:50
218 查看
尊重个人劳动成果,转载请注明出处:
http://blog.csdn.net/czd3355/article/details/72026801
异常法【推荐】
输出结果:
中断线程还有以下两种方法:
在 sleep 状态下中断线程
使用 return 中断线程
但由于与异常法相比稍逊一筹,所以在此也就不再多介绍了。详细可参考《java多线程编程核心技术》第一章
http://blog.csdn.net/czd3355/article/details/72026801
异常法【推荐】
public class Run { public static void main(String[] args) { try { StopThread01 thread01 = new StopThread01(); thread01.start(); Thread.sleep(1000); thread01.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("main end"); } } public class StopThread01 extends Thread { @Override public void run() { super.run(); try { for (int i = 0; i < 500000; i++) { if (Thread.interrupted()) { System.out.println("子线程已经被标记为中断状态了。。。"); throw new InterruptedException(); } System.out.println("i= " + i); } System.out.println("for 循环外部,若子线程没被停止则会执行该语句。。。"); } catch (InterruptedException e) { System.out.println("run catch"); e.printStackTrace(); } } }
输出结果:
中断线程还有以下两种方法:
在 sleep 状态下中断线程
使用 return 中断线程
但由于与异常法相比稍逊一筹,所以在此也就不再多介绍了。详细可参考《java多线程编程核心技术》第一章
相关文章推荐
- Response.End方法导致线程中断
- 有感于线程中断的一个方法(原创)
- Java之多线程interrupt中断线程的三种方法
- 使用interrupt方法中断线程
- 终止线程的2种方法。标记法和中断
- JAVA中断线程的方法
- Thread线程中断相关方法
- 线程中断 interrupt() 与 线程终止方法
- Java中断线程的方法
- Java多线程-通过线程的中断来深入学习interrupt方法,Volatile关键字
- 关键字this、super、final、static、synchronized及 中断线程的方法
- Android中断线程的处理方法
- 线程中断方法interrupt() 与 cancel()
- Android中断并重启一个Thread线程的简单方法
- Java 中断线程方法
- JAVA中断线程的方法
- 线程中断之interrupt和stop方法
- 让每一步都跳出,这样不破坏整体流程,可以对结果随心所欲的控制(线程中断也可采用这种方法)
- 线程中断机制,精确控制方法执行
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期