C#多线程Task用法举例

概述

  Task是微软在.Net 4.0时代推出来的,Task看起来像一个Thread,实际上,它是在ThreadPool的基础上进行的封装,Task的控制和扩展性很强,在线程的延续、阻塞、取消、超时等方面远胜于Thread和ThreadPool,所以一经问世,基本ThreadPool就被取代了。.

Task用法举例

 static void Main(string[] args)        {            string str = "当前时间是";
            for (int i = 0; i < 10; i++)            {                Task.Run(() => ThreadFuc(str));            }                      Console.ReadKey();        }
        public static void ThreadFuc(string str)        {            Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Thread.Sleep(1000);        }

运行结果:

C# Task用法详解

这里开了十个线程,task默认就是在线程池中创建线程,他会自行调度和回收不用的线程,减少每次重新创建线程的开销,而且创建的是背景线程。

task有很多封装好的API,比如:

①WaitAll:等待提供的所有 System.Threading.Tasks.Task 对象完成执行过程。

 Task.WaitAll(t1,t2);//等待所有任务结束

② WaitAny:等待提供的任一 System.Threading.Tasks.Task 对象完成执行过程。

 Task.WaitAny(t1,t2);

③ContinueWith:创建一个在目标 System.Threading.Tasks.Task 完成时异步执行的延续任务。

实例代码:

 static void Main(string[] args)        {            string str = "当前时间是";
            Task t1 = Task.Run(() => ThreadFuc1(str));            Task t2 = Task.Run(() => ThreadFuc2(str));            Task.WaitAny(t1,t2);            t1.ContinueWith(task =>            {                Console.WriteLine($"任务完成时的状态:IsCanceled={task.IsCanceled}\tIsCompleted={task.IsCompleted}\tIsFaulted={task.IsFaulted}");            });
            Task.WaitAll(t1,t2);//等待所有任务结束             Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Console.ReadKey();        }
        public static void ThreadFuc1(string str)        {            Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Thread.Sleep(1000);        }        public static void ThreadFuc2(string str)        {            Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Thread.Sleep(2000);        }

这里分别开启了两个线程t1、t2,在t1里面等待1秒,t2里面等待2秒,所以执行WaitAny时先等到ti完成,WaitAll时会等到t2完成.最终输出结果如下:

C# Task用法详解

④RunSynchronously:同步启动,对当前的 System.Threading.Tasks.Task 同步运行 System.Threading.Tasks.TaskScheduler。(类似委托开启线程,BeginInvoke是异步,而Invoke是同步)

用法:

            Task t1 = new Task(() => ThreadFuc1(str));            Task t2 = new Task(() => ThreadFuc2(str));            t1.RunSynchronously();            t2.RunSynchronously();

C# Task用法详解

⑤Wait:等待 System.Threading.Tasks.Task 完成执行过程。

⑥Start:启动 System.Threading.Tasks.Task,并将它安排到当前的 System.Threading.Tasks.TaskScheduler中执行;

带返回值的使用方式:

 static void Main(string[] args)        {            string str = "当前时间是";
            Task<int> t1 = new Task<int>(() => ThreadFuc1(str));            Task<int> t2 = new Task<int>(() => ThreadFuc2(str));            t1.Start();            t2.Start();
            Task.WaitAny(t1,t2);            t1.ContinueWith(task =>            {                Console.WriteLine($"任务完成时的状态:IsCanceled={task.IsCanceled}\tIsCompleted={task.IsCompleted}\tIsFaulted={task.IsFaulted}");            });
            Task.WaitAll(t1,t2);//等待所有任务结束             int result1 = t2.Result;            int result2 = t2.Result;            Console.WriteLine($"result1值是{result1},result2值是{result2}");            Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Console.ReadKey();        }
        public static int ThreadFuc1(string str)        {            Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Thread.Sleep(1000);            return 1;        }        public static int ThreadFuc2(string str)        {            Console.WriteLine($"线程Id是{Thread.CurrentThread.ManagedThreadId},{str}:{DateTime.Now}");            Thread.Sleep(2000);            return 2;        }

运行结果如下:

C# Task用法详解