C#:多线程(Smart Thread Pool)-1

原文来自Smart Thread Pool,这篇文章很长(感觉不是一篇文章,而是一本书),文章描叙为:A .Net Thread Pool fully implement in C# with many features。

文中作者整合/改进了别人的线程池,变成我们现在看到的智能线程池(Smart Thread Pool,类库版本.Net 2.0)。它具有以下特征:

1)线程池中线程的数量根据工作量的变化而改变;

2)Work Items可以返回一个值;

3)如果Work Item没有开始执行,那它可以被取消的;

4)当work Item被执行时,调用线程的上下文(context)可以被使用的(不是很明白?);

5)使用最小数量的Window32事件句柄,可以保证在应用中句柄的数量不会大幅扩大(也就是使用系统资源比较少);

6)调用者可以等待多个或所有的WorkItem完成(这个很有用的);

7)一个Work Item可以有PostExecute的回掉函数,它主要用于当Work Item完成后开始被执行;

8)伴随Work Item的状态对象(state Object)可以被自动释放;

9)Work Item运行过程中的异常是被返回到调用者;

10)Work Item是有优先级的;

11)Work Item有组的区分;

12)调用者可以暂停线程池和Work Item组的开始;

13)线程也有优先级;

14)线程有初始化及结束事件;

15)支持winCE平台(没玩过);

16)支持Action<T>和Func<T>泛型方法(也不是很明白);

17)<t>SilverLight(Web前端应用开发)也支持;

18)Mono(.Net平台下混合编程语言)也支持;

19)<t>Performance Counters (Windows and internal);

20)WorkItem支持超时(主动);

21)线程ApartmentState;

22)线程是否为后台线程;

23)线程名字模板;

24)支持Windowns Phone(受限);

25)可以设定线程的MaxStackSize;


接下来我们通过实例代码来熟悉这个智能线程池。为了简化,通过控制台来直接调用。

CatchException:
public class CatchExceptionExample
{
        public void DoWork()
        {
            SmartThreadPool smartThreadPool = new SmartThreadPool();

            //the 3rd param double is the result
            IWorkItemResult<double> wir = smartThreadPool.QueueWorkItem(new Func<double, double, double>(DoDiv), 10.0, 0.0);

            try
            {
                double result = wir.Result;
                Console.WriteLine(result.ToString());
            }
            // Catch the exception that Result threw
            catch (WorkItemResultException e)
            {
                // Dump the inner exception which DoDiv threw
                Debug.WriteLine(e.InnerException);
            }

            smartThreadPool.Shutdown();
        }

        private double DoDiv(double x, double y)
        {
            return x / y;
        }
}

控制台输出的结果:


这印证了上面的第九条:Work Item返回的异常会返回到调用者。实际调用时是这样表达的:

public static void Main()
{
      CatchExceptionExample catchExc1 = new CatchExceptionExample();
      catchExc1.DoWork();
      Console.ReadKey();
}
CooperativeCancel:
public class CooperativeCancelExample
{
	public void DoWork(object state) 
	{ 
	    SmartThreadPool smartThreadPool = new SmartThreadPool();

            // Queue the work item
	    IWorkItemResult wir = smartThreadPool.QueueWorkItem(DoRealWork); 

            // Give the work item some time to complete.
            Thread.Sleep(1000);

            // If the work item hasn't completed yet then cancel it.
            if (!wir.IsCompleted)
            {
                Console.WriteLine("Work item canceling started");
                wir.Cancel();
                Console.WriteLine("Work item canceling ended");
            }

	    smartThreadPool.Shutdown();
	} 

	// Do some lengthy work
        private void DoRealWork()
        {
            Console.WriteLine("In DoRealWork()");

            Random rm = new Random();
            Thread.Sleep(1);
            int time = rm.Next(10, 30);

            // Do something here.
            Thread.Sleep(time);
            Console.WriteLine("Sleep before do real job,time=" + time.ToString());

            // Sample SmartThreadPool.IsWorkItemCanceled
            if (SmartThreadPool.IsWorkItemCanceled)
            {
                Console.WriteLine("Work Item canceled, return directly");
                return;
            }

            // Sample the SmartThreadPool.IsWorkItemCanceled in a loop
            while (!SmartThreadPool.IsWorkItemCanceled)
            {
                // Do some real work here
                Thread.Sleep(1);
                time = rm.Next(1000, 3000);
                Thread.Sleep(time);
                Console.WriteLine("Work Item do real job, time=" + time.ToString());
            }
        }
}

控制台输出的结果:


结果显示:已经进入到DoRealWork()中,由于没有结束,所以可以取消,对应上面的第三项。

GetException:
public class GetExceptionExample
{
	private class DivArgs
	{
		public int x;
		public int y;
	}

	public void DoWork() 
	{ 
		SmartThreadPool smartThreadPool = new SmartThreadPool();

		DivArgs divArgs = new DivArgs();
		divArgs.x = 10;
		divArgs.y = 0;

		IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs);

		Exception e;
		object obj = wir.GetResult(out e);
		// e contains the expetion that DoDiv threw
		if(null == e)
		{
			int result = (int)obj;
                        Console.WriteLine("No exception, got result= " + result.ToString());
		}
		else
		{
                        // Do something with the exception
                        Console.WriteLine("Got exception in result: " + e.ToString());
		}

		smartThreadPool.Shutdown();
	} 

	private object DoDiv(object state)
	{ 
		DivArgs divArgs = (DivArgs)state;
		return (divArgs.x / divArgs.y);
	}
}

控制台输出结果:


同样对应上面的第九项,对线程池中的结果进行检查,Exception也包含在结果中,区分异常e的方式是通过判断其是否为空。


猜你喜欢

转载自blog.csdn.net/huan_126/article/details/80228375