C#中的异步多线程1 同步和异步对比

同步版本示例:

namespace SyncSample
{
    class MyDownloadString
    {
        Stopwatch sw = new Stopwatch();
        public void DoRun()
        {
            const int LargeNumber = 6000000;
            sw.Start();
            //网络任务
            int t1 = CountCharacters(1, "https://www.baidu.com");
            int t2 = CountCharacters(2, "https://www.sogou.com/");
            //本机任务
            CountToALargeNumber(1, LargeNumber);
            CountToALargeNumber(2, LargeNumber);
            CountToALargeNumber(3, LargeNumber);
            CountToALargeNumber(4, LargeNumber);
            Console.WriteLine("Chars in Baidu:{0}", t1);
            Console.WriteLine("Chars in Sougou:{0}", t2);
        }
        private int CountCharacters(int id, string url)
        {
            WebClient wc1 = new WebClient();
            Console.WriteLine("Starting call {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
            string result = wc1.DownloadString(new Uri(url));
            Console.WriteLine("call {0} completed:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
            return result.Length;
        }
        private void CountToALargeNumber(int id, int value)
        {
            for (long i = 0; i < value; i++) ;
            Console.WriteLine("End counting {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyDownloadString ds = new MyDownloadString();
            ds.DoRun();
            Console.ReadLine();
            //同步时,会按照如下顺序执行 t1-t2-count1-count2-count3-count4,最耗时的任务发生在等待网站数据(starting call-call completed)的过程
            //但如果能够在等待的过程中就执行count1-4就会极大节省时间
        }
    }
}

耗时为:

Starting call 1: 0 ms
call 1 completed: 222 ms
Starting call 2: 222 ms
call 2 completed: 435 ms
End counting 1: 448 ms
End counting 2: 460 ms
End counting 3: 473 ms
End counting 4: 486 ms
Chars in Baidu:14462
Chars in Sougou:19466

对应异步示例:

    class MyDownLoadString
    {
        Stopwatch sw = new Stopwatch();
        public void DoRun()
        {
            const int LargeNumber = 600000;
            sw.Start();
            Task<int> t1 = CountCharactersAsync(1, "https://www.baidu.com");
            Task<int> t2 = CountCharactersAsync(2, "http://www.sougou.com");
            CountToALargeNumber(1, LargeNumber);
            CountToALargeNumber(2, LargeNumber);
            CountToALargeNumber(3, LargeNumber);
            CountToALargeNumber(4, LargeNumber);
            Console.WriteLine("Chars in Baidu:{0}", t1.Result);
            Console.WriteLine("Chars in Sougou:{0}", t2.Result);
        }
        private async Task<int> CountCharactersAsync(int id, string site)
        {
            WebClient wc = new WebClient();
            Console.WriteLine("Starting call {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
            string result = await wc.DownloadStringTaskAsync(new Uri(site));
            Console.WriteLine("Call {0} completed:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
            return result.Length;
        }
        private void CountToALargeNumber(int id,int value)
        {
            for (long i = 0; i < value; i++) ;
            Console.WriteLine("End counting {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyDownLoadString ds = new MyDownLoadString();
            ds.DoRun();
            Console.ReadLine();
            //对于耗时的网络任务,修改成了异步执行的方式,于是任务执行演变成了t1 start,t2 start-count1-4-t1 completed/t2 completed
            //在等待网络任务的同时,完成了本机任务
            //而这些任务,都是在主线程完成,并没有创建其他线程
        }
    }

耗时为:

Starting call 1: 1 ms
Starting call 2: 58 ms
End counting 1: 62 ms
End counting 2: 63 ms
End counting 3: 64 ms
End counting 4: 65 ms
Call 2 completed: 199 ms
Call 1 completed: 243 ms
Chars in Baidu:14462
Chars in Sougou:16597

可见有效的压缩了时间,这是因为4次CountToALargeNumber都是在等待网站响应的过程中完成的,而这些工作都是在主线程完成的,并没有创建额外的线程。

 

posted @ 2020-05-22 16:32  NicolasLiaoRan  阅读(511)  评论(0编辑  收藏  举报