从头认识多线程-1.6 迫使线程停止的方法-异常法
来源:互联网 发布:matlab编程第四版 pdf 编辑:程序博客网 时间:2024/06/07 01:54
这一章节我们来讨论一下迫使线程停止的方法-异常法。
1.伪停止法
在介绍异常法前,我们来看一下一个伪停止的例子。
一般使用多线程,都是在执行一些循环任务,那么,我只要停止了for,就停止了线程了,这是错误的。
例子
package com.ray.deepintothread.ch01.topic_6;public class FakeInterruptSample {public static void main(String[] args) throws InterruptedException {ThreadFive threadFive = new ThreadFive();threadFive.start();threadFive.interrupt();}}class ThreadFive extends Thread {@Overridepublic void run() {for (int i = 0; i < 10; i++) {if (Thread.interrupted()) {System.out.println("退出循环");break;}System.out.println("interrupt:" + Thread.currentThread().isInterrupted());try {sleep(50);} catch (InterruptedException e) {}}System.out.println("测试数据");super.run();}}
输出:
退出循环
测试数据
从上面可以看出,虽然我们退出了循环,但是线程还是继续执行的
2.异常法
package com.ray.deepintothread.ch01.topic_6;public class InterruptWithExceptionSample {public static void main(String[] args) throws InterruptedException {ThreadSix threadSix = new ThreadSix();threadSix.start();}}class ThreadSix extends Thread {@Overridepublic void run() {for (int i = 0; i < 10; i++) {if (i == 3) {System.out.println("退出线程");throw new RuntimeException();}System.out.println("interrupt:" + Thread.currentThread().isInterrupted());try {sleep(50);} catch (InterruptedException e) {}}System.out.println("测试数据");super.run();}}
输出:
interrupt:false
interrupt:false
interrupt:false
退出线程
Exception in thread "Thread-0" java.lang.RuntimeException
at com.ray.deepintothread.ch01.topic_6.ThreadSix.run(InterruptWithExceptionSample.java:17)
从输出可以看见,当我们抛出异常的时候,线程就直接停止了,但是有一点需要注意的是,不能catch这个异常,不然这个不起作用
不起作用的异常:
package com.ray.deepintothread.ch01.topic_6;public class InterruptWithExceptionSample2 {public static void main(String[] args) throws InterruptedException {ThreadOne threadOne = new ThreadOne();threadOne.start();}}class ThreadOne extends Thread {@Overridepublic void run() {for (int i = 0; i < 10; i++) {if (i == 3) {System.out.println("想退出");try {throw new RuntimeException();} catch (RuntimeException e) {}}System.out.println("interrupt:" + Thread.currentThread().isInterrupted());try {sleep(50);} catch (InterruptedException e) {}}System.out.println("测试数据");super.run();}}
输出:
interrupt:false
interrupt:false
interrupt:false
想退出
interrupt:false
interrupt:false
interrupt:false
interrupt:false
interrupt:false
interrupt:false
interrupt:false
测试数据
从上面的输出可以看见,线程的运行并没有停止。
总结:这一章节主要讲述了退出线程的方法-异常法。
我的github:https://github.com/raylee2015/DeepIntoThread
1 0
- 从头认识多线程-1.6 迫使线程停止的方法-异常法
- 从头认识多线程-1.7 迫使线程停止的方法-异常法的扩展(Sleep的Exception)
- 从头认识多线程-1.9 迫使线程停止的方法-return法
- 从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法
- 从头认识java-18.2 基本的线程机制(8)多线程的异常捕捉
- 从头认识多线程-1.17 守护线程setDaemon()
- 从头认识多线程-2.8 同步方法的隐患
- 从头认识多线程-2.6 当异常出现时,线程自动释放锁
- 从头认识多线程-1.1 多线程的创建
- 停止线程--异常法
- 多线程基础四(二)、能停止的线程(异常法、sleep、return、interrupt、stop)
- 从头认识多线程-2.5 锁的可重入性
- 从头认识java-18.2 基本的线程机制(1)-再识多线程-1
- 从头认识java-18.2 基本的线程机制(1)-再识多线程-2
- 从头认识多线程-2.1 局部变量与实例变量的线程安全
- 从头认识多线程-目录
- 从头认识多线程-目录
- 从头认识多线程-1.2 共享变量与线程安全简介
- iOS 移除所有子视图
- POST请求工具函数
- JIRA忘记admin密码的恢复方法
- oracle执行计划分析
- 使用框架实现上拉下刷功能
- 从头认识多线程-1.6 迫使线程停止的方法-异常法
- Google Analytics内容分组功能与网站页面分类
- d3.js——面积图表的制作
- 机器学习:EM算法
- web测试清单
- 属性(Property)
- http 连接池
- 微博营销—新浪微博粉丝头条效果分析
- 标准Web系统的架构分层