C# Thread、delegate、Task三種非同步進行對比

來源:互聯網
上載者:User

標籤:委託   tree   執行個體   對比   速度   lin   ret   wait   null   

1.Thread 【線程】

不斷建立 Thread 會消耗很大的cup 導致 非同步效率變慢

2.delegate【委託非同步】

由於需要不斷的執行個體化 委託類 將會消耗cup 和 記憶體

3.Task 【結合  async】

這種是三種中效能最好的  較低的cup和記憶體損耗

下面是例子:

#region 委託非同步 和  asycn 非同步進行對比
        /// <summary>
        /// 運行
        /// </summary>
        /// <param name="count"></param>
        static void Run(int count)
        {

            // TestTreed(  count);
            TestAsycnTask(count);
         //   TestDelegate(count);

        }


        /// <summary>
        /// 損耗cup 而且建立速度慢
        /// </summary>
        /// <param name="count"></param>
        static void TestTreed(int count)
        {
            for (int i = 0; i < count; i++)
            {
                //  Console.WriteLine("開始計算");
                new Thread(a =>
                {
                    Console.WriteLine(i);
                }).Start();
            }
          

        }
        //***************Task 部分

        /// <summary>
        /// Task非同步
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        static async Task<int> AsyncTask(int i)
        {

            // await Task.Delay(1500);
            Console.WriteLine(i);
            return i;
        }

        /// <summary>
        /// 測試 asycn 只會消耗小部分 cup
        /// </summary>
        /// <param name="count"></param>
        static void TestAsycnTask(int count)
        {
            for (int i = 0; i < count; i++)
            {
              //  Console.WriteLine("開始計算");
                 AsyncTask(i);//直接就非同步了
            }
            
        }


        //********************************委託部分

        private delegate int _delegate(int i);
        /// <summary>
        /// 因為建立  委託  所以會消耗記憶體  和cup
        /// </summary>
        /// <param name="count"></param>
        static void TestDelegate(int count)
        {
            for (int i = 0; i < count; i++)
            {
                var d = new _delegate(Delegte);
                var r = d.BeginInvoke(i, null, null);

            }
        }

        /// <summary>
        /// 委託函數
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        static int Delegte(int i)
        {
            Console.WriteLine(i);
            return i;
        }

     

#endregion  


C# Thread、delegate、Task三種非同步進行對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.