线程中断之interrupt和stop方法
2014-04-03 20:49
274 查看
从阿里的笔试和网易游戏的电面中暴露了关于线程并发方面基础知识掌握的不到位, 现在从头再来学习一遍。
首先我们想中断一个线程,可以使用interrupt和stop两种方式。
首先说interrupt, 它没有stop那么的粗暴,因为可以用catch捕捉到InterruptedException这个异常
这里主函数中10000ms后打断了这个线程
输出如下:
如果使用stop方法,则更加粗暴一些:
因为此时线程直接终止,没有catch异常的机会, 无法对线程结束这一行为作出任何补救动作。
无论是interrupt还是stop都是不安全的做法,因为如果我们在线程进行时打开了某些资源,那么这样粗暴的结束资源将无法正确关闭,所以提倡以下做法:
用一个标志flag来控制线程的结束
首先我们想中断一个线程,可以使用interrupt和stop两种方式。
首先说interrupt, 它没有stop那么的粗暴,因为可以用catch捕捉到InterruptedException这个异常
package thread; import java.util.Date; public class interrupt { public static void main(String[] args){ MyThread mythread =new MyThread(); mythread.start(); try{ Thread.sleep(10000); }catch(InterruptedException e){ } mythread.interrupt(); //mythread.flag=false; } } class MyThread extends Thread{ public boolean flag =true; public void run(){ while(true){ System.out.println(new Date()); try{ sleep(1000); }catch(InterruptedException e){ System.out.println("Oh,no!!"); return; } } } }
这里主函数中10000ms后打断了这个线程
输出如下:
Thu Apr 03 20:36:11 CST 2014 Thu Apr 03 20:36:12 CST 2014 Thu Apr 03 20:36:13 CST 2014 Thu Apr 03 20:36:14 CST 2014 Thu Apr 03 20:36:15 CST 2014 Thu Apr 03 20:36:16 CST 2014 Thu Apr 03 20:36:17 CST 2014 Thu Apr 03 20:36:18 CST 2014 Thu Apr 03 20:36:19 CST 2014 Thu Apr 03 20:36:20 CST 2014
Oh,no!!
如果使用stop方法,则更加粗暴一些:
Thu Apr 03 20:49:06 CST 2014 Thu Apr 03 20:49:07 CST 2014 Thu Apr 03 20:49:08 CST 2014 Thu Apr 03 20:49:09 CST 2014 Thu Apr 03 20:49:10 CST 2014 Thu Apr 03 20:49:11 CST 2014 Thu Apr 03 20:49:12 CST 2014 Thu Apr 03 20:49:13 CST 2014 Thu Apr 03 20:49:14 CST 2014 Thu Apr 03 20:49:15 CST 2014
因为此时线程直接终止,没有catch异常的机会, 无法对线程结束这一行为作出任何补救动作。
无论是interrupt还是stop都是不安全的做法,因为如果我们在线程进行时打开了某些资源,那么这样粗暴的结束资源将无法正确关闭,所以提倡以下做法:
package thread; import java.util.Date; public class interrupt { public static void main(String[] args){ MyThread mythread =new MyThread(); mythread.start(); try{ Thread.sleep(10000); }catch(InterruptedException e){ } //mythread.stop(); mythread.flag=false; } } class MyThread extends Thread{ public boolean flag =true; public void run(){ while(flag){ System.out.println(new Date()); try{ sleep(1000); }catch(InterruptedException e){ System.out.println("Oh,no!!"); return; } } } }
用一个标志flag来控制线程的结束
相关文章推荐
- 在停止线程时,调用每个线程的stopRunning方法,如果存在堵塞的情况,在辅助调用该线程的interrupt方法。
- 使用interrupt方法中断线程
- java线程中断,interrupt()方法的一些示例
- Java多线程-通过线程的中断来深入学习interrupt方法,Volatile关键字
- java 多线程5: java 终止线程及中断机制 (stop()、interrupt() 、interrupted()、isInterrupted())
- java 线程的中断interrupt方法
- java中的interrupt和stop方法——java中线程停止的方法
- 停止Java线程,弃用stop()方法, 也要小心interrupt()方法
- Java学习笔记78. 线程的中断入门 —— interrupt方法
- 使用interrupt方法中断线程
- 线程stop及interrupt方法学习
- Java之多线程interrupt中断线程的三种方法
- java 多线程(1) join() / interrupt() 打断sleep() / stop() / run() / 线程停止的方法
- 线程中断方法interrupt() 与 cancel()
- 线程中断 interrupt() 与 线程终止方法
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期
- 停止Java线程,小心interrupt()方法
- 关于java线程的interrupt方法的使用总结
- Java Thread.interrupt 害人! 中断JAVA线程(zz)
- java线程中的interrupt,isInterrupt,interrupted方法