C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)

学习书籍: <C#本质论>

1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq).

其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式).

--用AggregateException处理Task上的未处理异常.

--取消任务. CancellationToken

--async修饰方法, 返回Task. task.wait(100)可以阻塞现场. async方法内await 启线程执行.

==> 第一部分, 5.0推荐的线程使用方式.

2--学习了Task 等线程同步使用方式:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

namespace Task1

{

    class Program

    {

        const int _Total = 99999;

        static long _Count = 0;

        readonly static object _Sync = new Object();

        static void Main(string[] args)

        {

            //Task task = Task.Run(()=>Decrement());

            //for (int i = 0; i < _Total; i++)

            //{

            //    _Count++;

            //}

            //task.Wait();

            //Console.WriteLine("Count = {0}", _Count);

            //CountAsync();

            //int x = 0;

            //Parallel.For(0, 999999, i =>

            //    {

            //        x++;

            //        x--;

            //    });

            //Console.WriteLine("Count = {0}",x);

            Task task = Task.Run(() => Decrement());

            for (int i = 0; i < _Total; i++)

            {

                bool lockTaken = false;

                try

                {

                    Monitor.Enter(_Sync, ref lockTaken);

                    _Count++;

                }

                finally

                {

                    if (lockTaken)

                    {

                        Monitor.Enter(_Sync);

                    }

                }

            }

            task.Wait();

            Console.WriteLine("Count = {0}", _Count);

            Console.ReadKey();           

        }

        public static async void CountAsync()

        {

            Task task = Task.Run(() => Decrement());

            for (int i = 0; i < _Total; i++)

            {

                _Count++;

            }

            await task;

            Console.WriteLine("Count = {0}", _Count);

        }

        static void Decrement()

        {

            for (int i = 0; i < _Total; i++)

            {

                bool lockTaken = false;

                try

                {

                    Monitor.Enter(_Sync, ref lockTaken);

                    _Count--;

                }

                finally

                {

                    if (lockTaken)

                    {

                        Monitor.Exit(_Sync);

                    }

                }

            }

        }

    }

}

==> 第二部分.

TPL和C#5.0之前的多线程模式

  
    
1. 调用APM

            #region

            String url = "www.baidu.com";

            if (args.Length > 0)

            {

                url = args[0]; 

            }

            Console.WriteLine(url);

            WebRequest webRequest = WebRequest.Create(url);

            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

            while(!asyncResult.AsyncWaitHandle.WaitOne(100))

            {

                Console.Write(".");

            }

            WebResponse response = webRequest.EndGetResponse(asyncResult);

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))

            {

                int length = reader.ReadToEnd().Length;

                Console.WriteLine(length);

            }

2-- 复杂带状态的操作

    1--CPS 连续调用样式. 的fire-and-forget模式. 减少了直接再beginXXX方法后调用EndXXX方法.

    通过CPS,"登记"异步方法结束时执行的代码.

3-- 在APM方法间传递状态

    1--可以通过Lambda完成委托BeginXXX调用.

你可能感兴趣的:(Pattern)