Net Core教程

C# Async Await 异步多线程

本文主要是介绍C# Async Await 异步多线程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Async_Await
{
    class Program
    {
        static  void Main(string[] args)
        {
            Program prm = new Program();
            Console.WriteLine($"主线程start{Thread.CurrentThread.ManagedThreadId}");
            prm.Invoke();
            Console.WriteLine($"主线程end{Thread.CurrentThread.ManagedThreadId}");
            Console.ReadKey();
        }

        public async void Invoke()
        {
            Program prm = new Program();
            long s =await prm.LongAsync();
            int l = await prm.DoSumAsync(22, 3);
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}+Hi" + s.ToString());
            prm.TestAsync();
           
        }
        public void TestAsync()
        {
            Console.WriteLine($"TestAsync执行{Thread.CurrentThread.ManagedThreadId}");
            List<Task> lst = new List<Task>();
            for (int i = 0; i < 5; i++)
            {
                int k = i;//要不然代码中i一直为5
                lst.Add(Task.Run(() => { Console.WriteLine($"TestAsync子线程{k}Start{Thread.CurrentThread.ManagedThreadId}"); }));
            }
            TaskFactory tf = new TaskFactory();
            //tf.ContinueWhenAny(lst.ToArray(), t => { Console.WriteLine($"TestAsync子线程某一个执行完成{Thread.CurrentThread.ManagedThreadId}"); });
            tf.ContinueWhenAll(lst.ToArray(), t => { Console.WriteLine($"TestAsync子线程全部执行完成{Thread.CurrentThread.ManagedThreadId}"); });
            Console.WriteLine($"TestAsync结束{Thread.CurrentThread.ManagedThreadId}");
        }

        public async Task<long> LongAsync()
        {
            Console.WriteLine($"LongAsync执行{Thread.CurrentThread.ManagedThreadId}");

               long lg= await Task.Run(() =>
               {
                   Thread.Sleep(2000);
                   long s = 0;
                   Console.WriteLine($"LongAsync1 Start{Thread.CurrentThread.ManagedThreadId}");
                   for (int i = 0; i < 100000; i++)
                   {
                       s += i;
                   }
                   Console.WriteLine($"LongAsync1 End{Thread.CurrentThread.ManagedThreadId}");
                   return s;
               });

            //Console.WriteLine(s.ToString()+$"当前线程ID{ Thread.CurrentThread.ManagedThreadId}");


            long lgs=await Task.Run(() =>
            {
                Thread.Sleep(2000);
                long v = 0;
                Console.WriteLine($"LongAsync2 Start{Thread.CurrentThread.ManagedThreadId}");
                for (int i = 0; i < 1000000; i++)
                {
                    v += i;
                }
                Console.WriteLine($"LongAsync2 End{Thread.CurrentThread.ManagedThreadId}");
                return v;
            });

            Console.WriteLine(lgs.ToString());
            Console.WriteLine($"LongAsync 结束{Thread.CurrentThread.ManagedThreadId}");
            return lg;
        }

        public async Task<int> DoSumAsync(int a, int b)
        {
            //3.await 表达式
            Console.WriteLine($"DoSumAsync Start{Thread.CurrentThread.ManagedThreadId}");

            int sum = await Task.Run(() => { Console.WriteLine(a + b); return a + b; });

            Console.WriteLine($"DoSumAsync End{Thread.CurrentThread.ManagedThreadId}");
            return sum;
        }
    }

}

主 线程碰到await Task会自动返回,去执行其他,await Task后的代码需要等待其先完成后,再交给其他线程池的线程去完成.

结果:

 

这篇关于C# Async Await 异步多线程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!