thread.abort java_C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)...
1、Abort当前线程,后续程序不会执行
class Program
{
public static Thread thread1;
static void Main(string[] args)
{
thread1 = new Thread(Method1);
thread1.Start();
Console.ReadKey();
}
public static void Method1()
{
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Mthod1: " + i.ToString());
Thread.Sleep(1000);
if (i == 3)
{
Thread.CurrentThread.Abort(); // 抛出的ThreadAbortException异常
}
Console.WriteLine("Mthod1: " + i.ToString() + " End");
}
}
catch (SocketException ex)
{
Console.WriteLine("Method1 SocketException: " + ex.ToString());
}
catch (ThreadAbortException ex)
{
// ThreadAbortException要在Exception的前面,因为Exception能够匹配所有异常
Console.WriteLine("Method1 ThreadAbortException: " + ex.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Method1 Exception: " + ex.ToString());
}
finally
{
Console.WriteLine("Method1 Finally:");
}
}
}
执行结果:
1、thread1.Abort()执行后会直接抛出ThreadAbortException异常。
2、异常会停止后续所有程序的执行(没有输出"Method1: 3 End")。
3、catch语句会执行。catch (Exception ex)和catch (ThreadAbortException ex)都可以捕获异常,由于Exception可以匹配所有异常,因此catch (ThreadAbortException ex)应该在catch (Exception ex)的前面使用,所以输出了“"Method1 ThreadAbortException”。
4、finally语句会执行。
2、Abort当前线程,进行try catch捕获异常
class Program
{
public static Thread thread1;
static void Main(string[] args)
{
thread1 = new Thread(Method1);
thread1.Start();
Console.ReadKey();
}
public static void StopMethod1()
{
try
{
thread1.Abort(); // 首先捕获抛出的ThreadAbortException异常
}
catch (Exception ex)
{
Console.WriteLine("StopMethod1: " + ex.ToString());
}
}
public static void Method1()
{
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Mthod1: " + i.ToString());
Thread.Sleep(1000);
if (i == 5) StopMethod1(); // 再次捕获抛出的ThreadAbortException异常
}
}
catch (Exception ex)
{
Console.WriteLine("Method1: " + ex.ToString());
}
}
}
运行结果:
try catch{} 捕获两次抛出的ThreadAbortException。
2、Abort当前线程,不进行try catch捕获异常
class Program
{
public static Thread thread1;
static void Main(string[] args)
{
thread1 = new Thread(Method1);
thread1.Start();
Console.ReadKey();
}
public static void StopMethod1()
{
thread1.Abort();
}
public static void Method1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Mthod1: " + i.ToString());
Thread.Sleep(1000);
if (i == 5) StopMethod1();
}
}
}
运行结果:
1、虽然线程抛出了ThreadAbortException异常,由于线程中没有使用try catch的语句捕获异常,所以看上去线程很平静的退出了。
2、而且,由于是在线程中抛出的异常,所以主进程没有任何错误提示。
3、for循环只输出到5,Abort()后一般线程会直接停止。
3、在一个线程中Abort另一个线程
class Program
{
public static Thread thread1;
public static Thread thread2;
static void Main(string[] args)
{
thread1 = new Thread(Method1);
thread2 = new Thread(Method2);
thread1.Start();
thread2.Start();
Console.ReadKey();
}
public static void StopMethod1()
{
try
{
thread2.Abort();
}
catch (Exception ex)
{
Console.WriteLine("StopMethod1: " + ex.ToString());
}
}
public static void Method1()
{
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Method1: " + i.ToString());
Thread.Sleep(1000);
if (i == 5) StopMethod1();
}
}
catch (Exception ex)
{
Console.WriteLine("Method3: " + ex.ToString());
}
}
public static void Method2()
{
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Method2: " + i.ToString());
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine("Method2: " + ex.ToString());
}
}
}
运行结果:
1、可以看到只在thread2中捕获到了ThreadAbortException异常。
2、thread1完整的运行完毕了,并且没有ThreadAbortException异常抛出。
总结
1、theadX.Abort()方法会在线程X中抛出ThreadAbortException异常,线程X中所有正在执行的try catch都会捕获该异常。
2、theadX.Abort()有可能在Y线程中调用的,不会在Y线程中抛出ThreadAbortException异常。
thread.abort java_C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)...相关推荐
- Thread的start()和join()方法
start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码.通过调用Thread类的start()方法来启动一个线程,这时此线程处于就 ...
- 【Groovy】Groovy 扩展方法 ( Groovy 扩展方法引入 | 分析 Groovy 中 Thread 类的 start 扩展方法 )
文章目录 一.Groovy 扩展方法引入 二. 分析 Groovy 中 Thread 类的 start 扩展方法 一.Groovy 扩展方法引入 Groovy 可以对 JDK 中的一些类进行 方法扩展 ...
- libreportparam2.dll无法继续执行代码_Java并发系列番外(1)——让Thread#stop方法无法终止你的线程...
众所周知,Thread 类有一个 stop 方法,可以用来终止线程.当然,这个方法连同 suspend,resume 方法一起都已经被弃用了.原因是 stop 方法终止线程过于暴力,不管被 stop ...
- java Thread 类的几种方法
//后台进程 /** 对于java来说只要还有一个前台线程在运行,这个进程就不结束,如果一个进程* 只有后台线程,这个进程就会结束*/ package xian_cheng;public class ...
- 得到当前堆栈信息的两种方式(Thread和Throwable)的方法
今天看到有一个工具类中有一句 Thread.currentThread().getStackTrace()[2].getClassName(); 原来工作中遇到的问题:使用Thread.current ...
- 并发编程——线程——Thread对象的属性和方法
Thread实例对象的方法 # isAlive(): 返回线程是否活动的.# getName(): 返回线程名.# setName(): 设置线程名. threading模块提供的一些方法: # th ...
- Java并发编程—Thread类的start()方法是如何启动一个线程的?
目录 一:Java线程介绍 二:Java线程入口分析 三:Java线程的创建 四:总结 周末抽了点时间,研究了下HotSpot是如何创建Java线程的,顺便总结一下.文中引用的源码里删除很多细节,只保 ...
- Android studio ERROR: Software caused connection abort: recv failed 解决方法
使用Android studio打开kotlin项目时报错:ERROR: Software caused connection abort: recv failed 解决方法:修改三个文件即可 1.修 ...
- Thread类中的join方法
package charpter06; //类实现接口 public class Processor implements Runnable { // 重写接口方法 @Override public ...
最新文章
- JavaScript设计模式与开发实践——JavaScript的多态
- 【转】计算机学会推荐国际学术期刊
- 视频编解码器 2020-比赛开始!
- Vue Router 4.0 正式发布!焕然一新。
- glassfish_RIP GlassFish –感谢所有的鱼。
- python 入门学习
- 鼠标控制视角wasd移动_绝地求生:为什么控制方向键是WASD?网友:就不能是其他键位吗?...
- Mysql导入导出数据总结
- 匿名内部类 可以访问外部类_Java 内部类与外部类的互访使用小结
- Ubuntu 11.10 快捷键 gnome gFTP 服务器 vsftpd 程序 面板
- 关于shell读取文件打印时展开通配符
- C++STL之双端队列Deque
- vector 实现二维数组
- Unity3D AssetStore下载文件/项目保存位置
- Spotfire 表达式之转换函数
- 计算机科学技术学报官网convex,计算机学报chin
- WISP Client+AP WDS 的区别
- SpringMVC入门案例【三层架构和MVC、SpringMVC的概述和入门程序】(超详细)
- 视频:插入U盘后Win7系统蓝屏,报iusb3xhc.sys
- 参数方程求二阶导时候不能直接把y,x分别对t求二阶导然后再相除作为d²y/dx²的原因