[.Net线程处理系列]专题二:线程池中的工作者线程
2012-7-18 19:12:00来源:Asp.net
目录:
一、上节补充
二、CLR线程池基础
三、通过线程池的工作者线程实现异步
四、使用委托实现异步
五、任务
一、上节补充
对于Thread类还有几个常用方法需要说明的。
1.1 Suspend和Resume方法
这两个方法在.net Framework 1.0的时候就支持的方法,他们分别可以挂起线程和恢复挂起的线程。但在.net Framework 2.0以后的版本中这两个方法都过时了,MSDN的解释是这样:
警告:
不要使用 Suspend 和 Resume 方法来同步线程的活动。您无法知道挂起线程时它正在执行什么代码。如果您在安全权限评估期间挂起持有锁的线程,则 AppDomain中的其他线程可能被阻止。如果您在线程正在执行类构造函数时挂起它,则 AppDomain中尝试使用该类的其他线程将被阻止。这样很容易发生死锁。
对于这个解释可能有点抽象吧,让我们来看看一段代码可能会清晰点:
class Program { static void Main(string[] args) { // 创建一个线程来测试 Thread thread1 = new Thread(TestMethod); thread1.Name = "Thread1"; thread1.Start(); Thread.Sleep(2000); Console.WriteLine("Main Thread is running"); ////int b = 0; ////int a = 3 / b; ////Console.WriteLine(a); thread1.Resume(); Console.Read(); } private static void TestMethod() { Console.WriteLine("Thread: {0} has been suspended!", Thread.CurrentThread.Name); //将当前线程挂起 Thread.CurrentThread.Suspend(); Console.WriteLine("Thread: {0} has been resumed!", Thread.CurrentThread.Name); } }
在上面这段代码中thread1线程是在主线程中恢复的,但当主线程发生异常时,这时候就thread1一直处于挂起状态,此时thread1所使用的资源就不能释放(除非强制终止进程),当另外线程需要使用这快资源的时候, 这时候就很可能发生死锁现象。
上面一段代码还存在一个隐患,请看下面一小段代码:
class Program { static void Main(string[] args) { // 创建一个线程来测试 Thread thread1 = new Thread(TestMethod); thread1.Name = "Thread1"; thread1.Start(); Console.WriteLine("Main Thread is running"); thread1.Resume(); Console.Read(); } private static void TestMethod() { Console.WriteLine("Thread: {0} has been suspended!", Thread.CurrentThread.Name); Thread.Sleep(1000); //将当前线程挂起 Thread.CurrentThread.Suspend(); Console.WriteLine("Thread: {0} has been resumed!", Thread.CurrentThread.Name); } }
当主线程跑(运行)的太快,做完自己的事情去唤醒thread1时,此时thread1还没有挂起而起唤醒thread1,此时就会出现异常了。并且上面使用的Suspend和Resume方法,编译器已经出现警告了,提示这两个方法已经过时, 所以在我们平时使用中应该尽量避免。
1.2 Abort和 Interrupt方法
Abort方法和Interrupt都是用来终止线程的,但是两者还是有区别的。
1、他们抛出的异常不一样,Abort 方法抛出的异常是ThreadAbortException, Interrupt抛出的异常为ThreadInterruptedException
-
【Silverlight】利用IsolatedStorageFile实现客户端缓存
2009-7-31 0:18:00为将一套系统局部改版为BS应用,最近在琢磨Silverlight,发现这玩意可能真能带来一场革命,对于程序员来说比Flash好的还真不是一星半点。 废话就不说了,来点实在的。我们有些数据,特别是
NBearMapping - 开源通用对象映射组件v1.0.0.0 beta
2007-7-25 15:50:00摘要: NBearMapping是NBearV4框架的组件之一,可以独立使用。可用于任意类型对象、DataRow和DataReader对象间的透明映射。建议结合NBearLite使用。 主要功能:
WF4 Workflow Service:SendParameters和ReceiveParameters
2009-12-11 22:45:00阅读: 68 评论: 0 作者: 生鱼片 发表于 2009-12-11 21:29 原文链接 本文主要介绍如何使用 SendParameters 和 ReceiveParamete
Background Garbage Collection in CLR 4.0
2009-5-30 8:46:00Yesterday I found this really nice Channel 9 interview with Maoni Stephens (Dev Owner of the CLR GC
Unable to load DLL ‘dllname.dll’: A dynamic link library (DLL) initialization routine failed. 0x8007045A
2009-4-30 7:53:00I recently got an email with the following question: “Can you give me some very helpful hints with
【原创】CTypedPtrList详细说明和具体用法
2010-4-12 8:07:00阅读: 0 评论: 0 作者: VCdog 发表于 2010-04-12 08:06 原文链接 CTypedPtrList类是个模板类,一般在软件开发中用到的时候比较多,它的作用就是类似一
多任务多线程(队列)示例代码
2009-7-17 11:52:00前几天需要使用一个多线程来完成工作,情况如下: 有一个列表,有N个任务,现在需要M个线程去实现它,不懂代码,没有办法完成,找同事完成了。 执行的过程如下 1、生成N个任务的队列(此
有道难题2的搞笑解,时间复杂度O(1)
2009-6-4 2:13:00经典查表法 static int[] arr; static T2() { arr = new int[1593]; arr[0] = 1729; arr[1] = 4104; a
A basic proxy for intercepting method calls (Part –1)
2010-1-4 20:46:00In this post i am going to show how you can write your own proxy for delegating calls. This just sh
Assembly.Load()方法,Assembly.LoadFrom()方法,Assembly.LoadFile()方法的区别!
2009-11-9 16:14:00阅读: 595 评论: 2 作者: 周雪峰 发表于 2009-11-09 13:07 原文链接 1,Assembly.Load() 这个方法通过程序集的长名称(包括程序集名,版本信息,