[置顶] Java 多线程 学习笔记(二)停止线程的几种方法
2016-07-03 15:36
856 查看
1.异常法:
运行结果:
......
i=255229
i=255230
i=255231
i=255232
i=255233
i=255234
i=255235
i=255236
i=255237
i=255238
i=255239
i=255240
i=255241
i=255242
i=255243
i=255244
i=255245
i=255246
i=255247
i=255248
i=255249
i=255250
i=255251
i=255252
i=255253
i=255254
i=255255
i=255256
i=255257
i=255258
i=255259
i=255260
i=255261
i=255262
i=255263
i=255264
i=255265
i=255266
已经是停止状态了!我要退出了!
end!
我被输出,如果此代码是for又继续运行,线程并未停止!
已经是停止状态了!我要退出了! 虽然停止,但是for循环以后的代码还是会执行的
正确的退出方式
2.在沉睡中停止
run begin
end!
在沉睡中被停止!进入catch!false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at exthread.MyThread.run(MyThread.java:9)
另外一种情况是先停止后sleep
3.暴力停止:
Thread.stop() 这种方法存在非线程安全问题。所以暂时不考虑
4.使用return 停止线程
package test; import exthread.MyThread; import exthread.MyThread; public class Run { public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } }
package exthread; public class MyThread extends Thread { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void run() { <span style="white-space:pre"> </span>super.run(); <span style="white-space:pre"> </span>for (int i = 0; i < 500000; i++) { <span style="white-space:pre"> </span>if (this.interrupted()) { <span style="white-space:pre"> </span>System.out.println("已经是停止状态了!我要退出了!"); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println("i=" + (i + 1)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println("我被输出,如果此代码是for又继续运行,线程并未停止!"); <span style="white-space:pre"> </span>} }
运行结果:
......
i=255229
i=255230
i=255231
i=255232
i=255233
i=255234
i=255235
i=255236
i=255237
i=255238
i=255239
i=255240
i=255241
i=255242
i=255243
i=255244
i=255245
i=255246
i=255247
i=255248
i=255249
i=255250
i=255251
i=255252
i=255253
i=255254
i=255255
i=255256
i=255257
i=255258
i=255259
i=255260
i=255261
i=255262
i=255263
i=255264
i=255265
i=255266
已经是停止状态了!我要退出了!
end!
我被输出,如果此代码是for又继续运行,线程并未停止!
已经是停止状态了!我要退出了! 虽然停止,但是for循环以后的代码还是会执行的
正确的退出方式
package exthread; public class MyThread extends Thread { @Override public void run() { super.run(); try { for (int i = 0; i < 500000; i++) { if (this.interrupted()) { System.out.println("已经是停止状态了!我要退出了!"); throw new InterruptedException(); } System.out.println("i=" + (i + 1)); } System.out.println("我在for下面"); } catch (InterruptedException e) { System.out.println("进MyThread.java类run方法中的catch了!"); e.printStackTrace(); } } }
2.在沉睡中停止
package exthread; public class MyThread extends Thread { @Override public void run() { super.run(); try { System.out.println("run begin"); Thread.sleep(200000); System.out.println("run end"); } catch (InterruptedException e) { System.out.println("在沉睡中被停止!进入catch!"+this.isInterrupted()); e.printStackTrace(); } } }
package test; import exthread.MyThread; import exthread.MyThread; public class Run { public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(200); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } }运行结果:
run begin
end!
在沉睡中被停止!进入catch!false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at exthread.MyThread.run(MyThread.java:9)
另外一种情况是先停止后sleep
3.暴力停止:
Thread.stop() 这种方法存在非线程安全问题。所以暂时不考虑
4.使用return 停止线程
package test.run; import extthread.MyThread; public class Run { public static void main(String[] args) throws InterruptedException { MyThread t=new MyThread(); t.start(); Thread.sleep(2000); t.interrupt(); } }
package extthread; public class MyThread extends Thread { @Override public void run() { while (true) { if (this.isInterrupted()) { System.out.println("停止了!"); return; } System.out.println("timer=" + System.currentTimeMillis()); } } }
相关文章推荐
- 初学java之12 泛型编程的个人理解总结
- [置顶] Java 多线程学习笔记 (一)interrupt
- 文章标题
- 修改eclipse中的propersties文件的默认编码格式
- 转: Android 软件开发之如何使用Eclipse Debug调试程序详解(七)
- 使用Spring MVC统一异常处理
- 【数字安全】Security in Java
- 自定义注解+Struts2拦截器实现简单权限控制
- 转: eclipse 快捷键列表(功能清晰版本)
- Spring AbstractRoutingDataSource 实现动态数据源切换
- java 中如何声明线程安全的集合 set, map 和list【转】
- spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
- Reverse Integer(java)
- Java类型信息与用反射机制编写通用的Excel导入导出
- JAVA---udp数据的发送与接收
- Java编程题目-4:数组操作问题
- quartz集群分布式(并发)部署解决方案-Spring
- JavaWeb概述(二)
- Java中public,private,protected,和默认的区别
- myeclipse中的svn的安装问题