C#使用任务并行库(TPL)

TPL(Task Parallel Library)

任务并行库 (TPL) 是 System.Threading和 System.Threading.Tasks 命名空间中的一组公共类型和 API。 TPL 的目的是通过简化将并行和并发添加到应用程序的过程来提高开发人员的工作效率。

使用线程池可以减少并行操作时操作系统资源的开销,然而使用线程池并不简单,从线程池的工作线程中获取结果也并不容易。于是就有了TPL,TPL可被认为是线程池上的又一个抽象层,其对开发人员隐藏了与线程池交互的底层代码,并提供了更细粒度的API。

TPL的核心概念是任务。一个任务代表了一个异步操作,该操作可以通过多种方式运行,可以使用或不使用独立线程运行。

更详细的说明,可以访问

https://docs.microsoft.com/zh-cn/dotnet/standard/parallel-programming/task-parallel-library-tpl

如何创建一个任务

使用Task.Run()方法或Task.Factory.StartNew方法。

创建一个控制台应用程序,输入以下代码

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //使用Task的构造函数
 6             Task task1 = new Task(Task1Method);
 7             task1.Start();
 8 
 9             //使用Task.Run
10             Task.Run(()=>  Task2Method());
11 
12             //使用Task.Factory.StartNew
13             Task.Factory.StartNew(Task3Method);
14 
15             Console.ReadLine();
16         }
17 
18 
19         static void Task1Method()
20         {
21             Console.WriteLine($"task1 run in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
22         }
23 
24         static void Task2Method()
25         {
26             Console.WriteLine($"task2 run in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
27         }
28 
29         static void Task3Method()
30         {
31             Console.WriteLine($"task3 run in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
32         }
33     }

 运行结果如下

以同步方式运行任务

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //主线程运行
 6             TaskMethod("Task1");
 7 
 8             Task task2 = new Task(()=>TaskMethod("Task2"));
 9             Task task3 = new Task(()=>TaskMethod("Task3"));
10 
11             //(同步)主线程运行
12             task2.RunSynchronously();
13 
14             //(异步)线程池运行
15             task3.Start();
16         }
17 
18         static void TaskMethod(string name)
19         {
20             Console.WriteLine($"Task {name} is running on a thread id : {System.Threading.Thread.CurrentThread.ManagedThreadId}, " + 
21                               $"Is thread pool thread: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}");
22             System.Threading.Thread.Sleep(1000);
23            
24         }
25     }

运行结果:

使用单独线程的任务

如果任务的代码将长时间运行,可以使用TaskCreationOptions.LongRunning来告诉任务创建一个新线程,而不是使用线程池中的线程

示例代码如下

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
 6             var t1 = new Task(TaskMethod,TaskCreationOptions.LongRunning);
 7             t1.Start();
 8         }
 9 
10         static void TaskMethod()
11         {
12             Console.WriteLine(System.Threading.Thread.CurrentThread.IsThreadPoolThread);
13             Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
14         }
15     }

运行结果:

使用任务来执行操作

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             TaskMethod("Task1");//会阻塞主线程,直到耗时操作完成
 6 
 7             Task<int> task = CreateTask("Task 2");
 8             Console.WriteLine(task.Status);
 9             task.Start();//启动任务,并不会阻塞主线程,会继续往下执行
10             while(!task.IsCompleted)
11             {
12                 Console.WriteLine(task.Status);
13                 System.Threading.Thread.Sleep(1000);
14             }
15             Console.WriteLine(task.Status);
16             int result = task.Result;
17             Console.WriteLine($"Result is : {result}");
18         }
19 
20         static Task<int> CreateTask(string name)
21         {
22             return new Task<int>(()=> TaskMethod(name));
23         }
24 
25         static int TaskMethod(string name)
26         {
27             Console.WriteLine($"Task {name} is running on a thread id : {System.Threading.Thread.CurrentThread.ManagedThreadId}, Is thread pool thread: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}");
28             //模拟耗时操作
29             System.Threading.Thread.Sleep(10000);
30             return 0;
31         }
32     }

运行结果:

组合任务

使用Task< TResult> . ContinueWith方法来创建当另一任务完成时可以执行的延续任务。

当task1执行完成后,把task1返回的结果传递到下一个任务

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Task<int> task1 = new Task<int>(()=> TaskMethod("Task1",1));
 6              7 
 8             task1.ContinueWith(x => Console.WriteLine($"The task1 result is {x.Result} " + 
 9                 $"Thread id is : {System.Threading.Thread.CurrentThread.ManagedThreadId} " + 
10                 $"Is Thread pool thread : {System.Threading.Thread.CurrentThread.IsThreadPoolThread} "),TaskContinuationOptions.OnlyOnRanToCompletion);
11             //TaskContinuationOptions.OnlyOnRanToCompletion 指定只应在延续任务前面的任务已完成运行的情况下才安排延续任务。
12             //更多TaskContinuationOptions可以参考
13             //https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskcontinuationoptions?redirectedfrom=MSDN&view=netframework-4.8
14 
15             task1.Start();        
16         }
17 
18         static int TaskMethod(string name,int seconds)
19         {
20             Console.WriteLine($"Task {name} is running on thread id :{System.Threading.Thread.CurrentThread.ManagedThreadId} " + 
21                 $"Is thread pool thread:{System.Threading.Thread.CurrentThread.IsThreadPoolThread} ");
22             System.Threading.Thread.Sleep(TimeSpan.FromSeconds(seconds));
23             return DateTime.Now.Second;
24         }
25     }

 运行结果:

原文地址:https://www.cnblogs.com/zhaotianff/p/10458075.html